2

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
              ...

2 Answers 2

3

_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');
Sign up to request clarification or add additional context in comments.

Comments

0

If I were you, I wouldn't let a little syntactic sugar like property visibility get between me and my data.

How do I access a private or protected property of an object?

With ReflectionProperty why of course =)

This should do the trick:

$my_options_count = array_map(function ($object) {
    $reflect = new ReflectionObject($object);
    foreach ($reflect->getProperties() as $prop)
         if ($prop->name == '_data') {
             $prop->setAccessible(true);
             $data = $prop->getValue($object);
            if (array_key_exists('my_options', $data))
                if (is_array($my_options = $data['my_options']))
                    return count($my_options);
         }
   return 0;
}, $myVar);

$my_options_count will now show 0 for the records without a my_options or the number of options for each record. If you were after the total my options from all the records then array_sum is your friend =)

$my_options_total = array_sum($my_options_count);

Never say never and always keep trying. Njoy!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.