PHP Find value in object

find, lookup, object, php

Solution

If the values in the array are not in order (eg array[0] does not always contain field_number 1) then you will need to iterate the array:

foreach($array as $item){
    if($item->field_number==1){
      $first_name = $item->value;
      break;
    }
}

However, if this is the result of an SQL query, probably you need to rewrite the query to give you data in a more useable form

Problem

I have a result of an SQL query that looks like this ``` Array ( [0] => stdClass Object ( [field_number] => 1 [value] => Joe ) [1] => stdClass Object ( [field_number] => 2 [value] => Bloggs ) [2] => stdClass Object ( [field_number] => 3 [value] => 12566 ) [3] => stdClass Object ( [field_number] => 4 [value] => 2000-07-24 ) ) ``` It wont always return all the fields as some are not required therefore not saved to the database. I know that first name is stored with field number 1. How can I look this up in the object. EG ``` $first_name = $result => field_number == 1 ``` I know thats not right, but Im sure there must be a simple way to get this info? Thanks

Original source