如何将array_push与关联数组和索引键一起使用?

时间:2015-09-22 15:25:43

标签: php simple-html-dom

我对php有点生气,因为有时我会使用它好几个星期,有时会发生几个月没用的事情。无论哪种方式,我试图以有序的方式在另一个数组上传递另一个数组的值是#34; array" ...我想要做的实际上是创建一个允许我组织增量的键特别是每行的值;

数组内容

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 
";"它需要能够识别你在哪里断线,我不记得是否有任何方法允许我访问下一个键。

目前我的代码是:

$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/>";
        }
    }
}

代码我将返回此错误:

  

警告:array_push()将参数1视为数组,在......

中给出null

特别是在array_push上,它似乎不接受增量键或者我做错了。 谁能告诉我怎么解决?

UPDATING

正如你所看到的那样,这个问题并不简单,很难解释这个问题,但我会更加清楚地认识到这个问题。 正如您在上面看到的那样,您是数组&#34; array&#34;的结构,但是是一个无序结构,需要在另一个数组中排序。概括阵列的结构&#34;阵列&#34;是:

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 , ;
&#34;;&#34;该行已完成。所以&#34 ;;&#34;附近的下一个值意味着新的生产线即将到来。我需要的是移动数组"array"classifica的所有值,但我想将它们组织起来:

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...

所以ROW1,2 ..代表数组classifica的关键字。我尝试将值推入一行并在增加$ i index之后但是代码没有添加该值,因为索引会在循环中替换实际的键,例如:

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.

0 个答案:

没有答案