0

I have this array of object:

[
  {
    "id": 1,
    "name": "Carbo",
    "menus": [
      {
        "id": 33,
        "name": "FloralWhite",
        "image": {
          "web": "https://lorempixel.com/640/360/food/?89722",
          "mobile": "https://lorempixel.com/640/360/food/?89722",
          "square": "https://lorempixel.com/640/360/food/?89722"
        },
        "logs": {
          "price": 2
        }
      },
      {
        "id": 40,
        "name": "LightGray",
        "image": {
          "web": "https://lorempixel.com/640/360/food/?63930",
          "mobile": "https://lorempixel.com/640/360/food/?63930",
          "square": "https://lorempixel.com/640/360/food/?63930"
        },
        "logs": {
          "price": 2
        }
      },
    ]
  }
]

What I want to achieve:

[
  {
    "id": 1,
    "name": "Carbo",
    "menus": [
      {
        "id": 33,
        "name": "FloralWhite",
        "image": {
          "web": "https://lorempixel.com/640/360/food/?89722",
          "mobile": "https://lorempixel.com/640/360/food/?89722",
          "square": "https://lorempixel.com/640/360/food/?89722"
        },
          "price": 2
      },
      {
        "id": 40,
        "name": "LightGray",
        "image": {
          "web": "https://lorempixel.com/640/360/food/?63930",
          "mobile": "https://lorempixel.com/640/360/food/?63930",
          "square": "https://lorempixel.com/640/360/food/?63930"
        },
          "price": 2
      },
    ]
    }
]

I've tried using laravel collection flatten with depth but it fail

$data = collect($data->menus);

$data = $data->flatten(1);
$data->values()->all();

How can I flatten the menus['logs'] object so it can one level with menu?

3
  • do you want to remove "logs" parent key ? Commented May 29, 2017 at 9:08
  • @BunkerBoy I want to remove it, and move all the content inside "logs" to its parent menu object Commented May 29, 2017 at 9:08
  • in times like this that you have to not use relationship of laravel .. use join instead .. if you want something like that .. Commented May 29, 2017 at 9:41

2 Answers 2

2

Something like this should do the trick. Simply iterate over your array, set the new property and remove the one you don't want.

        $data = json_decode("[{
        \"id\": 1,
        \"name\": \"Carbo\",
        \"menus\": [
            {
                \"id\": 33,
                \"name\": \"FloralWhite\",
                \"image\": {
                    \"web\": \"https://lorempixel.com/640/360/food/?89722\",
                    \"mobile\": \"https://lorempixel.com/640/360/food/?89722\",
                    \"square\": \"https://lorempixel.com/640/360/food/?89722\"
            },
            \"logs\": {
                \"price\": 2
            }
        },
        {
            \"id\": 40,
            \"name\": \"LightGray\",
            \"image\": {
                \"web\": \"https://lorempixel.com/640/360/food/?63930\",
                \"mobile\": \"https://lorempixel.com/640/360/food/?63930\",
                \"square\": \"https://lorempixel.com/640/360/food/?63930\"
            },
            \"logs\": {
                \"price\": 2
            }
        }
    ]
  }]");

foreach($data as $val){
    foreach($val->menus as $menuVal){
        $menuVal->price = $menuVal->logs->price;
        unset($menuVal->logs);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@StefenSuhat I've updated my answer, can you try again?
0

@Stefen Suhat i really don't know what is the syntax for laravel but i did it with a simple php foreach() hope this will help you, as your array's depth is long so i had gone to all the levels of your array, check code and output snippet here https://eval.in/806879

try below one:

<?php
$array = array(
            array(
                   "id"=> 1,
                   "name"=> "Carbo",
                   "menus"=> array(
                   array(
                        "id"=> 33,
                        "name"=> "FloralWhite",
                        "image"=> array(
                          "web"=> "https=>//lorempixel.com/640/360/food/?89722",
                          "mobile"=> "https=>//lorempixel.com/640/360/food/?89722",
                          "square"=> "https=>//lorempixel.com/640/360/food/?89722"
                    ),
                    "logs"=> array(
                      "price"=> 2
                    )
                  ),
                  array(
                    "id"=> 40,
                    "name"=> "LightGray",
                    "image"=> array(
                      "web"=> "https=>//lorempixel.com/640/360/food/?63930",
                      "mobile"=> "https=>//lorempixel.com/640/360/food/?63930",
                      "square"=> "https=>//lorempixel.com/640/360/food/?63930"
                    ),
                    "logs"=> array(
                      "price"=> 2
                    )
                  ),
                )
            )
        );

foreach($array as $key => $value){
    foreach ($value as $key1 => $value1) {
        if(is_array($value1) && $key1 == "menus"){
            foreach($value1 as $key2 => $value2) {
                foreach ($value2 as $key3 => $value3) {
                    if(is_array($value3) && $key3 == "logs"){
                        unset($array[$key][$key1][$key2][$key3]);
                        $array[$key][$key1][$key2] = array_merge($array[$key][$key1][$key2], $value3);
                    }
                }

            }

        }
    }
}
echo "array after<br>";
echo "<pre>";
print_r($array); //your array after
?>

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.