猿问

根据顶层(共享值)合并第二层数组

我想合并数组的顶层,以便共享同一父级的第二级的所有值形成该父级的单个数组。


数组 1:


Array

(

    [0] => Array

        (

            [0] => C1

            [1] => C1

            [2] => C1

            [3] => AC

            [4] => AC

        )


)

数组 2:


Array

(

    [0] => Array

        (

            [0] => C1_complete

            [1] => C1_29-95

            [2] => C1_49-95

            [3] => AC_complete

            [4] => AC_29-95

        )


)

想要的结果:


Array

(

    [0] => Array

        (

            'C1' => Array (C1_complete, C1_29-95, C1_49-95),

            'AC' => Array (AC_complete, AC_29-95)

        )


)

实现这一目标的简单而优雅的方法是什么?


我寻求一种可以将相同原理推广到任何两级数组结构的解决方案。


跃然一笑
浏览 152回答 3
3回答

翻阅古今

如果我只使用数组 2 和一个简单的foreach循环,我可以这样做:$array2 = [['C1_complete',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1_29-95',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1_49-95',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC_complete',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC_29-95']];$buffer = [];foreach ($array2[0] as $value) {&nbsp; &nbsp; $buffer[substr($value, 0, 2)][] = $value;}$result[] = $buffer;echo '<pre>';print_r($result);echo '</pre>';输出是:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [C1] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => C1_complete&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => C1_29-95&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => C1_49-95&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [AC] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => AC_complete&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => AC_29-95&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))如果该代码在函数中,那就太好了:function classify($array){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $result = [];&nbsp; &nbsp; foreach ($array[0] as $value) {&nbsp; &nbsp; &nbsp; &nbsp; $result[substr($value, 0, 2)][] = $value;&nbsp; &nbsp; }&nbsp; &nbsp; return [$result];}&nbsp; &nbsp;&nbsp;$array2 = [['C1_complete',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1_29-95',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1_49-95',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC_complete',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC_29-95']];echo '<pre>';print_r(classify($array2));echo '</pre>';输出保持不变。

森林海

对于您的数组,您不需要第一个循环,但如果您的数组中有多个数组。只需循环和 grep 以键开头的项目:foreach($array1 as $key => $inner) {&nbsp; &nbsp; foreach($inner as $k) {&nbsp; &nbsp; &nbsp; &nbsp; $result[$key][$k] = preg_grep("/^$k/", $array2[0]);&nbsp; &nbsp; }}使用给定的数组:foreach($array1[0] as $k) {&nbsp; &nbsp; $result[0][$k] = preg_grep("/^$k/", $array2[0]);}

慕斯709654

那么,这两个simple和elegant的方式在这种情况下,因为这可能是基于舆论似乎不大可能给我。一个简单的 foreach 是实现这一目标的最简单有效的方法。但是,为了优雅,您可以使用array_walk、array_fill和array_combine来实现这一点。<?php$arr1 = Array(&nbsp; &nbsp; Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC'&nbsp; &nbsp; ));$arr2 = Array(&nbsp; &nbsp; Array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1_complete',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1_29-95',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'C1_49-95',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC_complete',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'AC_29-95'&nbsp; &nbsp; ));$data = array_combine(array_values($arr1[0]),array_fill(0,count($arr1[0]),[]));array_walk($arr2[0],function($value) use (&$data){&nbsp; &nbsp; if(isset($data[explode("_",$value)[0]])) $data[explode("_",$value)[0]][] = $value;});$data = [$data]; // since you want it inside another array.print_r($data);演示: https : //3v4l.org/VhFLH7
随时随地看视频慕课网APP
我要回答