gpt4 book ai didi

php - 数组与对象和更多数组混合

转载 作者:行者123 更新时间:2023-12-04 05:37:13 26 4
gpt4 key购买 nike

我的数组的 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
...

最佳答案

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

访问 _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');

关于php - 数组与对象和更多数组混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11788020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com