Search code examples
phpphp-5.3

array mixed with objects and more arrays


var_dump of my array shows up like this. I want to get the count of my_options in the array. note that index 2 has no options. How' I go about doing that

var_dump($myVar);


array
  0 => 
    object(app\models\Product)[209]
      protected '_Nested' => 
        array
          empty
      protected '_Sibling' => 
        array
          empty
      protected '_class' => string 'app\models\Customer' (length=18)
      protected '_data' => 
        array
          '_id' => int 345543
          'customer_name' => string 'John Dee' (length=14)
          'Sibling' => 
            array
              ...
          'my_options' => 
            array
              ...
          'Nesting' => 
            array
              ...
          'image' => string 'img.jpg' (length=35)
          'inventory' => 
            array
              ...
          'name' => string 'papa john' (length=35)
          'price' => float 26
          'status' => int 1
1 => 
    object(app\models\Product)[209]
      protected '_Nested' => 
        array
          empty
      protected '_Sibling' => 
        array
          empty
      protected '_class' => string 'app\models\Customer' (length=18)
      protected '_data' => 
        array
          '_id' => int 89237
          'customer_name' => string 'Linda Arap' (length=14)
          'Sibling' => 
            array
              ...
          'my_options' => 
            array
              ...
          'Nesting' => 
            array
              ...
          'image' => string 'img2.jpg' (length=35)
          'inventory' => 
            array
              ...
          'name' => string 'Pizza Hut' (length=35)
          'price' => float 26
          'status' => int 1
2 => 
    object(app\models\Product)[209]
      protected '_Nested' => 
        array
          empty
      protected '_Sibling' => 
        array
          empty
      protected '_class' => string 'app\models\Customer' (length=18)
      protected '_data' => 
        array
          '_id' => int 89237
          'customer_name' => string 'Linda Arap' (length=14)
          'Sibling' => 
            array
              ...

Solution

  • _data is a protected variable so you won't be able to access it from outside the object. You'll need to use (or create) a getData() method that accesses$this->_data. Just call that method and you'll have access to the options array.

    Example method that accesses _data and returns my_options all in one:

    public function getMyOptions() {
      return $this->_data['my_options'];
    }
    

    Which can be invoked with something like:

    $product instanceof app\models\Product;
    $myOptions = $product->getMyOptions();
    

    Also, it looks like you're using a model (possibly orm) class. I would imagine it has built-in methods to access the data array. Common methods to access my_options would be:

    $options = $product->my_options; // via magic methods
    $options = $product->get('my_options');
    $options = $product->getField('my_options');