I'm dealing with some really old code I'm trying to get running on Laravel. The database is complicated and full of unnecessarily long stored procedures that I don't have the budget to rewrite.
For the most part, these work well, as I can access singular results like $myID = $result[0]->id
I'm hoping this is something more than a gap in my PHP knowledge...
$result = DB::select(DB::raw("Call MyOldStoredProcedure()"));
print_r($result);
This gives me:
Array ( [0] => stdClass Object ( [MIN(user_responses.sectionid)] => 2 ) )
Which I unsurprisingly can't access as
$number = $result[0]["MIN(user_responses.sectionid)"]; //or...
$number = $result[0]->...
What can I do to retrieve this singular result from this weird associative array/object? I'm about to give up and parse the array string, but I know there's a better way.
The easiest option is to use curly braces in order to be able to quote the special characters:
$number = $result[0]->{'MIN(user_responses.sectionid)'};
Another option is to set a variable to the name of the property, and then access the property using the variable:
$property = 'MIN(user_responses.sectionid)';
$number = $result[0]->$property;
Additionally, you could also convert the object to an array, and access the property like you originally attempted.
$array = (array)$result[0];
$number = $array['MIN(user_responses.sectionid)'];
// or, if you're on PHP 7+
$number = ((array)$result[0])['MIN(user_responses.sectionid)'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.