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

前端 未结 2 938
别跟我提以往
别跟我提以往 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 04:58

    You should create a new array variable as below.

    $recArr = array();
    
    while ($records = mysqli_fetch_array($query)) {
        $recArr[$records['id']] = $records;
    }
    
    var_dump($recArr);
    

    Good luck.

    0 讨论(0)
  • 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']
    );
    
    0 讨论(0)
提交回复
热议问题