Slicing a multi-dimensional PHP array across one of its elements

后端 未结 10 910
再見小時候
再見小時候 2020-12-28 15:51

Say for example you just queried a database and you recieved this 2D array.

$results = array(
    array(\'id\' => 1, \'name\' => \'red\'  , \'spin\' =&         


        
10条回答
  •  时光取名叫无心
    2020-12-28 16:51

    I think this will do what you want

    array_uintersect_uassoc

    You would have to do something like this

    $results = array(
        array('id' => 1, 'name' => 'red'  , 'spin' =>  1),
        array('id' => 2, 'name' => 'green', 'spin' => -1),
        array('id' => 3, 'name' => 'blue' , 'spin' => .5)
    );
    $name = array_uintersect_uassoc( $results, array('name' => 'value')  , 0, "cmpKey");
    print_r($name);
    
    //////////////////////////////////////////////////
    // FUNCTIONS
    //////////////////////////////////////////////////
    function cmpKey($key1, $key2) {
      if ($key1 == $key2) {
        return 0;
      } else {
        return -1;
      }
    }
    

    However, I don't have access to PHP5 so I haven't tested this.

提交回复
热议问题