简体   繁体   English

无循环的数组结构重排

[英]Array structure rearrangement without loop

I have an array like mentioned below, which I want to rearrange without using loop:我有一个如下所述的数组,我想在不使用循环的情况下重新排列它:

Array
(
    [0] => Array
        (
            [Books] => Array
                (
                    [id] => 4
                )

        )

    [1] => Array
        (
            [Books] => Array
                (
                    [id] => 3
                )

        )

    [2] => Array
        (
            [Books] => Array
                (
                    [id] => 2
                )

        )

    [3] => Array
        (
            [Books] => Array
                (
                    [id] => 1
                )

        )

)

I want an output like this:我想要这样的输出:

Array(4,3,2,1)

I'm assuming you do not want to use for or foreach loops, but anything else that internally is or uses a loop is fine.我假设您不想使用 for 或 foreach 循环,但任何其他内部是或使用循环的东西都可以。

in this case, you can use array_map:在这种情况下,您可以使用 array_map:

$result = array_map(function($item){
  return $item['books']['id'];
}, $currentArray);

OR或者

if you do not even want that:如果你甚至不想这样:

$v1 = array_column($input, 'books');
$result = array_column($v1, 'id');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM