如何转置数组元素?

时间:2016-04-08 06:37:41

标签: php arrays

我在php中有一个数组$categories,如下所示。

Array
(
    [item_name] => Array
        (
            [0] => I-Phone
            [1] => samsung
            [2] => nokia
            [3] => htc
        )

    [item_price] => Array
        (
            [0] => 30.00
            [1] => 20
            [2] => 10
            [3] => 15
        )
)

我想把它的元素转换为,

Array
(
    [0] => Array
        (
            [item_name] => I-Phone
            [item_price] => 30.00
        )

    [1] => Array
        (
            [item_name] => samsung
            [item_price] => 20
        )
    [2] => Array
        (
            [item_name] => nokia
            [item_price] => 10
        )

    [2] => Array
        (
            [item_name] => htc
            [item_price] => 15
        )
)

我尝试过使用foreach循环但没有工作。

$count=0;
foreach ($categories as $key=> $category)
{
        $categories[$count] = $category[$key];
        $categories[$count] = $category[$key];
        $count++;
}

5 个答案:

答案 0 :(得分:5)

这是一种方法:

$categories = array(
    'item_name' => array('I-Phone', 'samsung', 'nokia','htc'),
    'item_price' => array('30.00', '20', '10', '15')
);

$out = array();
foreach($categories as $key => $a){
    foreach($a as $k => $v){
        $out[$k][$key] = $v;
    }
}
echo '<pre>';
print_r($out);
echo '</pre>';

答案 1 :(得分:1)

这样的事情:

$categories = array();  // your array
$ctg_names = array_keys($categories);   // get names of source array keys
$new_ctgs = array();   // result array

// iterate over first key values to get indexes
foreach ($categories[$ctg_names[0]] as $k => $v) {
    $ctg = array();
    // iterate over all key names
    foreach ($ctg_names as $name) {
        $ctg[$name] = $categories[$name][$k];
    }
    $new_ctgs[] = $ctg;
}

print_r($new_ctgs);

答案 2 :(得分:1)

使用array_keysarray_map函数的简单解决方案:

$keys = array_keys($categories);
$transposed = array_map(function($a, $b) use($keys){
    return [$keys[0] => $a, $keys[1] => $b];
}, $categories['item_name'], $categories['item_price']);

print_r($transposed);

输出:

Array
(
    [0] => Array
        (
            [item_name] => I-Phone
            [item_price] => 30
        )

    [1] => Array
        (
            [item_name] => samsung
            [item_price] => 20
        )

    [2] => Array
        (
            [item_name] => nokia
            [item_price] => 10
        )

    [3] => Array
        (
            [item_name] => htc
            [item_price] => 15
        )    
)

答案 3 :(得分:0)

试试这个:

<?php

$array = Array
(
    'item_name' => Array
        (
            '0' => 'I-Phone',
            '1' => 'samsung',
            '2' => 'nokia',
            '3' => 'htc',
        ),

    'item_price' => Array
        (
            '0' => 30.00,
            '1' => 20,
            '2' => 10,
            '3' => 15,
        )
);

$keys = array_keys($array);
$result = array();

for($i = 0; $i < count($array['item_name']); $i++)
{
    $tempArray = array();
    foreach($keys as $key)
        $tempArray[$key] = $array[$key][$i];

    $result[] = $tempArray;
}

输出:

Array
(
    [0] => Array
        (
            [item_name] => I-Phone
            [item_price] => 30
        )

    [1] => Array
        (
            [item_name] => samsung
            [item_price] => 20
        )

    [2] => Array
        (
            [item_name] => nokia
            [item_price] => 10
        )

    [3] => Array
        (
            [item_name] => htc
            [item_price] => 15
        )

)

答案 4 :(得分:0)

$result = [];
foreach($categories['Item_name'] as $key => $data){
    $result[$key]['category'] = $categories['Item_name']['$key'];
    $result[$key]['price'] = $categories['price'][$key]; 
}
dd($result);