title: Retrieving a value dynamically when depth of array is unknown
body: <p>I'm trying to make a function that will make a simple key->value list based on different parameters of an object. I have it working fine so long as each object is flat, but I'm in a situation now where I need the list's value to be based on a property that's an array. I can force it to assume this only goes one level deep, but how would I account for variable array depths?</p>
  
  <p>I've changed it so that the <code>value</code> argument can be passed an array that assumes the form $object->array[0][array[1]];</p>
  
  <p>Hopefully you can tell from the code what I'm trying to do:</p>
  
  <pre><code>function make_list($data, $value = 'name', $key = 'id'){
      $new_arr = array();
      if (is_array($data))
      {
        if (isset($data[0]-&gt;weight)){
          usort($data, function( $a, $b ){
            return $a-&gt;weight == $b-&gt;weight ? 0 : ( $a-&gt;weight &gt; $b-&gt;weight ) ? 1 : -1;
          });
        } elseif (isset($data[0]-&gt;value)){
          usort($data, function( $a, $b ){
            return $a-&gt;value == $b-&gt;value ? 0 : ( $a-&gt;value &gt; $b-&gt;value ) ? 1 : -1;
          });
        }
        foreach ($data as $key =&gt; $item) 
        {
          if (!is_object($item))
          {
              $item = (object)$item;
          }
          if(isset($item-&gt;{$key}))
          {
            if (is_array($value)){
              //account for nested value requests, assuming object properties that are arrays (seems to be what most of the database calls return) ['property', 'value']
              foreach ($value as $key =&gt; $val) {
                if($key == 0){
  
                } else {
                          //don't know what to do here!
                }
              }
                      //THIS WOULD GENERALLY WORK IF ARRAY LENGTH IS 2 -- BUT SYNTAX IS FUBARED
              $new_arr[$item-&gt;{$key}] = $item-&gt;{$value[0]}[{$value[1]}];
            } else {
                      //THIS IS THE OLD BEHAVIOUR THAT WAS WORKING WHEN PASSING A STRING
                 $new_arr[$item-&gt;{$key}] = $item-&gt;{$value};
            }
          }
        }
      }
      return $new_arr;
    }
  </code></pre>
  
  <p>Once I figure that out I would make it so the $key can also be an array.</p>
  
  <pre><code>$data = new Customer();
  $data-&gt;name = 'John Smith';
  $data-&gt;age = 21;
  $data-&gt;contact = array('street'=&gt;'Main St', 'number'=&gt;56, 'city_id'=&gt;1, 'city'=&gt;array('name'=&gt;'Dorset'));
  
  //Want these behaviours to work:
  make_list($data, array('contact', 'city', 'name');
  make_list($data, array('contact', 'street');
  </code></pre>
  
  <p>make_list($data, 'age');</p>