Php Array of arrays get id from name

前端 未结 2 1851
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 21:00

I have an array like the following in PHP:

$my_array = array();
$my_array[] = array(\"id\"=>\"myid1\", \"name\"=>\"myname1\");
$my_array[] = array(\"id\"=&         


        
相关标签:
2条回答
  • 2021-01-27 21:26

    you can get your array column name by array_column method.Then find you want string by array_search its will return the index of array.

    $index = array_search("othername",array_column($my_array,"name"));
    var_dump($my_array[$index]["id"]);
    
    0 讨论(0)
  • 2021-01-27 21:38

    Please use

    $id = array_filter( 
            array_map(
                function($array) {
                    if($array['name']== 'othername'){
                        return $array['id']; 
                    }
                },$my_array
            )
        ); 
    print "<pre>";print_r($id);
    
    0 讨论(0)
提交回复
热议问题