Convert a PHP object to an associative array

后端 未结 30 1571
走了就别回头了
走了就别回头了 2020-11-22 02:18

I\'m integrating an API to my website which works with data stored in objects while my code is written using arrays.

I\'d like a quick-and-dirty function to convert

30条回答
  •  遇见更好的自我
    2020-11-22 02:38

    You might want to do this when you obtain data as objects from databases:

    // Suppose 'result' is the end product from some query $query
    
    $result = $mysqli->query($query);
    $result = db_result_to_array($result);
    
    function db_result_to_array($result)
    {
        $res_array = array();
    
        for ($count=0; $row = $result->fetch_assoc(); $count++)
            $res_array[$count] = $row;
    
        return $res_array;
    }
    

提交回复
热议问题