Reputation: 119
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
)
)
)
I want this array in (see, I added one key name attributes)
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
)
)
)
Upvotes: 1
Views: 70
Reputation: 164
use this code, it is helpful to you :)
$var = array();
foreach($array['items'] as $k => $v)
{
$var[$k] = $v;
$var[$k]['attributesValue'] = $array['items']['attributesValue'][$k];
}
unset($var['attributesValue']);
print_r($var);
Upvotes: 0
Reputation: 150
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);
Upvotes: 0
Reputation: 9583
Something like that should work, if not then let me know. Let your array name is $arr. First store your attributesValue
from $arr
and unset it from the main array. Now loop the array and add the new attributes
in the array, use &
for the reference so the main array also changed.
Check Online: https://3v4l.org/mJJR4, Thanks to @RanjeetSingh for the array.
$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);
Upvotes: 1
Reputation: 924
I am assume your array shouild be like this.
<?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'
)
)
);
The idea is loop over your items then check if their is attributesValue is exits according to current key, then assign into new node 'attributes'
<?php
$new_arr = array();
foreach($your_arr as $key => $item) {
$new_arr[$key] = $item;
if(isset($your_arr['attributesValue'][$key])) {
$new_arr[$key]['attributes'] = $your_arr['attributesValue'][$key];
}
}
echo '<pre>';
print_r($new_arr);
?>
Upvotes: 3
Reputation: 40673
Here's a working example of what (I think) is the behaviour you want to achieve:
The idea is to separate the attribute values out of the array and match the indices in this array with the indices in the items
subarray of your array.
<?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);
Prints:
Array
(
[items] => Array
(
[0] => stdClass Object
(
[id] => 1
[attributes] => Attribute for id 1
)
[1] => stdClass Object
(
[id] => 2
[attributes] => Attribute for id 2
)
)
)
Upvotes: 2