Pathros
Pathros

Reputation: 10720

PHP: How to store values from a big (bidimensional) array into different arrays in a foreach loop

i have an array in which each key it has another array. What i want to do is to store those values in different arrays. For example, the original array looks like the following:

Array
(
    [0] => Array
        (
            [concurso] => 2758
            [R1] => 12
            [R2] => 20
            [R3] => 33
            [R4] => 46
            [R5] => 50
            [R6] => 51
            [R7] => 54
        )

    [1] => Array
        (
            [concurso] => 2759
            [R1] => 12
            [R2] => 15
            [R3] => 31
            [R4] => 50
            [R5] => 54
            [R6] => 55
            [R7] => 11
        )

    [2] => Array
        (
            [concurso] => 2760
            [R1] => 4
            [R2] => 11
            [R3] => 12
            [R4] => 40
            [R5] => 45
            [R6] => 51
            [R7] => 55
        )

.
.
.

    [29] => Array
        (
            [concurso] => 2787
            [R1] => 3
            [R2] => 5
            [R3] => 19
            [R4] => 24
            [R5] => 28
            [R6] => 30
            [R7] => 15
        )

)

And for each key i want to store the corresponding values in different arrays (where 'concurso' will be the key of each new array and its corresponding Rn value):

R1:

Array
(
    [2758] => 12
    [2759] => 12
    [2760] => 4
...
    [2787] => 3
)

R2:

Array
(
    [2758] => 20
    [2759] => 15
    [2760] => 11
...
    [2787] => 5
)

R3:

Array
(
    [2758] => 33
    [2759] => 31
    [2760] => 12
...
    [2787] => 19
)

R4:

Array
(
    [2758] => 46
    [2759] => 50
    [2760] => 40
...
    [2787] => 24
)

R5:

Array
(
    [2758] => 50
    [2759] => 54
    [2760] => 45
...
    [2787] => 28
)

R6:

Array
(
    [2758] => 51
    [2759] => 55
    [2760] => 51
...
    [2787] => 30
)

...

Rn:

How do i achieve this? I guess i need to create variable names dynamically, since the number of elements of a given array may change depending on the data retrieved. What do you suggest?

I am trying this code but no luck so far:

$ultimos_sorteos_m,true); //this is the big array shown above

foreach($ultimos_sorteos_m as $key1 => $last_sorteos){
    $contador=count($last_sorteos); //how many items the current sub-array has
    $k=1; //an index
    echo '<p>the number of items is '.$contador.'</p>';
    foreach($last_sorteos as $key=>$valor){
        if($key=='concurso'){
            $concurso=$valor;
            echo 'concurso: '.$concurso.' <br>'; //to get the 'concurso' that will work as a key for the other arrays
        }
            //storing here the rest of the values
        if(substr( $key, 0, 1 ) === "R" && substr($key, 1, 1)===$k){
                //i don't know here how to store the values in different arrays
                echo 'storing value: '.$valor.'<br>';
                $Ritems[$concurso]=$valor; //the problem is that only store the last value
        }   
    }
}

If you want to know why, I want it this way in order to graph those data by using the phpgraphlib graphing library. It will be a graph showing different lines.

Upvotes: 0

Views: 72

Answers (3)

georg
georg

Reputation: 214949

Like this?

foreach($orig_array[0] as $key => $_)
   $new_array[$key] = array_column($orig_array, $key, 'concurso');

If you're absolutely sure you need R1, R2 as variables (you don't), you can extract() the array afterwards.

Upvotes: 2

Jay S.
Jay S.

Reputation: 1327

Try this : This creates variable names dynamically so you dont need to know the number for R1,R2,R3 etc elements you have

<?php

foreach($ultimos_sorteos_m[0] as $key1 => $last_sorteos){

  $$key1 = array_column($ultimos_sorteos_m, $key1 , 'concurso'); // This is a dynamic variable name. See http://php.net/manual/en/language.variables.variable.php

}

var_dump($R1);
var_dump($R2);


?>

Please comment if you see a problem. Thanks!

Upvotes: 2

MH2K9
MH2K9

Reputation: 12039

You can use array_column. Try this if your PHP version 5.5+

$R1 = array_column($arr, 'R1', 'concurso');
.
.
.
$R7 = array_column($arr, 'R7', 'concurso');

Upvotes: 1

Related Questions