doushaju4901 2016-07-14 21:29
浏览 43
已采纳

提取数组索引并合并其他数组PHP中的项

I have this array $all_designs_from_select_articles in PHP. Each index contains the objects Design:

array:7 [▼
  0 => array:12 [▼
    0 => Design {#297 ▶}
    1 => Design {#298 ▶}
    2 => Design {#299 ▶}
    3 => Design {#300 ▶}
    4 => Design {#301 ▶}
    5 => Design {#302 ▶}
    6 => Design {#303 ▶}
    7 => Design {#304 ▶}
    8 => Design {#305 ▶}
    9 => Design {#306 ▶}
    10 => Design {#307 ▶}
    11 => Design {#308 ▶}
  ]
  1 => array:15 [▼
    0 => Design {#309 ▶}
    1 => Design {#310 ▶}
    2 => Design {#311 ▶}
    3 => Design {#312 ▶}
    4 => Design {#313 ▶}
    5 => Design {#314 ▶}
    6 => Design {#315 ▶}
    7 => Design {#316 ▶}
    8 => Design {#317 ▶}
    9 => Design {#318 ▶}
    10 => Design {#319 ▶}
    11 => Design {#320 ▶}
    12 => Design {#321 ▶}
    13 => Design {#322 ▶}
    14 => Design {#323 ▶}
  ]

]

But I want an array with all Design keys combined, like this:

array:26 [▼

 0 => Design {#297 ▶}
 1 => Design {#298 ▶}
 2 => Design {#299 ▶}
 3 => Design {#300 ▶}
 4 => Design {#301 ▶}
 5 => Design {#302 ▶}
 6 => Design {#303 ▶}
 7 => Design {#304 ▶}
 8 => Design {#305 ▶}
 9 => Design {#306 ▶}
 10 => Design {#307 ▶}
 11 => Design {#308 ▶}

 12 => Design {#309 ▶}
 13 => Design {#310 ▶}
 14 => Design {#311 ▶}
 15 => Design {#312 ▶}
 16 => Design {#313 ▶}
 17 => Design {#314 ▶}
 18 => Design {#315 ▶}
 19 => Design {#316 ▶}
 20 => Design {#317 ▶}
 21 => Design {#318 ▶}
 22 => Design {#319 ▶}
 23 => Design {#320 ▶}
 24 => Design {#321 ▶}
 25 => Design {#322 ▶}
 26 => Design {#323 ▶}
 ...
 etc
]

And this is my query to my API and get the first array values:

foreach ($search_articles as $article) {

                $all_designs_from_select_articles[] = ApiDesign::getList([
                    'article_id' => $article,
                    'visibility' => 'ALL',
                    'order' => '-POPULARITY,NAME',
                    'lang' => 'multi,es',
                ]);
            }

Is there a function in PHP to do this? I´m using PHP7 and the framework laravel 5.2

  • 写回答

2条回答 默认 最新

  • dongluyi5123 2016-07-15 08:44
    关注

    You could merge the subarrays together after the fact with

    $all_designs_from_select_articles = array_reduce(
        $all_designs_from_select_articles, "array_merge", []);
    

    Or during the creation of the array:

    foreach ($search_articles as $article) {
        $all_designs_from_select_articles = array_merge(
            $all_designs_from_select_articles,
            ApiDesign::getList(...));
    }
    

    I think I'd prefer the latter approach, but it's a marginal preference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里