我有数组格式的数据,我怎么能从数组中获取每个值并构建一个新数组

时间:2017-08-21 05:57:37

标签: php arrays

我有这样的数组格式的数据: -

Array
(
    [itemId] => Array
        (
            [0] => 1001
            [1] => 1002
        )
    [itemName] => Array
        (
            [0] => Sample Item one
            [1] => Sample Item two
        )
    [itemDesc] => Array
        (
            [0] => Item Specifications
            [1] => Item Warranty
        )
    [itemCode] => Array
        (
            [0] => GL2113
            [1] => SP88293
        )
    [itemQty] => Array
        (
            [0] => 1
            [1] => 5
        )

    [itemType] => Array
        (
            [0] => Electronic
            [1] => Computer
        )
)

如何将其转换为以下格式: -

array(
    [0]=>array(
        [itemId]    =>1001,
        [itemName]  =>Sample Item one,
        [itemDesc]  =>Item Specifications,
        [itemCode]  =>GL2113,
        [itemQty]   =>1,
        [itemType]  =>Electronic
        )
    [1]=>array(
        [itemId]    =>1002,
        [itemName]  =>Sample Item two,
        [itemDesc]  =>Item Warranty,
        [itemCode]  =>SP88293,
        [itemQty]   =>5,
        [itemType]  =>Computer
        )
)

1 个答案:

答案 0 :(得分:2)

你对问题的格式感到非常难过,但这是你的解决方案

import Slider from 'react-slick';

输出:

<?php

$a = [
    'itemId'   => [ 
        '0' => '1001', 
        '1' => '2002' 
    ],
    'itemName' => [ 
        '0' => 'Dan', 
        '1' => 'Bob' 
    ],
    'itemDesc' => [
        '0' => 'Foo',
        '1' => 'Bar'
    ]    
];

$b = [];

foreach ($a as $aa => $v ) {
    foreach ($v as $kk => $vv) {
        $b[$kk][$aa] = $vv;
    }
}

var_dump($b);

?>