我怎么能使用php / laravel迭代这个JSON

时间:2018-04-12 16:31:55

标签: php laravel

我需要访问每个级别。我正在使用laravel blade和php。我一直在尝试一些不同的方法但没有成功,这是我尝试过的一个例子:

@foreach($exercisePlan as $al)
   @if(isset($al->exercises))
     <li>{{$al->exercise->description}}</li>
   @endif
@endforeach

这是我的Json,如果有人能帮助我,我将非常感激。提前谢谢!

{
      "id": 1,
      "userID": 1,
      "exercisePlanID": 1,
      "updated_at": null,
      "created_at": null,
      "exercises": [
        {
          "id": 1,
          "planID": 1,
          "trainerID": "1",
          "day": "Monday",
          "exercise": [
            {
              "id": 1,
              "name": "Concentration Curls",
              "bodyPart": "Arm",
              "muscle": "Bicep",
              "description": "While sitting on a bench with your feet firmly on the floor, place the back of your left upper arm on the inside of your thigh. Keep your arm on your thigh throughout. Put your right hand on the right knee for stability. Do your curls on the left side, then repeat on the right side. ",
              "video": "https:\/\/www.youtube.com\/watch?v=ebqgIOiYGEY",
              "created_at": null,
              "updated_at": null
            }
          ],
          "reps": "10",
          "sets": "5",
          "priority": "1",
          "weight": "10kg",
          "created_at": null,
          "updated_at": null
        },
        {
          "id": 2,
          "planID": 1,
          "trainerID": "2",
          "day": "Monday",
          "exercise": [
            {
              "id": 2,
              "name": "Preacher Curls",
              "bodyPart": "Arm",
              "muscle": "Bicep",
              "description": "Using a regular preacher bench, grab an EZ Curl bar with both hands using an underhand grip (palms facing upwards). Slowly curl the bar up to the top and bring it a few inches from your chin. Return the weight back down with a slow and controlled tempo to the starting position, allowing some resistance (negative) on the way back down. Repeat the movement for the desired number of repetitions.",
              "video": "https:\/\/www.youtube.com\/watch?v=fIWP-FRFNU0",
              "created_at": null,
              "updated_at": null
            }
          ],
          "reps": "5",
          "sets": "5",
          "priority": "2",
          "weight": "15kg",
          "created_at": null,
          "updated_at": null
        }
      ]
    }

立即编辑获取错误:未定义变量:exercise:

            @foreach($exercisePlan as $al)
                @if(isset($al->exercises))
                    @foreach($al->$exercises as $exercise)
                        <li>{{$exercise->description}}</li>
                    @endforeach
                @endif
            @endforeach

1 个答案:

答案 0 :(得分:0)

知道了:

                @foreach($exercisePlan as $plan)
                    @foreach($plan->exercises as $exercises)
                        @foreach($exercises->exercise as $exercise)
                            {{$exercise->name}}
                        @endforeach
                    @endforeach
                @endforeach