1
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
                )
        )
)
4
  • 1
    Can you post your actual array ? Commented May 18, 2016 at 10:03
  • 1
    Not a clear question... Commented May 18, 2016 at 10:07
  • Please specify the behaviour you want to achieve. From the context one can assume that you want to match each entry in attributesValue with each entry in items excluding attributesValue but its not clear whether this is the case. Commented May 18, 2016 at 10:08
  • Got you, check my answer. Commented May 18, 2016 at 10:15

5 Answers 5

3

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);
    ?>
Sign up to request clarification or add additional context in comments.

2 Comments

I think the looping should go over $your_arr["items"] and ignore the 'attributeValues' key.
Nice, What about my answer?
2

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
                )

        )

)

1 Comment

Nice, What about my answer?
1

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);

Comments

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);

2 Comments

Welcome to StackOverflow! Please consider adding some explanation to your code. Thank you!
@Aurasphere Thank you :)
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.