Question about in_array
$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);
Sorry, the above json is generated from the background. I would like to ask whether the first parameter of in_array is a one-dimensional array and the second parameter is null data. I am very confused about the in_array process, think about the great gods in the forum
Reply to discussion (solution)
The first $ json ['id _ server'] is the key that is classified to $ str, that is, my $ json ['id _ server'] has a value of 20, $ str ['20'].
Please describe your problem
$ Str [$ id_server] ['extens'] is an empty array only in the first array_push ($ str [$ id_server] ['extens'], $ value );
In_array is used to check whether the first parameter is in the second parameter.
You have it before in_array! (Inverse), that is, check that the first parameter is not included in the second parameter.
I want to know the in_array processing process, and its first parameter is a one-dimensional array rather than a string.
In addition, I want to classify my $ json into my $ str ['extens']. Previously, $ json is a one-dimensional array, and after foreach is executed, print $ str and you can see that the keys of $ str are 22, 18, 20, 23, and 24.
Prototype
Bool in_array (mixed needle, array haystack [, bool strict])
Mixed identifies all data types supported by php, including arrays
Simplify your data and give expected results
Thank you for your answers.