0

Please Help in conversion of an array from one form to another I Have This Array

Array ( 
    [mpr_last_month] => 376431 
    [mpr_month] => 03 
    [total_boys_all_6m_36m] => 5550225 
    [total_girls_all_6m_36m] => 5215529 
    [total_boys_all_36m_72m] => 4209639 
    [total_girls_all_36m_72m] => 4149613 
    [total_pse_boys_36m_72m] => 4442301 
    [total_pse_all_girls_36m_72m] => 4413446 
    [total_pregnanting] => 2209158 ) 



Array (
    [mpr_last_month] => 448216 
    [mpr_month] => 04 
    [total_boys_all_6m_36m] => 7153209 
    [total_girls_all_6m_36m] => 6798913 
    [total_boys_all_36m_72m] => 5175846 
    [total_girls_all_36m_72m] => 5105460 
    [total_pse_boys_36m_72m] => 5290617 
    [total_pse_all_girls_36m_72m] => 5263340 
    [total_pregnanting] => 2944612 
    ) 

Array ( 
    [mpr_last_month] => 448253 
    [mpr_month] => 05 
    [total_boys_all_6m_36m] => 11742417 
    [total_girls_all_6m_36m] => 6362815 
    [total_boys_all_36m_72m] => 4879252 
    [total_girls_all_36m_72m] => 4756805 
    [total_pse_boys_36m_72m] => 5344042 
    [total_pse_all_girls_36m_72m] => 5095155 
    [total_pregnanting] => 2852864 

    )

Array ( 
    [mpr_last_month] => 470848 
    [mpr_month] => 06 
    [total_boys_all_6m_36m] => 6552523 
    [total_girls_all_6m_36m] => 6217771 
    [total_boys_all_36m_72m] => 4613019 
    [total_girls_all_36m_72m] => 4551685 
    [total_pse_boys_36m_72m] => 5182666 
    [total_pse_all_girls_36m_72m] => 5165730 
    [total_pregnanting] => 2746293 
    ) 

Array ( 
    [mpr_last_month] => 465489 
    [mpr_month] => 07 
    [total_boys_all_6m_36m] => 6638749 
    [total_girls_all_6m_36m] => 6310676 
    [total_boys_all_36m_72m] => 4801665 
    [total_girls_all_36m_72m] => 4657764 
    [total_pse_boys_36m_72m] => 5020964 
    [total_pse_all_girls_36m_72m] => 5051785 
    [total_pregnanting] => 2815773

    )

I Want This

 name: 'mpr_last_month',
 data: [43934, 52503, 57177, 69658, 97031, 119931, 137133,  154175,123,123,123,123]

. . . . . . . .

1
  • 3
    What have you already tried? Commented Sep 19, 2017 at 7:28

2 Answers 2

2

Simplest approach would be foreach loop.

$newData = [];
foreach ($yourArray as $innerArray) {
    foreach ($innerArray as $key => $value) {
        $newData[$key][] = $value;
    }
}

It loops over the first array (the big one, containg all others), the runs over each inner array and store the value in the correct place.

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

Comments

0

You can use array_column function. But be aware that this function works only for PHP >= 5.5.0. Try this:

//example data
$array = [
    ['a' => 2, 'b'=>3, 'c'=>6],
    ['a' => 12, 'b'=>13, 'c'=>16],
    ['a' => 22, 'b'=>23, 'c'=>26],
];


$result = [];

//get indexes from first element of array
$indexes = array_keys($array[0]);

foreach($indexes as $index) {
    $result[$index] = array_column($array, $index);
}

And result is:

Array
(
    [a] => Array
        (
            [0] => 2
            [1] => 12
            [2] => 22
        )

    [b] => Array
        (
            [0] => 3
            [1] => 13
            [2] => 23
        )

    [c] => Array
        (
            [0] => 6
            [1] => 16
            [2] => 26
        )

)

2 Comments

Wow the array_column() function is pretty neat! Didnt know about it, thank sir.
@ManuelMannhardt, I'm happy that you've learn something new :)

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.