3

I have JSON response from angular front end. the response is:

 {
  "selected": null,
  "lists": [
    {
      "board one": [
        {
          "card_name": "Card 10",
          "id": 1,
          "card_unique_id": "PC1"
        },
        {
          "card_name": "card 11",
          "id": 2,
          "card_unique_id": "PC2"
        },
        {
          "card_name": "card12",
          "id": 3,
          "card_unique_id": "PC3"
        },
        {
          "card_name": "adfaf",
          "id": 12,
          "card_unique_id": "PC12"
        },
        {
          "card_name": "fa df af ad",
          "id": 13,
          "card_unique_id": "PC13"
        }
      ],
      "BoardId": "PB1"
    },
    {
      "board two": [
        {
          "card_name": "Card 20",
          "id": 4,
          "card_unique_id": "PC4"
        },
        {
          "card_name": "CArd 21",
          "id": 5,
          "card_unique_id": "PC5"
        },
        {
          "card_name": "card 23",
          "id": 6,
          "card_unique_id": "PC6"
        },
        {
          "card_name": "Card 24",
          "id": 7,
          "card_unique_id": "PC7"
        }
      ],
      "BoardId": "PB2"
    },
    {
      "board three": [
        {
          "card_name": "Card 30",
          "id": 8,
          "card_unique_id": "PC8"
        },
        {
          "card_name": "card 31",
          "id": 9,
          "card_unique_id": "PC9"
        },
        {
          "card_name": "card 32",
          "id": 10,
          "card_unique_id": "PC10"
        }
      ],
      "BoardId": "PB3"
    },
    {
      "new board": [
        {
          "card_name": "card 40",
          "id": 11,
          "card_unique_id": "PC11"
        }
      ],
      "BoardId": "PB4"
    }
  ]
}

I want to fetch all card name in laravel. In laravel controller function i code like this: It returns some error. I dont know how to fetch all these card_names.

    public function sortupdate(Request $response)
    {
       $get_result_arr = json_decode($response->getContent());

       foreach($get_result_arr as $result){
           $lists = $result->lists;
       }
     }

please anyone help. Thanks Advanced..

2
  • Please be more specific on the error it returns Commented Nov 2, 2016 at 12:50
  • i didn't know how to fetch. please answer. Thanks Wistar Commented Nov 2, 2016 at 12:52

1 Answer 1

4

Add true to json_decode() to get an associative array:

$get_result_arr = json_decode($response->getContent(), true);

Then iterate over this array and get each card_name:

foreach ($get_result_arr['lists'] as $list) {
    foreach (array_values($list)[0] as $card) {
        echo $card['card_name'];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Alexey Mezenin. But it returns Undefined index: lists.
$get_result_arr returns array. but return error as undefined index lists
@SARAN, I've updated the code and tested it. It works perfectly with your data.

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.