0

I have a json array in my Particles controller that looks like this after json_encode, true:

Array
(
    [0] => Array
        (
            [Particle.day_id] => Sat
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [1] => Array
        (
            [Particle.day_id] => Fri
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [2] => Array
        (
            [Particle.day_id] => Thu
            [Particle.week_no] => 1
            [Particle.pattern_id] => 589
            [Particle.work] => 0
        )

    [3] => Array
        (.....

I am trying to convert this array to the correct format for a multiple record save:

Array
(
    [Particle] => Array(
            [0] => Array
                (
                            [day_id] => Sat
                            [week_no] => 1
                            [pattern_id] => 589
                            [work] => 0
                        )
            [1] => Array
                (
                            [day_id] => Fri
                            [week_no] => 1
                            [pattern_id] => 589
                            [work] => 0
                        )
                )


                 ....

Where Particle is my Cakephp model name.

The nearest I can get is using this code (php):

 $output = array();

        foreach ($jsonData as $keyA => $valueA) {
        foreach ($valueA as $keyB => $valueB) {
            $output = Set::insert( $output, $keyB, $valueB );
        }
        }

Where $jsonData is the json_encoded array. This gives me:

Array
(
    [Particle] => Array
        (
            [day_id] => Sun
            [week_no] => 1
            [pattern_id] => 589
            [work] => 1
        )

)

Which is only the first part of the array - how do I get the rest of the array?

I know the answer will probably be straighforward but it has baffled me for too long now!

Thanks for any help.

1 Answer 1

1
$result = array();
$parentKey = null;
foreach ($jsonData as $keyA => $valueA) {
     foreach ($valueA as $keyB => $valueB) {
        $keyB = explode('.', $keyB);
        list($parentKey, $childKey) = $keyB;
        $output[$childKey] = $valueB;
     }

    if(!isset($result[$parentKey]))
       $result[$parentKey] = array();

   array_push($result[$parentKey], $output);
}


 print_r($result);

Your current code it over writing the array $output. Thats why it is showing you the last part of data.

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

4 Comments

Now I see! This brings me to my next issue - the $result array does not quite match the format I need (as shown in the original question).
Not too sure how to change this - I have tried a few combinations of keys and values but can't seem to hit the right combination - do you have any clues?
I think there is a syntax error on the list($parent.. line, not too sure what it is.
This works a treat! Thanks for your quick answer - now I can move on to my next problem!

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.