0

假设我有一些看起来像这样的数据库行:

group    version  tool         info
group_a  m        screwdriver  for screwing things
group_a  m        hammer       for hitting things
group_a  l        screwdriver  for screwing things
group_a  xl       screwdriver  for screwing things
group_b  m        screwdriver  for screwing things
group_b  m        hammer       for hitting things
group_b  m        wrench       for wrenching things
group_b  l        screwdriver  for screwing things
group_b  xl       wrench       for wrenching things

从那我想创建一个看起来像这样的数组:

array(
    array( 'id' => 'group_a',
        array( 'id' => 'm',
            array( 'id' => 'screwdriver',
                'info' => 'for screwing things'
            ),
            array( 'id' => 'hammer',
                'info' => 'for hitting things'
            ),
        ),
        array( 'id' => 'l',
            array( 'id' => 'screwdriver',
                'info' => 'for screwing things'
            ),
        ),
        array( 'id' => 'xl',
            array( 'id' => 'screwdriver',
                'info' => 'for screwing things'
            ),
        )
    array( 'id' => 'group_b',
        array()
    )
    ~~~~~~~ etc ~~~~~~~
);

对于一些上下文,我将其作为 JSON 从 Zend 传递到 BackboneJS,然后将这些数组消耗到一个组集合中,其中包含 n 个版本集合,其中包含 n 个工具模型。

收到任何帮助,很高兴澄清任何问题。

干杯。

4

2 回答 2

0

将您的数据转换为一个三维临时数组。

$temparray=array();
foreach ($rows as $row) {
  $temparray[$row['group']][$row['version']][$row['tool']]=$row['info'];
}

这个临时数组看起来与您想要实现的相似,但不完全是:

Array
(
    [group_a] => Array(
            [m] => Array(
                    [screwdriver] => for screwing things
                    [hammer] => for hitting things
                )
            [l] => Array(
                    [screwdriver] => for screwing things
                )
            [xl] => Array(
                    [screwdriver] => for screwing things
                )
    )
    [group_b] => Array(
            [m] => Array(
                    [screwdriver] => for screwing things
                    [hammer] => for hitting things
                    [wrench] => for wrenching things
                )
            [l] => Array(
                    [screwdriver] => for screwing things
                )
            [xl] => Array(
                    [wrench] => for wrenching things
                )
    )
)

现在要么这足以满足您的需求,要么您必须重现您显示的确切结构。在后一种情况下,只需遍历该临时数组即可实现您的目标:

foreach ($temparray as $groupid=>$groupdata) {
  $grouparray=array('id'=>$groupid);
  foreach ($groupdata as $versionid=>$versiondata) {
    $versionarray=array('id'=>$versionid);
    foreach ($versiondata as $toolid=>$tooldata) {
      $toolarray=array('id'=>$infoid,'info'=>$tooldata);
      $versionarray[]=$toolarray;
    }
    $grouparray[]=$versionarray;
  }
  $targetarray[]=$grouparray;
}

你完成了:)

顺便说一句,以 'id',0,1,2,... 作为索引的数组有点尴尬。我不知道 BackboneJS,但是任何使用这种格式的脚本(而不是 temptable 的样子)在我看来都很可疑。

于 2013-02-23T23:34:42.560 回答
0

我真的不喜欢这个,因为它会像疯了一样抛出通知。但最终结果将是这样的:

$array = array();
while($row = fetch($query) {
  $array['id'][$row['group']]['id'][$row['version']]['id'] = $row['tool'];
  $array['id'][$row['group']]['id'][$row['version']]['info'] = $row['info'];
}

我会用 issets 让它更优雅,并检查以防止东西被覆盖和附加。

我可能会使用实际 ID 作为键而不是单词来帮助实现唯一性。

无论如何,这应该可以帮助您朝着正确的方向思考。

于 2013-02-23T18:23:20.960 回答