How to use array_push with associative array and index key?

I am a little "rusty" with php, as it happens that sometimes I use it for several weeks, and sometimes it happens that you do not use it for several months. In any case, I am trying to pass the values ​​of another array to "array", in another array in an orderly manner. What I want to do is essentially create a key that allows me to organize incremental values ​​for each row, in particular;

array contents

Array ( [key] => value [2] => 1 [3] => Inter [4] => 4 [5] => 4 [6] => 0 [7] => 0 [8] => 5 [9] => 1 [10] => +4 [11] => 12 [12] => Chievo Verona - Inter 0 - 1 [13] => Inter - Milan 1 - 0 [14] => Carpi - Inter 1 - 2 [15] => Inter - Atalanta 1 - 0 [16] => ; [17] => 2 [18] => Torino [19] => 4 [20] => 3 [21] => 1 [22] => 0 [23] => 9 [24] => 4 [25] => +5 [26] => 10 [27] => Torino - Sampdoria 2 - 0 [28] => Hellas Verona - Torino 2 - 2 [29] => Torino - Fiorentina 3 - 1 [30] => Frosinone - Torino 1 - 2 [31] => ; [32] => 3 [33] => Fiorentina [34] => 4 [35] => 3 [36] => 0 [37] => 1 [38] => 5 [39] => 3 [40] => +2 [41] => 9 [42] => Carpi - Fiorentina 0 - 1 [43] => Fiorentina - Genoa 1 - 0 [44] => Torino - Fiorentina 3 - 1 [45] => Fiorentina - Milan 2 - 0 [46] => ; [47] => 4 [48] => Roma [49] => 4 [50] => 2 

";" He should be able to recognize where you are breaking the line, I do not remember if there is any method that allows me to access the next key.

My current code is:

 $classifica = array("key" => "value"); function buildArrayClassifica() { global $array; global $classifica; $i = 0; foreach(array_slice($array,1) as $key => $value) { if($value != ";") { array_push($classifica[$i], $value); //there is a problem echo $value . " "; } else if($value == "value ") { continue; } else { $i++; echo "<br/>"; } } } 

Code I will return this error:

Warning: array_push () Expects that parameter 1 is an array, null is specified in ...

in particular on array_push, it looks like it is not accepting incremental keys, or maybe I'm doing it wrong. Can someone tell me how to solve?

UPDATE

As you saw, the problem is not simple, and it is rather difficult to explain, but I will try to be even clearer to meet. As you can see above, you are an array structure, but it is an unordered structure that needs to be ordered in an additional array. To reproduce the structure of an array, an "array":

 1 , Inter , 4 , 4 , 0 , 0 , 5 , 1 , +4 , 12 , Chievo Verona - Inter 0 - 1 , Inter - Milan 1 - 0 , Carpi - Inter 1 - 2 , Inter - Atalanta 1 - 0 , ; 

";" means the line is completed. So, the next value next to ";" means a new line will come. I need to move the entire value of "array" to a classifica array, but I want to organize them for:

 ROW1 => 1 , Inter , 4 , 4 , 0 , 0 , 5 , 1 , +4 , 12 , Chievo Verona - Inter 0 - 1 , Inter - Milan 1 - 0 , Carpi - Inter 1 - 2 , Inter - Atalanta 1 - 0 ROW2 => Other values... 

So, ROW1, 2 .. represents the key of the classifica array. I try to press the value inside the line and after that increase $ i index , but the code does not add the value because the index replaces the actual key in the loop, for example:

 actual foreach content: $i = 0 value = "Inter" content of array=> [0] => Inter now the $i is ever 0 because the row isn't finished yet, the ";" it has not yet been reached, so the next content of foreach is: "1" but replace the "Inter" value, so this is a problem. 
+6
source share
1 answer

You cannot use array_push() in this way. Try:

 $classifica = array(); function buildArrayClassifica() { global $array; global $classifica; $i = 0; foreach(array_slice($array,1) as $key => $value) { if($value != ";") { $classifica[$i] = $value; echo $value . " "; } else if($value == "value ") { continue; } else { $i++; echo "<br/>"; } } } 

This will create indexes ( $i value) when $value added to your array. array_push() puts $value in the next numeric index and may not be what you want in appearance. You can also use $key if you want the index to match.

EDIT

After a more detailed discussion, you have a specific format in which the key is the first element, the following indices are values, and when you encounter the value ";", it starts the sequence. Therefore, when we read:

 [2] => 1 [3] => Inter [4] => 4 [5] => 4 [6] => 0 [7] => 0 [8] => 5 [9] => 1 [10] => +4 [11] => 12 [12] => Chievo Verona - Inter 0 - 1 [13] => Inter - Milan 1 - 0 [14] => Carpi - Inter 1 - 2 [15] => Inter - Atalanta 1 - 0 [16] => ; 

The first value, '1' is our index, the following values ​​become the value for this index, and we stop reading when we find ";". It looks something like this:

 <?php function buildArrayClassifica($dataArray){ $resultArray = array(); $t = array_values($dataArray); print_r($t); $lsc = 0; foreach($t as $k => $v){ if((string)$v == ';'){ echo "<p>Found ';' at [$k] => {$v}</p>"; // Found end of data // Save position $scp = $k; echo "<p>Recorded [$scp] position for ';'.</p>"; // Reset to find the Index, first int in this series $c=$lsc; // First pass this should be 0 // Set the index if($lsc ==0){ // First pass $index = intval($t[$c]); echo "<p>Getting Index from position [" . ($c) ."] => $index for Result Array.</p>"; $c++; } else { $c++; $index = intval($t[$c]); echo "<p>Getting Index from position [" . ($c) ."] => $index for Result Array.</p>"; $c++; } echo "<p>Starting to read data from [$c] until [$scp].</p>"; // Init implode variable $data = ""; for($c;$c<$scp;$c++){ //Populate variable with the series up to semicolon, skipping first element (index) $data .= $t[$c] . ", "; } echo "<p>Data collected for this round: '" . htmlentities(substr($data,0,-2)) . "'</p>"; // populate result array $resultArray[$index] = substr($data,0,-2); echo "<p>resultArray[$index] => " . htmlentities($resultArray[$index]) . "</p><br />"; $lsc = $scp; } } return $resultArray; } $oldArray = array(1, "Inter", 4 , 4 , 0 , 0 , 5 , 1 , "+4" , 12 , "Chievo Verona - Inter 0 - 1", "Inter - Milan 1 - 0", "Carpi - Inter 1 - 2", "Inter - Atalanta 1 - 0", ";", 2, "Torino", 4, 3, 1, 0, 9, 4, '+5', 10, "Torino - Sampdoria 2 - 0", "Hellas Verona - Torino 2 - 2", "Torino - Fiorentina 3 - 1", "Frosinone - Torino 1 - 2", ";", 3, "apple", 0, 4, 6, "apple", ";"); $classifica = buildArrayClassifica($oldArray); print_r($classifica); ?> 

My initial testing seems to work for what you described. The first element of the array becomes an index, the next few values ​​will explode until we reach the value of the semicolon ( ; ).

What I see as a result:

 Array ( [1] => Inter, 4, 4, 0, 0, 5, 1, +4, 12, Chievo Verona - Inter 0 - 1, Inter - Milan 1 - 0, Carpi - Inter 1 - 2, Inter - Atalanta 1 - 0 [2] => Torino, 4, 3, 1, 0, 9, 4, +5, 10, Torino - Sampdoria 2 - 0, Hellas Verona - Torino 2 - 2, Torino - Fiorentina 3 - 1, Frosinone - Torino 1 - 2 [3] => apple, 0, 4, 6, apple ) 

ASIDE

If it were me, I would put all this into an array, like this:

 $data = array(); for($c;$c<$scp;$c++){ $data[] = $t[$c]; } $resultArray[$index] = $data; 

Or if you really want a string:

 $resultArray[$index] = implode(", ", $data); 

Hope this helps.

+3
source

All Articles