将PHP“分组”数组转换为“分段”数组

时间:2011-12-19 21:50:19

标签: php arrays multidimensional-array foreach

尝试将“分组”多维数组转换为“分段”多维数组。不确定如何解释它,所以希望这些print_r结果可以更好地解释它。

当前数组

Array
(
    [qty] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
        )

    [item_id] => Array
        (
            [0] => RR332
            [1] => FF586
            [2] => QW865
        )

    [item_name] => Array
        (
            [0] => Widget 1
            [1] => Widget 2
            [2] => Widget 3
        )

    [price] => Array
        (
            [0] => 1.00
            [1] => 1.50
            [2] => 1.50
        )

)

通缉数组

Array
(
    [0] => Array
        (
            [qty] => 1
            [item_id] => RR332
            [item_name] => Widget 1
            [price] => 1.00
        )

    [1] => Array
        (
            [qty] => 1
            [item_id] => FF586
            [item_name] => Widget 2
            [price] => 1.50
        )

    [2] => Array
        (
            [qty] => 1
            [item_id] => QW865
            [item_name] => Widget 3
            [price] => 1.50
        )

)

这似乎应该是一个简单的任务,但我还没有能够破解它。

如果可能,我最好循环使用 Current Array 创建通用数组

非常感谢任何帮助!

感谢。

4 个答案:

答案 0 :(得分:4)

你是说这个?:

$data = array(
  'qty' => array(
    0 => 1,
    1 => 1,
    2 => 1,
  ),
  'item_id' => array(
    0 => 'RR332',
    1 => 'FF586',
    2 => 'QW865',
  ),
  'item_name' => array(
    0 => 'Widget 1',
    1 => 'Widget 2',
    2 => 'Widget 3',
  ),
  'price' => array(
    0 => '1.00',
    1 => '1.50',
    2 => '1.50',
  ),
);

$result = array();
foreach($data as $key => $values)
{
    foreach($values as $number => $value)
        $result[$number][$key] = $value;
}

print_r($result);

输出:

Array
(
    [0] => Array
        (
            [qty] => 1
            [item_id] => RR332
            [item_name] => Widget 1
            [price] => 1.00
        )

    [1] => Array
        (
            [qty] => 1
            [item_id] => FF586
            [item_name] => Widget 2
            [price] => 1.50
        )

    [2] => Array
        (
            [qty] => 1
            [item_id] => QW865
            [item_name] => Widget 3
            [price] => 1.50
        )

)

答案 1 :(得分:3)

我认为这将为您完成这项工作,但我没有一个好的数据集可供测试。

$wanted_array = array();
// Iterate over the outer array to get the text keys & inner arrays
foreach ($current_array as $txtkey=>$arr) {
  // Iterate over each inner array to get the numeric key
  foreach ($arr as $numkey=>$val) {
    // Set a numeric key pointing to a new array 
    // onto the new outer array if it isn't already there
    if (!isset($wanted_array[$numkey])) $wanted_array[$numkey] = array();

    // Swap the keys of the outer and inner arrays.
    $wanted_array[$numkey][$txtkey] = $val;
  }
}

更新:使用来自@ hakre答案的测试数组,这里实际上是:

$current_array = array(
  'qty' => array(
    0 => 1,
    1 => 1,
    2 => 1
  ),
  'item_id' => array(
    0 => 'RR332',
    1 => 'FF586',
    2 => 'QW865'
  ),
  'item_name' => array(
    0 => 'Widget 1',
    1 => 'Widget 2',
    2 => 'Widget 3'
  ),
  'price' => array(
    0 => '1.00',
    1 => '1.50',
    2 => '1.50'
  )
);
// Output:
Array
(
    [0] => Array
        (
            [qty] => 1
            [item_id] => RR332
            [item_name] => Widget 1
            [price] => 1.00
        )

    [1] => Array
        (
            [qty] => 1
            [item_id] => FF586
            [item_name] => Widget 2
            [price] => 1.50
        )

    [2] => Array
        (
            [qty] => 1
            [item_id] => QW865
            [item_name] => Widget 3
            [price] => 1.50
        )

)

答案 2 :(得分:1)

就像你说的那样,只需循环就可以构建它。

$wanted = array();
foreach($current['item_id'] as $key => $value) {
    $wanted[$key] = array(
        'qty' = $current['qty'][$key],
        'item_id' = $current['item_id'][$key],
        'item_name' = $current['item_name'][$key],
        'price' = $current['price'][$key]
    );
}

print_r($wanted);

答案 3 :(得分:0)

您可以使用foreach循环遍历原始数组,该循环为您提供数组中每个项目的$ key和$ value。从那里,根据需要构建新的数组。

$array2 = array();
foreach ($array1 as $key => $value)
{
  $array2[] = array('key' => 'value');
}