检查PHP多维数组中是否存在数组值

时间:2022-10-28 16:15:13

I have the following multidimensional array:

我有以下多维数组:

Array ( [0] => Array 
         ( [id] => 1 
           [name] => Jonah 
           [points] => 27 )
        [1] => Array 
         ( [id] => 2 
           [name] => Mark 
           [points] => 34 )
      )

I'm currently using a foreach loop to extract the values from the array:

我目前正在使用foreach循环从数组中提取值:

foreach ($result as $key => $sub)
{
    ...
}

But I was wondering how do I see whether a value within the array already exists.

但是我想知道如何查看数组中的值是否已经存在。

So for example if I wanted to add another set to the array, but the id is 1 (so the person is Jonah) and their score is 5, can I add the 5 to the already created array value in id 0 instead of creating a new array value?

例如,如果我想向数组添加另一个集合,但是id是1(所以这个人是Jonah),而他们的分数是5,那么我是否可以将5添加到id 0中已经创建的数组值中,而不是创建一个新的数组值?

So after the loop has finished the array will look like this:

当循环结束后,数组会是这样的:

Array ( [0] => Array 
         ( [id] => 1 
           [name] => Jonah 
           [points] => 32 )
        [1] => Array 
         ( [id] => 2 
           [name] => Mark 
           [points] => 34 )
      )

3 个解决方案

#1


5  

What about looping over your array, checking for each item if it's id is the one you're looking for ?

对于数组的循环,如果它的id是你要找的那个,检查每一项呢?

$found = false;
foreach ($your_array as $key => $data) {
    if ($data['id'] == $the_id_youre_lloking_for) {
        // The item has been found => add the new points to the existing ones
        $data['points'] += $the_number_of_points;
        $found = true;
        break; // no need to loop anymore, as we have found the item => exit the loop
    }
}

if ($found === false) {
    // The id you were looking for has not been found, 
    // which means the corresponding item is not already present in your array
    // => Add a new item to the array
}

#2


1  

you can first store the array with index equal to the id. for example :

您可以首先存储索引等于id的数组。

 $arr =Array ( [0] => Array 
     ( [id] => 1 
       [name] => Jonah 
       [points] => 27 )
    [1] => Array 
     ( [id] => 2 
       [name] => Mark 
       [points] => 34 )
  );
 $new = array();
 foreach($arr as $value){
    $new[$value['id']] = $value; 
 }

//So now you can check the array $new for if the key exists already 
if(array_key_exists(1, $new)){
    $new[1]['points'] = 32;
}

#3


0  

Even though the question is answered, I wanted to post my answer. Might come handy to future viewers. You can create new array from this array with filter then from there you can check if value exist on that array or not. You can follow below code. Sample

尽管这个问题得到了回答,我还是想发表我的答案。可能对未来的观众有用。你可以用过滤器从这个数组中创建新的数组,然后你可以检查这个数组上是否存在值。您可以遵循下面的代码。样本

$arr = array(
        0 =>array(
                "id"=> 1,
                "name"=> "Bangladesh",
                "action"=> "27"
            ),
         1 =>array(
                "id"=> 2,
                "name"=> "Entertainment",
                "action"=> "34"
                 )
        );

     $new = array();
     foreach($arr as $value){
        $new[$value['id']] = $value; 
     }


    if(array_key_exists(1, $new)){
        echo $new[1]['id'];
    }
    else {
      echo "aaa";
    }
    //print_r($new);

#1


5  

What about looping over your array, checking for each item if it's id is the one you're looking for ?

对于数组的循环,如果它的id是你要找的那个,检查每一项呢?

$found = false;
foreach ($your_array as $key => $data) {
    if ($data['id'] == $the_id_youre_lloking_for) {
        // The item has been found => add the new points to the existing ones
        $data['points'] += $the_number_of_points;
        $found = true;
        break; // no need to loop anymore, as we have found the item => exit the loop
    }
}

if ($found === false) {
    // The id you were looking for has not been found, 
    // which means the corresponding item is not already present in your array
    // => Add a new item to the array
}

#2


1  

you can first store the array with index equal to the id. for example :

您可以首先存储索引等于id的数组。

 $arr =Array ( [0] => Array 
     ( [id] => 1 
       [name] => Jonah 
       [points] => 27 )
    [1] => Array 
     ( [id] => 2 
       [name] => Mark 
       [points] => 34 )
  );
 $new = array();
 foreach($arr as $value){
    $new[$value['id']] = $value; 
 }

//So now you can check the array $new for if the key exists already 
if(array_key_exists(1, $new)){
    $new[1]['points'] = 32;
}

#3


0  

Even though the question is answered, I wanted to post my answer. Might come handy to future viewers. You can create new array from this array with filter then from there you can check if value exist on that array or not. You can follow below code. Sample

尽管这个问题得到了回答,我还是想发表我的答案。可能对未来的观众有用。你可以用过滤器从这个数组中创建新的数组,然后你可以检查这个数组上是否存在值。您可以遵循下面的代码。样本

$arr = array(
        0 =>array(
                "id"=> 1,
                "name"=> "Bangladesh",
                "action"=> "27"
            ),
         1 =>array(
                "id"=> 2,
                "name"=> "Entertainment",
                "action"=> "34"
                 )
        );

     $new = array();
     foreach($arr as $value){
        $new[$value['id']] = $value; 
     }


    if(array_key_exists(1, $new)){
        echo $new[1]['id'];
    }
    else {
      echo "aaa";
    }
    //print_r($new);