用其他格式转换数组的最简单方法是什么?

时间:2022-05-12 21:33:20

I have the following array:

我有以下数组:

Array
(
    [key1] => 10
    [key2] => 7
    [key3] => 8
)

I want to convert it to the following structure:

我想把它转换成如下结构:

Array
(
    [0] => Array
        (
            [section] => key1
            [quantity] => 10
        ),
    [1] => Array
        (
            [section] => key2
            [quantity] => 7
        ),
    [2] => Array
        (
            [section] => key3
            [quantity] => 8
        )
)

I tried to use array_map function, but inside the closure I get only a value without a key. Please, suggest me the easiest way to do it, without foreach.

我尝试使用array_map函数,但是在闭包中,我只能得到一个没有键的值。请给我建议最简单的方法,不要忘记。

Update
I think using foreach requires creation of an additional variable which makes the code messy and resource requiring.

更新我认为使用foreach需要创建一个额外的变量,这会使代码变得混乱并且需要资源。

4 个解决方案

#1


2  

Try using array_walk as

尽可能使用array_walk

$result = array();
array_walk($arr, function ($v,$k) use(&$result) {
     $result[] = array('section' => $k, 'quantity' => $v);
});

demo using array_walk

演示使用array_walk

Using foreach

使用foreach

$result = array();
foreach($arr as $key => $value){
    $result[] = array('section' => $key, 'quantity' => $value);
}

print_r($result);

demo using foreach

演示使用foreach

#2


1  

There's no reason not to use foreach for this. The best solution would be:

没有理由不使用foreach。最好的解决办法是:

$cookedArray = [];
foreach ($rawArray as $key => $value)
{
    $cookedArray [] = ["section" => $key, "quantity" => $value];
}

#3


1  

$previous_array=array(
    'key1' => 10,
    'key2' => 7,
    'key3' => 8,
);

$new_array=array();
foreach($previous_array as $key => $value):
    $new_array[]=array('section'=>$key,'quantity'=>$value);

endforeach;
Print_r($new_array);

Output:

输出:

Array
(
    [0] => Array
        (
            [section] => key1
            [quantity] => 10
        )

    [1] => Array
        (
            [section] => key2
            [quantity] => 7
        )

    [2] => Array
        (
            [section] => key3
            [quantity] => 8
        )

)

#4


1  

About by updated requirement, I myself have came to this solution:

关于更新的要求,我自己想到了这个解决方案:

$section = array_map(function ($key,$value) {
    return ['section' => $key, 'quantity' => $value];
}, array_keys($section), array_values($section));

The only concern is about keys and values. Now I am testing, won't it mess up with each other, I mean keys mix with values?

唯一关心的是键和值。现在我在测试,它会不会互相搞砸,我是说键和值混合?

Update
It worked fine. Thanks to the pointing of @Halcyon in comments. And all others who suggested solutions.

更新它工作得很好。感谢@Halcyon的评论。以及所有提出解决方案的人。

#1


2  

Try using array_walk as

尽可能使用array_walk

$result = array();
array_walk($arr, function ($v,$k) use(&$result) {
     $result[] = array('section' => $k, 'quantity' => $v);
});

demo using array_walk

演示使用array_walk

Using foreach

使用foreach

$result = array();
foreach($arr as $key => $value){
    $result[] = array('section' => $key, 'quantity' => $value);
}

print_r($result);

demo using foreach

演示使用foreach

#2


1  

There's no reason not to use foreach for this. The best solution would be:

没有理由不使用foreach。最好的解决办法是:

$cookedArray = [];
foreach ($rawArray as $key => $value)
{
    $cookedArray [] = ["section" => $key, "quantity" => $value];
}

#3


1  

$previous_array=array(
    'key1' => 10,
    'key2' => 7,
    'key3' => 8,
);

$new_array=array();
foreach($previous_array as $key => $value):
    $new_array[]=array('section'=>$key,'quantity'=>$value);

endforeach;
Print_r($new_array);

Output:

输出:

Array
(
    [0] => Array
        (
            [section] => key1
            [quantity] => 10
        )

    [1] => Array
        (
            [section] => key2
            [quantity] => 7
        )

    [2] => Array
        (
            [section] => key3
            [quantity] => 8
        )

)

#4


1  

About by updated requirement, I myself have came to this solution:

关于更新的要求,我自己想到了这个解决方案:

$section = array_map(function ($key,$value) {
    return ['section' => $key, 'quantity' => $value];
}, array_keys($section), array_values($section));

The only concern is about keys and values. Now I am testing, won't it mess up with each other, I mean keys mix with values?

唯一关心的是键和值。现在我在测试,它会不会互相搞砸,我是说键和值混合?

Update
It worked fine. Thanks to the pointing of @Halcyon in comments. And all others who suggested solutions.

更新它工作得很好。感谢@Halcyon的评论。以及所有提出解决方案的人。