$json = '[{"id_server":"20","exten":"2607","name":null},{"id_server":"20","exten":"2608","name":null},{"id_server":"20","exten":"2611","name":null},{"id_server":"20","exten":"2620","name":null},{"id_server":"20","exten":"2639","name":null},{"id_server":"20","exten":"2640","name":null},{"id_server":"20","exten":"2684","name":null},{"id_server":"20","exten":"2687","name":null},{"id_server":"20","exten":"2693","name":null},{"id_server":"20","exten":"2700","name":null},{"id_server":"20","exten":"2712","name":null},{"id_server":"20","exten":"2714","name":null},{"id_server":"20","exten":"2716","name":null},{"id_server":"20","exten":"2717","name":null},{"id_server":"20","exten":"2720","name":null},{"id_server":"20","exten":"2721","name":null},{"id_server":"20","exten":"2722","name":null},{"id_server":"20","exten":"2731","name":"Candy"},{"id_server":"20","exten":"2788","name":null},{"id_server":"20","exten":"2789","name":null},{"id_server":"20","exten":"2790","name":null},{"id_server":"20","exten":"2791","name":"Jan"},{"id_server":"18","exten":"2800","name":"Crystal Wong"},{"id_server":"18","exten":"2801","name":"alex"},{"id_server":"18","exten":"2802","name":"Vki"},{"id_server":"18","exten":"2803","name":"Ling Cheung"},{"id_server":"18","exten":"2804","name":"Martin chen"},{"id_server":"18","exten":"2805","name":"Shawn"},{"id_server":"18","exten":"2806","name":"Roger"},{"id_server":"18","exten":"2807","name":"Gary"},{"id_server":"18","exten":"2808","name":"Jerry"},{"id_server":"18","exten":"2809","name":"Andy"},{"id_server":"18","exten":"2810","name":"Cary"},{"id_server":"18","exten":"2811","name":"Andrew"},{"id_server":"18","exten":"2812","name":"Chan chan"},{"id_server":"18","exten":"2812","name":"Michael"},{"id_server":"18","exten":"2813","name":"one"},{"id_server":"18","exten":"2813","name":"Anna"},{"id_server":"18","exten":"2814","name":"Carry"},{"id_server":"18","exten":"2815","name":"Lisa"},{"id_server":"18","exten":"2816","name":"Ronnie"},{"id_server":"18","exten":"2817","name":"Jerman"},{"id_server":"18","exten":"2818","name":"Crystal Yip"},{"id_server":"18","exten":"2819","name":"Emma Wong"},{"id_server":"18","exten":"2820","name":"Cindy Luk"},{"id_server":"18","exten":"2821","name":null},{"id_server":"18","exten":"2822","name":null},{"id_server":"18","exten":"2823","name":"Joe"},{"id_server":"18","exten":"2824","name":null},{"id_server":"18","exten":"2825","name":"Gina"},{"id_server":"18","exten":"2826","name":"Seven"},{"id_server":"18","exten":"2827","name":"Jay.wong"},{"id_server":"18","exten":"2828","name":"Amber"},{"id_server":"18","exten":"2829","name":null},{"id_server":"18","exten":"2830","name":null},{"id_server":"18","exten":"2831","name":null},{"id_server":"18","exten":"2832","name":null},{"id_server":"18","exten":"2833","name":null},{"id_server":"18","exten":"2834","name":null},{"id_server":"18","exten":"2835","name":null},{"id_server":"18","exten":"2836","name":null},{"id_server":"18","exten":"2837","name":null},{"id_server":"18","exten":"2838","name":null},{"id_server":"18","exten":"2839","name":null},{"id_server":"20","exten":"2840","name":null},{"id_server":"18","exten":"2841","name":null},{"id_server":"18","exten":"2842","name":null},{"id_server":"18","exten":"2843","name":null},{"id_server":"18","exten":"2844","name":null},{"id_server":"18","exten":"2845","name":null},{"id_server":"18","exten":"2846","name":null},{"id_server":"18","exten":"2847","name":null},{"id_server":"18","exten":"2848","name":"Cloris"},{"id_server":"18","exten":"2849","name":null},{"id_server":"18","exten":"2850","name":"Chary Tsui"},{"id_server":"18","exten":"2851","name":null},{"id_server":"18","exten":"2852","name":null},{"id_server":"18","exten":"2853","name":null},{"id_server":"18","exten":"2854","name":"Carl"},{"id_server":"18","exten":"2855","name":"Ace Fan"},{"id_server":"18","exten":"2856","name":"Steven"},{"id_server":"18","exten":"2857","name":null},{"id_server":"18","exten":"2858","name":null},{"id_server":"18","exten":"2859","name":null},{"id_server":"18","exten":"2860","name":"Sunny"},{"id_server":"20","exten":"2876","name":null},{"id_server":"20","exten":"2881","name":null},{"id_server":"20","exten":"2882","name":null},{"id_server":"20","exten":"2883","name":null},{"id_server":"20","exten":"2884 ","name":null},{"id_server":"20","exten":"2886","name":null},{"id_server":"20","exten":"2887","name":null},{"id_server":"20","exten":"2889","name":null},{"id_server":"20","exten":"2890","name":null},{"id_server":"18","exten":"2900","name":"Ricky Chan"},{"id_server":"20","exten":"2905","name":null},{"id_server":"20","exten":"2908","name":null},{"id_server":"20","exten":"2909","name":null},{"id_server":"20","exten":"2910","name":"Denny Lam"},{"id_server":"20","exten":"2913","name":null},{"id_server":"20","exten":"2920","name":"admin"},{"id_server":"20","exten":"2929","name":null},{"id_server":"18","exten":"2947","name":null},{"id_server":"18","exten":"2991","name":"Seven"},{"id_server":"24","exten":"3001","name":null},{"id_server":"24","exten":"3002","name":null},{"id_server":"24","exten":"3003","name":null},{"id_server":"24","exten":"3004","name":null},{"id_server":"24","exten":"3005","name":null},{"id_server":"24","exten":"3006","name":null},{"id_server":"24","exten":"3007","name":null},{"id_server":"24","exten":"3008","name":null},{"id_server":"24","exten":"3009","name":null},{"id_server":"24","exten":"3010","name":null},{"id_server":"24","exten":"3012","name":null},{"id_server":"24","exten":"3013","name":null},{"id_server":"24","exten":"3014","name":null},{"id_server":"24","exten":"3015","name":null},{"id_server":"24","exten":"3016","name":null},{"id_server":"24","exten":"3017","name":null},{"id_server":"24","exten":"3018","name":null},{"id_server":"24","exten":"3019","name":null},{"id_server":"24","exten":"3020","name":null},{"id_server":"18","exten":"8884","name":null},{"id_server":"18","exten":"8885","name":"Frankie Chu"},{"id_server":"18","exten":"8886","name":"Port"},{"id_server":"18","exten":"8887","name":"jack"},{"id_server":"22","exten":"9001","name":null},{"id_server":"22","exten":"9002","name":null},{"id_server":"22","exten":"9003","name":null},{"id_server":"22","exten":"9004","name":"\u6d77\u665624"},{"id_server":"22","exten":"9005","name":null},{"id_server":"22","exten":"9006","name":null},{"id_server":"22","exten":"9008","name":"Andy.wong"},{"id_server":"18","exten":"9009","name":"andy.wong"}]'; $str = '{"22":{"id":"22","ip":"192.168.80.108","ami_user":"phpagi","ami_password":"phpagi","mysql_user":"peter","mysql_password":"eLaStIx.2oo7","extens":[]},"18":{"id":"18","ip":"192.168.80.109","ami_user":"phpagi","ami_password":"phpagi","mysql_user":"phper","mysql_password":"321456","extens":[]},"20":{"id":"20","ip":"192.168.80.100","ami_user":"phpagi","ami_password":"phpagi","mysql_user":"peter","mysql_password":"eLaStIx.2oo7","extens":[]},"23":{"id":"23","ip":"192.168.80.187","ami_user":"phpagi","ami_password":"phpagi","mysql_user":"peter","mysql_password":"eLaStIx.2oo7","extens":[]},"24":{"id":"24","ip":"192.168.80.188","ami_user":"phpagi","ami_password":"phpagi","mysql_user":"peter","mysql_password":"eLaStIx.2oo7","extens":[]}}'; $str = json_decode($str, true); $json = json_decode($json, true); foreach ($json as $key => $value) { $id_server = $value['id_server']; if(!in_array($value, $str[$id_server]['extens'])){ array_push($str[$id_server]['extens'],$value); } }print_r($str);
最初の $json['id_server'] が分類されるキー、$str が関連するキー、つまり私の $json['id_server'] が持つキー値は 20、$str[' 20']。
問題を説明してください
$str[$id_server]['extens'] は初回のみ array_push($str[ $id_server]['extens'] ,$value); 以前は空の配列でした
in_array は最初のパラメーターが 2 番目のパラメーターの中にあるかどうかを確認するために使用されます
in_array の前に ! (反転) があります、最初のパラメータを確認することです パラメータはもう2番目のパラメータにはありません
in_array処理のプロセスと、その最初のパラメータが ではなく1次元配列であることを知りたいだけです文字列。
追加するには、$json を分離して $str['extens'] に分類する必要があります。以前は、$json は 1 次元配列であり、foreach の実行後に $str が出力されます。 out. out を見ると、 $str のキーは 22, 18, 20,, 23, 24 であることがわかります。
プロトタイプ
bool in_array (混合針, 配列 haystack [, bool strict] )
混合ロゴ もちろん配列も含め、php でサポートされているすべてのデータ型
データを単純化し、望ましい結果を得る
モデレーターの回答ありがとうございます。理解しました