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.