Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用另一个数组替换/更改数组值_Php_Arrays - Fatal编程技术网

Php 用另一个数组替换/更改数组值

Php 用另一个数组替换/更改数组值,php,arrays,Php,Arrays,我有两个数组 <?php //Array 1 $txt_bln[] = array($bln_txt => 0); echo json_encode($txt_bln) //the result :[{"Januari":0},{"Februari":0},{"Maret":0},{"April":0},{"Mei":0},{"Juni":0},{"Juli":0},{"Agustus":0},{"September":0

我有两个数组

<?php 
       //Array 1
       $txt_bln[]   = array($bln_txt => 0);
       echo json_encode($txt_bln)
       //the result :[{"Januari":0},{"Februari":0},{"Maret":0},{"April":0},{"Mei":0},{"Juni":0},{"Juli":0},{"Agustus":0},{"September":0},{"Oktober":0},{"November":0},{"Desember":0}]

       //Array 2
       $data_bln_2[]    = array($bln_txt_2 => (int)$results_jmlh['total']);
       echo json_encode($data_bln_2)
       //the result :[{"April":1},{"Oktober":1},{"Desember":8}]

?>
然后将其更改为php数组。最后的产出:

[0,0,0,1,0,0,0,0,0,1,0,8]
谢谢您的帮助。

您正在寻找阵列合并吗

将一个或多个数组的元素合并在一起,以便将其中一个数组的值追加到前一个数组的末尾。它返回结果数组

如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个值。但是,如果数组包含数字键,则后面的值将不会覆盖原始值,而是追加

带有数字键的输入数组中的值将在结果数组中以从零开始递增的键重新编号

如果您想忘记字符串键来获取数字键,我猜您需要查找数组_值

array_values返回数组中的所有值,并对数组进行数字索引


到目前为止你试过什么?
[0,0,0,1,0,0,0,0,0,1,0,8]
array array_merge ( array $array1 [, array $... ] )
array array_values ( array $array )
<?php
$previous=json_decode('[{"Januari":0},{"Februari":0},{"Maret":0},{"April":0},{"Mei":0},{"Juni":0},{"Juli":0},{"Agustus":0},{"September":0},{"Oktober":0},{"November":0},{"Desember":0}]',true);   
//replace json value with your variables 
$current=json_decode('[{"April":1},{"Oktober":1},{"Desember":8}]',true);
//replace json value with your variable 
$latest=array();
$modified_latest=array();
foreach($previous as $p)
{
    $found=false;
    foreach($current as $c)
    {
        if(key($p)==key($c))
        {
            $latest[]=$c;
            $modified_latest[]=$c[key($c)];
            $found=true;
            break;
        }
    }
    if(!$found)
    {
        $latest[]=$p;
        $modified_latest[]=$p[key($p)];
    }

}    
echo json_encode($latest);
//outputs : [{"Januari":0},{"Februari":0},{"Maret":0},{"April":1},{"Mei":0},{"Juni":0},{"Juli":0},{"Agustus":0},{"September":0},{"Oktober":1},{"November":0},{"Desember":8}]

 print_r($modified_latest);
 //outputs :Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 1 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 1 [10] => 0 [11] => 8 ) 
 //that means : [0,0,0,1,0,0,0,0,0,1,0,8]