【问题标题】:array mixed with objects and more arrays数组与对象和更多数组混合
【发布时间】:2012-08-03 01:17:18
【问题描述】:

我的数组的 var_dump 显示如下。我想获取数组中 my_options 的计数。请注意,索引 2 没有选项。我该怎么做呢

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

【问题讨论】:

    标签: php php-5.3


    【解决方案1】:

    _data 是一个受保护的变量,因此您将无法从对象外部访问它。您需要使用(或创建)访问$this->_datagetData() 方法。只需调用该方法,您就可以访问 options 数组。

    访问_data并返回my_options的示例方法:

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

    可以通过以下方式调用:

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

    此外,您似乎正在使用模型(可能是 orm)类。我想它有内置的方法来访问数据数组。访问my_options 的常用方法是:

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

    【讨论】:

      【解决方案2】:

      如果我是你,我不会让 syntactic sugarproperty visibility 这样的小人物挡在我和我的数据之间。

      如何访问对象的私有或受保护属性?

      ReflectionProperty 为什么当然 =)

      这应该可以解决问题:

      $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 现在将为没有 my_options 或每条记录的选项数的记录显示 0。如果您想从所有记录中获得我的全部选项,那么 array_sum 就是您的朋友 =)

      $my_options_total = array_sum($my_options_count);
      

      永远不要说永远,永远努力。开心!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        • 2021-11-30
        • 2012-07-25
        • 1970-01-01
        • 1970-01-01
        • 2015-08-25
        相关资源
        最近更新 更多