fetch data primary key value as the index for the associative array

前端 未结 2 939
别跟我提以往
别跟我提以往 2021-01-20 04:17

After executing a fetch query I get a result array:

[row_choice] => Array
  (
    [0] => Array
      (
        [id] => 277410
        [text_value] =         


        
2条回答
  •  [愿得一人]
    2021-01-20 05:01

    It isn't possible directly from the SQL query: but you can retrieve all your data, then re-map the array

    Using PHP 5.5's array_column() you can do something like:

    $myarray['row_choice'] = array_combine(
        array_column($myarray['row_choice'], 'id'),
        $myarray['row_choice']
    );
    

    else for earlier versions of PHP, use array_map() instead

    $myarray['row_choice'] = array_combine(
        array_map(
            function($value) {
                return $value['id'];
            },
            $myarray['row_choice']
        ),
        $myarray['row_choice']
    );
    

提交回复
热议问题