array_replace_recursive不会用一些空数组替换数组

时间:2015-07-09 08:25:46

标签: php arrays

我遇到了问题。这是我的代码:

html
      head
        title File upload Node.
      body
        form#uploadForm(enctype='multipart/form-data', action='/api/photo', method='post')
          input(type='file', name='userPhoto')
          input(type='submit', value='Upload Image', name='submit')
      script(src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js')
      script(src='http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js')
      script.
        $(document).ready(function() {
        $('#uploadForm').submit(function() {
        $(this).ajaxSubmit({
        error: function(xhr) {
        status('Error: ' + xhr.status);
        },
        success: function(response) {
        console.log(response);
        }
        });
        return false;
        });
        });
---------------------------ur sever file-------------
    app.use(multer({ dest: './uploads/',
     rename: function (fieldname, filename) {
        return filename+Date.now();
      },
    onFileUploadStart: function (file) {
      console.log(file.originalname + ' is starting ...')
    },
    onFileUploadComplete: function (file) {
      console.log(file.fieldname + ' uploaded to  ' + file.path)
      done=true;
    }
    }));

    app.get('/',function(req,res){
          res.sendfile("index.html");
    });

    app.post('/api/photo',function(req,res){
      if(done==true){
        console.log(req.files);
        res.end("File uploaded.");
      }
    });

在$ c中,我希望看到 ['elm1'=> 1,'elm2'=> []] ,但我得到 ['elm1'=> 1,'elm2'=> [3]] 即可。它不会取代'elm2'=> [3] 'elm2'=> []

这是某种功能还是这是array_replace_recursive中的错误?

谢谢。

2 个答案:

答案 0 :(得分:2)

而不是array_replace_recursive,您需要简单的array_replace作为

$a = ['elm1' => 1, 'elm2' => []];
$b = ['elm1' => 2, 'elm2' => [3]];
$c = array_replace($b, $a);
print_r($c);//['elm1' => 1, 'elm2' => []]

Fiddle

答案 1 :(得分:0)

使用array_merge(): -

<?php

$a = ['elm1' => 1, 'elm2' => []];
$b = ['elm1' => 2, 'elm2' => [3]];
$c = array_merge($b,$a); //it will check the indexes in both array and if indexes are same then first array value will remain on that index second will replace. 

echo "<pre/>";print_r($c);
?>

输出: - https://eval.in/395368