简体   繁体   English

比较数组中的数据 - PHP

[英]Comparing data in Array - PHP

I want to make a comparison from the data that comes with JSON.我想从JSON附带的数据中进行比较。 That's what I wrote for it.这就是我为它写的。

$packages[0][0] = "5678"; // on the way
$packages[0][1] = "3098"; // checkpoint
$packages[0][2] = "4331"; // accept

if (array_key_exists($packages[0][2], $data[6])) {
    $packageID= 1;
} else if (array_key_exists($packages[0][1], $data[6]) == true) {
    $packageID= 5; 
} else if (array_key_exists($packages[0][0], $data[6]) == true) {
    $packageID= 10; 
} else {
    $packageID=0;
}

For example JSON:例如 JSON:

* Array
    (
        [0] => xxx
        [1] => yyy
        [2] => xxx
        [3] => yyy
        [4] => xxx
        [5] => yyy
        [6] => Array
            (
                [0] => 1234
                [1] => 5678
                [2] => 9012 
            )

    )

PackageID always comes out to 0 . PackageID始终为0

What could be the reason?可能是什么原因?

You're using array_key_exists() , but you should be using in_array() .您正在使用array_key_exists() ,但您应该使用in_array() The values you're searching are elements, not keys.您正在搜索的值是元素,而不是键。

$packages[0][0] = "5678"; // on the way
$packages[0][1] = "3098"; // checkpoint
$packages[0][2] = "4331"; // accept

if (in_array($packages[0][2], $data[6])) {
    $packageID= 1;
} else if (in_array($packages[0][1], $data[6]) == true) {
    $packageID= 5; 
} else if (in_array($packages[0][0], $data[6]) == true) {
    $packageID= 10; 
} else {
    $packageID=0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM