duanao2585 2018-10-27 11:30
浏览 28

没有循环的数组结构重排

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)
  • 写回答

1条回答 默认 最新

  • dplase3140 2018-10-27 11:43
    关注

    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.

    in this case, you can use 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');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探