Array
(
[items] => Array
(
[0] => stdClass Object
(
[id] => 1
[order_id] => 1
[product_id] => 1
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 2,3
[product_type] => 0
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => sdf
)
[1] => stdClass Object
(
[id] => 2
[order_id] => 1
[product_id] => 2
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 1,3
[product_type] => 1
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => qqqqqqqqqq
)
[attributesValue] => Array
(
[0] => Attributes 2, Attributes 3
[1] => Attributes 1, Attributes 3
)
)
)
我想要这个数组(参见,我添加了一个键名属性)
Array
(
[items] => Array
(
[0] => stdClass Object
(
[id] => 1
[order_id] => 1
[product_id] => 1
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 2,3
[product_type] => 0
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => sdf
[attributes] => Attributes 2, Attributes 3
)
[1] => stdClass Object
(
[id] => 2
[order_id] => 1
[product_id] => 2
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 1,3
[product_type] => 1
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => qqqqqqqqqq
[attributes] => Attributes 1, Attributes 3
)
)
)
答案 0 :(得分:3)
我认为你的数组应该是这样的。
subscript(array,[1],First)
这个想法是循环你的项目,然后检查 如果它们是根据当前键退出attributesValue,则分配到新节点'attributes'
<?php
$your_arr = array(
'items' => array(
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
'attributesValue' => array(
0 => 'Attributes 2, Attributes 3'
1 => 'Attributes 1, Attributes 3'
)
)
);
答案 1 :(得分:2)
以下是您希望实现的行为(我认为)的实例:
我们的想法是将属性值从数组中分离出来,并将此数组中的索引与数组的items
子数组中的索引相匹配。
<?php
$arr = [
"items" => [
(object)[ "id"=> 1 ],
(object)[ "id"=> 2 ],
"attributeValues" => [
"Attribute for id 1",
"Attribute for id 2"
]
]
];
$attributes = $arr["items"]["attributeValues"];
unset($arr["items"]["attributeValues"]);
foreach ($attributes as $key => $value) {
if (isset($arr["items"][$key])) {
$arr["items"][$key]->attributes = $value;
}
}
print_r($arr);
打印:
Array
(
[items] => Array
(
[0] => stdClass Object
(
[id] => 1
[attributes] => Attribute for id 1
)
[1] => stdClass Object
(
[id] => 2
[attributes] => Attribute for id 2
)
)
)
答案 2 :(得分:1)
这样的事情应该有效,如果没有,请告诉我。让你的数组名称是$ arr。首先将attributesValue
从$arr
存储起来,然后将其从主数组中取消设置。现在循环数组并在数组中添加新的attributes
,使用&
作为参考,以便主数组也发生变化。
在线查询:https://3v4l.org/mJJR4,感谢@RanjeetSingh了解数组。
$arr = array(
'items' => array(
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
'attributesValue' => array(
'Attributes 2, Attributes 3',
'Attributes 1, Attributes 3'
)
)
);
$attribute = $arr['items']['attributesValue'];
unset($arr['items']['attributesValue']);
foreach($arr['items'] as $key => &$values){
$values['attributes'] = $attribute[$key];
}
echo '<pre>';
print_r($arr);
答案 3 :(得分:0)
function add_attr($items, $attr) {
$items->attributesValue = $attr;
return $items;
}
$attribute = $items['attributesValue'];
unset($items['attributesValue']);
$new_array = array_map(add_attr, $items, $attribute);
print_r($new_array);
答案 4 :(得分:0)
使用此代码,对您有帮助:))
$var = array();
foreach($array['items'] as $k => $v)
{
$var[$k] = $v;
$var[$k]['attributesValue'] = $array['items']['attributesValue'][$k];
}
unset($var['attributesValue']);
print_r($var);