How to find index of object in php array?

前端 未结 10 1906
时光说笑
时光说笑 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:36

    try this

    foreach($array AS $key=>$object){
       if($object['id'] == 4){
           $key_in_array = $key;
       }
    }
    
    // chop it from the original array
    array_slice($array, $key_in_array, 1);
    
    0 讨论(0)
  • 2021-02-13 19:37
    foreach( $arr as $k=>&$a) {
        if( $a['id'] == 4 )
            unset($arr[$k]);
    }
    
    0 讨论(0)
  • 2021-02-13 19:38

    In my case, this my array as $array
    I was confused about this problem of my project, but some answer here helped me.

    array(3) { 
               [0]=> float(-0.12459619130796) 
               [1]=> float(-0.64018439966448) 
               [2]=> float(0)
             }
    

    Then use if condition to stop looping

    foreach($array as $key => $val){
               if($key == 0){ //the key is 0
                   echo $key; //find the key
                   echo $val; //get the value
               }
            }
    
    0 讨论(0)
  • 2021-02-13 19:40

    Another way to achieve the result is to use array_filter.

    $array = array(
        (object)array('id' => 5),
        (object)array('id' => 4),
        (object)array('id' => 3)
    );
    $array = array_filter($array, function($item) {
        return $item->id != 4;
    });
    print_r($array);
    
    0 讨论(0)
提交回复
热议问题