PHP - 在数组中移动一个键并将其展平

时间:2017-05-29 09:02:18

标签: php laravel

我有这个对象数组:

[
  {
    "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
      },
    ]
    }
]

我想要实现的目标:

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

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

我尝试使用laravel collection flatten with depth但它失败了

public bool BindService() {
  Intent intent = new Intent("com.plustek.service.plkscan.IPlkScanService");
  this._scanServiceConnection = new ServiceConnection(this, this._scanCallBack, this._systemCallBack);
  bool serviceBoundSuccess = this.Context.BindService(intent, this._scanServiceConnection, Bind.AutoCreate);

  if (serviceBoundSuccess) {
    return true;
  }
  return false;
}

如何平整菜单['logs']对象,使其与菜单一致?

2 个答案:

答案 0 :(得分:2)

这样的事情应该可以解决问题。只需遍历您的数组,设置新属性并删除您不想要的属性。

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

答案 1 :(得分:0)

@Stefen Suhat我真的不知道laravel的语法是什么,但我用一个简单的php foreach()希望这会对你有帮助,因为你的数组的深度很长所以我有进入阵列的所有级别,检查代码和输出片段https://eval.in/806879

尝试下面的一个:

<?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
?>