PHP二维数组如何添加一个属性(元素)。
比如对象数组添加属性foreach($mainNavas$key=>$value){foreach($childNavas$k=>$v){if($value->id==$...
比如对象数组添加属性
foreach ($mainNav as $key => $value) {
foreach ($childNav as $k => $v) {
if ($value->id == $v['sid']) {
//二维数组如何现实呢
$value->child[] = $v; //这样就可以添加属性 见下面数组数据
}
}
}
[6] => stdClass Object
(
[id] => 24
[name] => 电子数码
[sid] => 0
[child] => Array //这对象数组就是添加的,如何用二维数组现实
(
[0] => Array
(
[id] => 59
[name] => U盘
[sid] => 24
)
)
)
$mainNav 数组
Array
(
[0] => Array
(
[id] => 30
[name] => 牛仔裤
[sid] => 17
)
[1] => Array
(
[id] => 29
[name] => 连衣裙
[sid] => 17
)
$childNav 数组
[4] => Array
(
[id] => 17
[name] => 女士服装
[sid] => 0
)
[5] => Array
(
[id] => 25
[name] => 时尚家居
[sid] => 0
)
数组对象 ->child(属性名)[] = 数据 就可以添加属性
二维数组如何做到上面的效果 展开
foreach ($mainNav as $key => $value) {
foreach ($childNav as $k => $v) {
if ($value->id == $v['sid']) {
//二维数组如何现实呢
$value->child[] = $v; //这样就可以添加属性 见下面数组数据
}
}
}
[6] => stdClass Object
(
[id] => 24
[name] => 电子数码
[sid] => 0
[child] => Array //这对象数组就是添加的,如何用二维数组现实
(
[0] => Array
(
[id] => 59
[name] => U盘
[sid] => 24
)
)
)
$mainNav 数组
Array
(
[0] => Array
(
[id] => 30
[name] => 牛仔裤
[sid] => 17
)
[1] => Array
(
[id] => 29
[name] => 连衣裙
[sid] => 17
)
$childNav 数组
[4] => Array
(
[id] => 17
[name] => 女士服装
[sid] => 0
)
[5] => Array
(
[id] => 25
[name] => 时尚家居
[sid] => 0
)
数组对象 ->child(属性名)[] = 数据 就可以添加属性
二维数组如何做到上面的效果 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询