当元素属于相同的 id 索引时,PHP 将数组数据结构转换为另一种数据结构?

我需要将 $array1 的数据结构转换为 $array2。我如何将属于相同 uid 和 title 的城市聚集在一起?


$array1 = [array("uid" => "29",

                 "title" => "Developer",

                 "city" => "Madrid"),

           array("uid" => "29",

                 "title" => "Developer",

                 "city" => "Berlin"),

           array("uid" => "29",

                 "title" => "Developer",

                 "city" => "London"),

           array("uid" => "30",

                 "title" => "Designer",

                 "city" => "Amsterdam"),

           ];


$array2 = [array("title":"Developer",

                 "city" : ["Madrid","Berlin","London"]),

           array("title":"Designer",

                 "city" : ["Amsterdam"])

          ];


慕码人2483693
浏览 185回答 2
2回答

Qyouu

&nbsp; &nbsp; $src = array(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "uid"&nbsp; &nbsp;=> "29",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title" => "Developer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city"&nbsp; => "Madrid"&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "uid"&nbsp; &nbsp;=> "29",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title" => "Developer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city"&nbsp; => "Berlin"&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "uid"&nbsp; &nbsp;=> "29",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title" => "Developer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city"&nbsp; => "London"&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "uid"&nbsp; &nbsp;=> "30",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title" => "Designer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city"&nbsp; => "Amsterdam"&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; );&nbsp; &nbsp; /* placeholder for storing final array */&nbsp; &nbsp; $out=array();&nbsp; &nbsp; /* iterate through source array */&nbsp; &nbsp; foreach( $src as $arr ){&nbsp; &nbsp; &nbsp; &nbsp; /* for convenience, cast child array as object */&nbsp; &nbsp; &nbsp; &nbsp; $obj=(object)$arr;&nbsp; &nbsp; &nbsp; &nbsp; $key=$obj->uid;&nbsp; &nbsp; &nbsp; &nbsp; /* construct outpur array format & add values from child arrays/objects */&nbsp; &nbsp; &nbsp; &nbsp; if( !array_key_exists( $key, $out ) )$out[ $key ]=array();&nbsp; &nbsp; &nbsp; &nbsp; if( !array_key_exists( 'title', $out[ $key ] ) )$out[ $key ]['title']=$obj->title;&nbsp; &nbsp; &nbsp; &nbsp; if( !array_key_exists( 'city', $out[ $key ] ) ) $out[ $key ]['city']=array();&nbsp; &nbsp; &nbsp; &nbsp; $out[ $key ]['city'][]=$obj->city;&nbsp; &nbsp; }&nbsp; &nbsp; /* this produces an output array keyed by uid */&nbsp; &nbsp; printf('<pre>%s</pre>',print_r($out,true));&nbsp; &nbsp; /* If the uid keys are not important and should not appear in output */&nbsp; &nbsp; $final=array();&nbsp; &nbsp; foreach( $out as $uid => $child )$final[]=$child;&nbsp; &nbsp; printf('<pre>%s</pre>',print_r($final,true));其输出为:Array(&nbsp; &nbsp; [29] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [title] => Developer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [city] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Madrid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Berlin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => London&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [30] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [title] => Designer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [city] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Amsterdam&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [title] => Developer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [city] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Madrid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Berlin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => London&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [title] => Designer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [city] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Amsterdam&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))

慕田峪7331174

这可能是您正在寻找的内容:<?php$input = [&nbsp;&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; "uid" => "29",&nbsp; &nbsp; &nbsp; &nbsp; "title" => "Developer",&nbsp; &nbsp; &nbsp; &nbsp; "city" => "Madrid"&nbsp; &nbsp; ],&nbsp;&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; "uid" => "29",&nbsp; &nbsp; &nbsp; &nbsp; "title" => "Developer",&nbsp; &nbsp; &nbsp; &nbsp; "city" => "Berlin"&nbsp; &nbsp; ],&nbsp;&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; "uid" => "29",&nbsp; &nbsp; &nbsp; &nbsp; "title" => "Developer",&nbsp; &nbsp; &nbsp; &nbsp; "city" => "London"&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; "uid" => "30",&nbsp; &nbsp; &nbsp; &nbsp; "title" => "Designer",&nbsp; &nbsp; &nbsp; &nbsp; "city" => "Amsterdam"&nbsp; &nbsp; ]];$output = [];array_walk($input, function($entry) use (&$output) {&nbsp; &nbsp; if (!array_key_exists($entry["uid"], $output)) {&nbsp; &nbsp; &nbsp; &nbsp; $output[$entry["uid"]] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "title" => $entry["title"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "city" => [$entry["city"]]&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $output[$entry["uid"]]["city"][] = $entry["city"];&nbsp; &nbsp; }});var_dump($output);输出显然是:array(2) {&nbsp; [29]=>&nbsp; array(2) {&nbsp; &nbsp; ["title"]=>&nbsp; &nbsp; string(9) "Developer"&nbsp; &nbsp; ["city"]=>&nbsp; &nbsp; array(3) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; string(6) "Madrid"&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; string(6) "Berlin"&nbsp; &nbsp; &nbsp; [2]=>&nbsp; &nbsp; &nbsp; string(6) "London"&nbsp; &nbsp; }&nbsp; }&nbsp; [30]=>&nbsp; array(2) {&nbsp; &nbsp; ["title"]=>&nbsp; &nbsp; string(8) "Designer"&nbsp; &nbsp; ["city"]=>&nbsp; &nbsp; array(1) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; string(9) "Amsterdam"&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP