我有点怀疑如何从对象数组中选择一个非常具体的信息让我说我有这个JSON数据:
Category {#790
#id: 1234
#name: "Tines"
#slug: "calcetines-y-medias/tines"
#urlKey: "tines"
#categoryBusinessPlan: ""
#isCurrent: false
#count: 0
#path: array:5 [
0 => Category {#732
#id: 16006
#name: "test"
#slug: "test"
#urlKey: "test"
#categoryBPlan: "3_1 test"
#isCurrent: false
#count: 0
#parent: null
#hasChildren: false
}
1 => Category {#789
#id: 16642
#name: "test2"
#slug: "moda/test2"
#urlKey: "mujeres"
#categoryBPlan: "3_1 test"
#isCurrent: false
#count: 0
#parent: null
#hasChildren: false
}
但是有一个小秘密,它是一个带对象的数组:
array (size=2)
0 =>
object(\Catalog\Category)[1620]
protected 'id' => int 16006
protected 'name' => string 'Moda' (length=4)
protected 'slug' => string 'moda' (length=4)
protected 'categoryBPlan' => string '3_1 test' (length=8)
protected 'count' => int 0
protected 'parent' => null
protected 'hasChildren' => boolean false
1 =>
object(\Catalog\Category)[1529]
protected 'id' => int 16642
protected 'name' => string 'Mujeres' (length=7)
protected 'slug' => string 'moda/mujeres' (length=12)
protected 'urlKey' => string 'mujeres' (length=7)
protected 'categoryBusinessPlan' => string '3_1 test' (length=8)
protected 'isCurrent' => boolean false
protected 'count' => int 0
protected 'parent' => null
protected 'hasChildren' => boolean false
所以我试图用current()
来获取第一个数组,同时用下一个代码获得'categoryBPlan':
$categoryBusinessPlanPath = $categoryTree->getPath(); // this part it's only to obtain in the #path
// This is when I'm trying to use current() but it doesn't work
// $categoryBusinessPlanPath = current($categoryTree->getPath());
$test = null;
foreach ($categoryBusinessPlanPath as $struct) {
$test = $struct->getCategoryBusinessPlan(); // <--- works
}
但是这里有一个小问题,我如何使用current()
数组与对象???
答案 0 :(得分:1)
请你试试以下内容:
$categoryBusinessPlanPath = $categoryTree->getPath();
$test = current(categoryBusinessPlanPath)->getCategoryBusinessPlan();