无法使用PHP检查json键值是否不存在

时间:2022-10-26 16:55:40

I need some help.I need to check weather the key value is present inside the json array or not using PHP but its throwing some warning message and no value is coming. I am explaining my code below.

我需要一些帮助。我需要检查天气,关键值存在于json数组中或不使用PHP,但它抛出一些警告消息,没有任何值。我在下面解释我的代码。

$mainArr=array(array("type"=>1,"name"=>"hello"),array("type"=>1,"name"=>"hii"));
//echo json_encode($mainArr);
foreach ($mainArr as $v) {
    if(!in_array(1, $v['type'])){
        $result['primary'][]=array();
    }else{
        $result['primary'][]=$v;
    }
    if(!in_array(2, $v['type'])){
        $result['secondary'][]=array();
    }else{
        $result['secondary'][]=$v;
    }
}
echo json_encode($result);  

Here I need to check if type==2 is not present inside that array it should return blank array but its throwing the below message.

在这里,我需要检查在该数组中是否存在type == 2,它应该返回空白数组,但它抛出以下消息。

Warning: in_array() expects parameter 2 to be array, integer given in /opt/lampp/htdocs/test/arrchk.php on line 5

Warning: in_array() expects parameter 2 to be array, integer given in /opt/lampp/htdocs/test/arrchk.php on line 10

Warning: in_array() expects parameter 2 to be array, integer given in /opt/lampp/htdocs/test/arrchk.php on line 5

Warning: in_array() expects parameter 2 to be array, integer given in /opt/lampp/htdocs/test/arrchk.php on line 10
{"primary":[[],[]],"secondary":[[],[]]}

Please help me to resolve this issue.

请帮我解决这个问题。

4 个解决方案

#1


1  

You are using the PHP's in_array function on a scalar value.

您在标量值上使用PHP的in_array函数。

$v['type'] is an integer, a simple comparaison like

$ v ['type']是一个整数,一个简单的比较

1 === $v['type']

will do the job

会做的

#2


1  

You search element in an array element not an array.

您搜索数组元素中的元素而不是数组。

<?php
$mainArr = array(
    array(
        "type" => 1,
        "name" => "hello"
    ),
    array(
        "type" => 1,
        "name" => "hii"
    )
);
//echo json_encode($mainArr);
foreach ($mainArr as $v) {
    if (!in_array(1, $v)) {
        $result['primary'][] = array();
    } else {
        $result['primary'][] = $v;
    }
    if (!in_array(2, $v)) {
        $result['secondary'][] = array();
    } else {
        $result['secondary'][] = $v;
    }
}
echo json_encode($result);

?>

#3


0  

$mainArr=array(array("type"=>1,"name"=>"hello"),array("type"=>1,"name"=>"hii"));
//echo json_encode($mainArr);
foreach ($mainArr as $v => $values) {
    if(!in_array(1, $values['type'])){
        $result['primary'][]=array();
}else{
    $result['primary'][]=$values;
}
if(!in_array(2, $values['type'])){
    $result['secondary'][]=array();
}else{
    $result['secondary'][]=$values;
}
}
echo json_encode($result);

#4


0  

Use array_search() function. For example:

使用array_search()函数。例如:

foreach ($mainArr as $v) {
    if (!array_search('1', $v)) {
        $result['primary'][]=array();
    } else {...}

#1


1  

You are using the PHP's in_array function on a scalar value.

您在标量值上使用PHP的in_array函数。

$v['type'] is an integer, a simple comparaison like

$ v ['type']是一个整数,一个简单的比较

1 === $v['type']

will do the job

会做的

#2


1  

You search element in an array element not an array.

您搜索数组元素中的元素而不是数组。

<?php
$mainArr = array(
    array(
        "type" => 1,
        "name" => "hello"
    ),
    array(
        "type" => 1,
        "name" => "hii"
    )
);
//echo json_encode($mainArr);
foreach ($mainArr as $v) {
    if (!in_array(1, $v)) {
        $result['primary'][] = array();
    } else {
        $result['primary'][] = $v;
    }
    if (!in_array(2, $v)) {
        $result['secondary'][] = array();
    } else {
        $result['secondary'][] = $v;
    }
}
echo json_encode($result);

?>

#3


0  

$mainArr=array(array("type"=>1,"name"=>"hello"),array("type"=>1,"name"=>"hii"));
//echo json_encode($mainArr);
foreach ($mainArr as $v => $values) {
    if(!in_array(1, $values['type'])){
        $result['primary'][]=array();
}else{
    $result['primary'][]=$values;
}
if(!in_array(2, $values['type'])){
    $result['secondary'][]=array();
}else{
    $result['secondary'][]=$values;
}
}
echo json_encode($result);

#4


0  

Use array_search() function. For example:

使用array_search()函数。例如:

foreach ($mainArr as $v) {
    if (!array_search('1', $v)) {
        $result['primary'][]=array();
    } else {...}