Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于in_array的问题

关于in_array的问题

Jun 20, 2016 pm 12:40 PM

$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);
Copy after login

不好意思上面的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 支持的所有数据类型, 当然包括数组

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

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

HTTP Method Verification in Laravel HTTP Method Verification in Laravel Mar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

See all articles