简体   繁体   中英

PHP check if array contains value from array with different key

I have this array:

$term = array();

For example:

$common_item = array('id' => 0, 'full_name' => 'my great name'); //etc
$first_item = array('name' => 'Robert', 'item' => $common_item);

$second_item = array('name' => 'Roberto', 'item' => $common_item);

$term[] = $first_item;
//check if terms already has an item with the same item(common_item) and don't add
$term[] = $second_item;

So both items in term have the same 'item' value. How can i check if term item already has the same item in terms array before adding?

Did you try with in_array() function?

 <?php
   $os = array("Mac", "NT", "Irix", "Linux");
    if (in_array("Irix", $os)) {
    echo "Got Irix";
    }
    if (in_array("mac", $os)) {
    echo "Got mac";
   }
 ?>

Source: https://www.php.net/manual/en/function.in-array.php

Edit: Possible duplicate ### in_array() and multidimensional array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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