How to find index of object in php array?

前端 未结 10 1904
时光说笑
时光说笑 2021-02-13 18:46

Here is print_r output of my array:

Array
(
[0] => stdClass Object
    (
        [itemId] => 560639000019
        [name] => Item no1
        [code] =>         


        
相关标签:
10条回答
  • 2021-02-13 19:14

    Here's my solution. Given, it is a bit hackish, but it will get the job done.

    search(array $items, mixed $id[, &$key]);

    Returns the item that was found by $id. If you add the variable $key it will give you the key of the item found.

    function search($items, $id, &$key = null) {
      foreach( $items as $item ) {
        if( $item->id == $id ) {
          $key = key($item);
          return $item;
          break;
        }
      }
    
      return null;
    }
    

    Usage

    $item = search($items, 4, $key);
    unset($items[$key]);
    

    Note: This could be modified to allow a custom key and return multiple items that share the same value.

    I've created an example so you can see it in action.

    0 讨论(0)
  • 2021-02-13 19:16

    A funny alternative

    $getIdUnset = function($id) use ($myArray)
    {
        foreach($myArray as $key => $obj) {
            if ($obj->id == $id) {
                return $key;
            }
        }
    
        return false;
    };
    
    if ($unset = $getIdUnset(4)) {
        unset($myArray[$unset]);
    }
    
    0 讨论(0)
  • 2021-02-13 19:25

    use array_search:

    $a = new stdClass;
    $b = new stdClass;
    $a->id = 1;
    $b->id = 2;
    
    $arr = array($a, $b);
    $index = array_search($b, $arr);
    
    echo $index;
    // prints out 1
    
    0 讨论(0)
  • 2021-02-13 19:26
    $found = false;  
    foreach($values as $key => $value) {
        if ($value->id == 4) {
            $found = true;
            break;
        }
    }
    
    if ($found) unset($values[$key]);
    

    This is considered to be faster then any other solution since we only iterate the array to until we find the object we want to remove.

    Note: You should not remove an element of an array while iterating so we do it afterwards here.

    0 讨论(0)
  • 2021-02-13 19:32

    Currently php does not have any supported function for this yet.

    So refer to Java's Vector, or jQuery's $.inArray(), it would simply be:

    public function indexOf($object, array $elementData) {
        $elementCount = count($elementData);
        for ($i = 0 ; $i < $elementCount ; $i++){
            if ($object == $elementData[$i]) {
                return $i;   
            }
        }
        return -1;
    }
    

    You can save this function as a core function for later.

    0 讨论(0)
  • 2021-02-13 19:34
    foreach($parentObj AS $key=>$element){
      if ($element->id == THE_ID_YOU_ARE_LOOKING_FOR){
        echo "Gottcha! The index is - ". $key;
      }
    }
    

    $parentObj is obviously your root array - the one that holds all the others.

    We use the foreach loop to iterate over each item and then test it's id property against what ever value you desire. Once we have that - the $key that we are on is the index you are looking for.

    0 讨论(0)
提交回复
热议问题