0

I want to fetch the data from children field or get count of children.

I am fetching like this: $temp = json_decode($request->get('tempData');

Here is my json payload:

tempData:[{
      "id":"c625",
      "name":"Chapter 1",
      "children":[
         {
            "id":3,
            "type":"study material",
            "content":"Demo Study Material Title 2"
         },
         {
            "id":4,
            "type":"study material",
            "content":"Demo Study Material Title 3"
         },
         {
            "id":5,
            "type":"study material",
            "content":"Demo Study Material Title 4"
         }
      ],
      "type":"chapter"
   }
]

2 Answers 2

3

You can get children count for all items on tempData array by using collection

collect(json_decode($request->get('tempData'), true))
->mapWithKeys(fn($item) => [$item['id'] => count($item['children'])])
->all();

to get result of children count for every id:

 [
    "c625" => 3
 ]
Sign up to request clarification or add additional context in comments.

1 Comment

syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' .. this error is thrown on UI
0

You could create a new array and push children in that array with a foreach loop.

$temp = json_decode($request->get('tempData');
$children = array();
$child_count = 0;
foreach ($temp['children'] as $child) {
    $child_count++;
    array_push($children, $child);
}

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.