【问题标题】:I have data in array format, how could i take each value from an array and built a new array [closed]我有数组格式的数据,我如何从数组中获取每个值并构建一个新数组[关闭]
【发布时间】:2018-01-29 02:50:03
【问题描述】:

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

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

【问题讨论】:

标签: php arrays


【解决方案1】:

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

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

?>

输出:

array(2) {
  [0]=>
  array(3) {
    ["itemId"]=>
    string(4) "1001"
    ["itemName"]=>
    string(3) "Dan"
    ["itemDesc"]=>
    string(3) "Foo"
  }
  [1]=>
  array(3) {
    ["itemId"]=>
    string(4) "2002"
    ["itemName"]=>
    string(3) "Bob"
    ["itemDesc"]=>
    string(3) "Bar"
  }
}

【讨论】:

  • 对不起格式,非常感谢您提供解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多