0

I have array data like this

enter image description here

I want to add new key expected_count to the array with if conditional,

if flags key == is_join ? expected_count=2
if flags key == is_grade ? expected_count=1

So the result will be like this

enter image description here

How the best way to do this?

1 Answer 1

2

Use foreach to iterate over the array

foreach($data['sections'] as $key => $section){
     if(array_key_exists('is_join', $section['flags'])){
         $data['sections'][$key]['flags']['is_join']['expected_count'] = 2;
     }
     if(array_key_exists('is_grade', $section['flags'])){
         $data['sections'][$key]['flags']['is_grade']['expected_count'] = 1;
     }
}

I hope this will work.

Sign up to request clarification or add additional context in comments.

1 Comment

@AndiSiswanto Glad to hear, If it solved your issue please accept the answer and close the question ;)

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.