0

This is the json array I have and I decode it using json_decode() of php

{"form":{"fieldsets":[{"fieldset_name":"name_1","datapoints":{"dp_id_1":1,"dp_id_2":4}},{"fieldset_name":"name_2","datapoints":{"dp_id_3":1,"dp_id_4":5}}]}}.

After decoding the array I obtained is

Array ( 
    [form] => Array ( 
        [fieldsets] => Array ( 
            [0] => Array ( 
                [fieldset_name] => name_1 
                [datapoints] => Array ( 
                    [dp_id_1] => 1 
                    [dp_id_2] => 4 
                ) 
            ) 
            [1] => Array ( 
                [fieldset_name] => name_2 
                [datapoints] => Array ( 
                    [dp_id_3] => 1 
                    [dp_id_4] => 5 
                ) 
            ) 
        ) 
    ) 
) 

Now I want to push datapoints array dp_id_1,dp_id_2 elements into one array and dp_id_3,dp_id_4 into other array

Please help me........

1 Answer 1

4

Simply loop over the array:

$points = array();

for($array['form']['fieldsets'] as $set) {
    $points = array_merge($points, $set['datapoints']);
}

Update:

Regarding your edit, then it is even simpler. Replace the body with:

$points[] = $set['datapoints'];

I suggest to read about arrays in PHP to get a better understanding on how they work.

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

1 Comment

I also want dp_id_1,dp_id_2 into one array and dp_id_3,dp_id_4 into other array

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.