Custom array from a query only write the last row of the query

Looking for some input on this one. I am writing a custom array but having a strange issue. It seems to be cycling through the loop and dropping previous values and only storing the last row of the query. Any ideas?

global $wpdb;
$queryresult = $wpdb->get_results(select query);

$modified_result = array();

foreach($queryresult as $result){
$modified_result[‘name’] = $result->name;
$modified_result[‘address’] = $result->address;
}

$datadump = json_encode($modified_result);
echo file_put_contents(‘my_output.json’, $datadump);

// outputs {“name”:”Mark”,”address”:”1313 Mockingbird Lane”}
// which is only the LAST row in the query instead of
// building an array of ALL names and addresses that should be
// produced by the loop. Any ideas anyone?

Read more here:: Custom array from a query only write the last row of the query

Leave a Reply

Your email address will not be published. Required fields are marked *