合并两个具有不同值的不同数组

时间:2019-08-23 10:27:43

标签: php arrays php-7

我有下面这样的数组

 public static void deleteDatabaseFile(Context context, String databaseName) {


    File databases = new File(context.getApplicationInfo().dataDir + "/databases");
    File db = new File(databases, databaseName);
    if (db.delete()) {
        Toast.makeText(context, "Database deleted", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(context, "Failed to delete database", Toast.LENGTH_SHORT).show();
    }
    File journal = new File(databases, databaseName + "-journal");
    if (journal.exists()) {
        if (journal.delete()) 
           Toast.makeText(context, "Database journal deleted", Toast.LENGTH_SHORT).show();
        }
        else {
            System.out.println("Failed to delete database journal");
        }
    }

    File cache = context.getCacheDir();
    File appDir = new File(cache.getParent());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            if (!s.equals("lib")) {
                deleteDir(new File(appDir, s));
                Log.i("EEEEEERRRRRROOOOOOORRRR", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
            }
        }
    }

}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        int i = 0;
        while (i < children.length) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
            i++;
        }
    }

    assert dir != null;
    return dir.delete();
}

如何合并$ foo和$ bar,使输出像这样

$foo = [
    a => 0,
    b => 0,
    c => 0,
    d => 0
];

$bar = [
    c => 3,
]

我尝试了array_diff和array_intersect,但没有得到任何结果。

3 个答案:

答案 0 :(得分:1)

请参见array-replace功能。以下代码段应该起作用:

<?php
$foo = [
    a => 0,
    b => 0,
    c => 0,
    d => 0
];

$bar = [
    c => 3,
];

$merged = array_replace($foo, $bar);
print_r($merged);
?>

输出:

Array ( [a] => 0 [b] => 0 [c] => 3 [d] => 0 )

答案 1 :(得分:0)

array_replace_recursive($ foo,$ bar);

答案 2 :(得分:0)

user_passord.yml

输出

$merge=array_merge($foo,$baar);


print_r($merge);



第二个参数覆盖第一个参数