关于in_array的问题

WBOY
Lepaskan: 2016-06-20 12:40:39
asal
817 orang telah melayarinya

$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);
Salin selepas log masuk

不好意思上面的json是我从后台生成的,我想请问in_array第一个参数是一维数组,第二个参数是空数据,我很困惑的问题,in_array的流程,想想论坛里的大神


回复讨论(解决方案)

第一个$json['id_server']他是归类到,$str相对于的键,也就是我的$json['id_server']他的值是20,$str['20'].

请说明你的问题

$str[$id_server]['extens'] 只在第一次 array_push($str[$id_server]['extens'],$value); 之前是空数组

in_array 用于检查 第一个参数 是否在 第二个 参数之中
你在 in_array 前有 ! (取反),就是 检查 第一个参数 不在在 第二个 参数之中 了

我就想知道in_array处理的过程,还有他的第一个参数是一维数组而不是字符串。

在补充一下,就是我的$json要分别,分类到我的$str['extens']里面,之前$json都是一维数组,然后foreach执行完后,打印出$str,就能看到$str的键有,22、18、20,、23、24

原型
bool in_array (  mixed needle, array haystack [, bool strict] )
mixed 标识 php 支持的所有数据类型, 当然包括数组

简化你的数据,并给出期望的结果

谢谢版主的解答,已经理解了

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan