从JSON解码破坏数组推送PHP数组

时间:2014-07-14 10:27:50

标签: php arrays

$resultViewed='["88"]';

$viewed=json_decode($resultViewed);

if(!in_array("9",$viewed)){
    print_r($viewed);
    $viewed = array_push($viewed,"9");
    print_r($viewed);
}

打印

Array ( [0] => 88 ) 2

而不是

Array ( [0] => 88,[1]=>9 )

该数组有效,但使用array_push()添加其他值并不是我所期望的。

1 个答案:

答案 0 :(得分:3)

删除作业:$viewed =

$viewed = array_push($viewed,"9");

只需:

array_push($viewed,"9");

它已经在manual,它会返回新的项目数,数组的值。

或者只是使用简单的方法:

$viewed[] = "9";