转置索引数组的关联数组[重复]

问题描述 投票:0回答:4

尝试将“分组”多维数组转换为“分段”多维数组。不确定如何准确解释它,所以希望这些

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 来创建 Wanted Array

php arrays multidimensional-array foreach transpose
4个回答
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
        )

)

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
        )

)

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);

0
投票

您可以使用

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

$array2 = array();
foreach ($array1 as $key => $value)
{
  $array2[] = array('key' => 'value');
}
© www.soinside.com 2019 - 2024. All rights reserved.