Hi everyone
I have an array of stdClass objects.
At the moment it's non-trivial to change the structure of the array so I'm having to use what feels like a longhand way of getting all the contents of an object into the appropriate variable names.
foreach($clientarray AS $itemid => $objects){
if ($objects->ClientID == $_GET[clientid]){ //this one
$ClientName = $objects->ClientName;
$Contact = $objects->Contact;
$Phone = $objects->Phone;
$Fax = $objects->Fax;
} // done assigning variable names to contents of correct object
} // done parsing array
Is there a more elegant way of:
1) Identifying which item in the array I want (rather than parsing the whole thing)? As I write this I realise I could exit the array parsing once I've found the specific object I want, which would save some looping, but jumping straight to it would be much better.
2) Assigning the variables. There are actually many more than the 4 shown; up to 20.
Long term I would have set the keys to the ClientID but for all sorts of reasons, I can't do that yet.
I would also prefer not to have the array contain stdObjects, but I didn't discover this solution on StackExchange until recently:
// These both give you an array:
$json = json_decode($whatever, true);
$json = (array) json_decode($whatever);
(credit to http://stackoverflow.com/questions/5515918/php-cannot-use-object-stdclass-as-array )
Thank you