使用1个密钥PHP将2个变量推入数组

时间:2016-04-12 21:49:39

标签: php arrays file path key

我试图将2个变量推入数组,但我希望密钥是相同的。

下面的代码是一个搜索功能,通过一个文件夹。 在foreach中,我正在检查名称或名称的一部分是否与搜索词匹配。如果有结果,我将文件名和文件路径放在数组中。

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

defaultConfig {
    applicationId "com.myapp"
    minSdkVersion 21
    targetSdkVersion 22
    versionName "12-APR-2016"
    versionCode 12042016
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/MANIFEST.MF'
    exclude 'META-INF/NOTICE'
}

lintOptions {
    checkReleaseBuilds false
    // Or, if you prefer, you can continue to check for errors in release builds,
    // but continue the build even when errors are found:
    abortOnError false
}

buildTypes {
    debug {
        buildConfigField "String", "SERVER_URL", "\"https://dev.myapp.com/?action=\""

        applicationIdSuffix ".debug"
        minifyEnabled = false
        proguardFiles += file('proguard-rules.pro')
    }
    release {
        buildConfigField "String", "SERVER_URL", "\"https://release.myapp.com/?action=\""
        applicationIdSuffix ".release"
        minifyEnabled = false
        proguardFiles += file('proguard-rules.pro')
    }

}

productFlavors {
    x86 {
        applicationId "com.myapp"
        versionCode Integer.parseInt("6" + defaultConfig.versionCode)
        ndk {
            abiFilter "x86"
        }
    }
    mips {
        applicationId "com.myapp"
        versionCode Integer.parseInt("4" + defaultConfig.versionCode)
        ndk {
            abiFilter "mips"
        }
    }
    armv7 {
        applicationId "com.myapp"
        versionCode Integer.parseInt("2" + defaultConfig.versionCode)
        ndk {
            abiFilter "armeabi-v7a"
        }
    }
    arm {
        applicationId "com.myapp"
        versionCode Integer.parseInt("1" + defaultConfig.versionCode)
        ndk {
            abiFilter "armeabi"
        }
    }
}

splits {
    abi {
        enable true // enable ABI split feature to create one APK per ABI
        universalApk true //generate an additional APK that targets all the ABIs
    }
}
// map for the version code
project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]

android.applicationVariants.all { variant ->
    // assign different version code for each output
    variant.outputs.each { output ->
        output.versionCodeOverride =
                project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
    }
}
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile group: 'com.android.support', name: 'appcompat-v7', version: '23+'
    //omitted for brevity
}

当我运行代码时,它会返回以下内容:

protected function search()
    {

        $keyword = $this->strKeyword;

        $foundResults = array();

        $dir_iterator = new RecursiveDirectoryIterator(TL_ROOT."/tl_files/");
        $iterator = new RecursiveIteratorIterator($dir_iterator,
            RecursiveIteratorIterator::SELF_FIRST);

        foreach ($iterator as $splFile) {
            if ($splFile->getBaseName() == $keyword) {
                array_push($foundResults, $splFile->getBaseName(), $splFile->getPathName());
            }
            elseif(stripos($splFile->getBaseName(), $keyword) >= 3){
                array_push($foundResults, $splFile->getBaseName(), $splFile->getPathName());
            }
        }

        return $foundResults;
    }

如您所见,他为文件名和文件路径设置了一个新密钥

但我想要的是:

[0] => FileName Output 1
[1] => FilePath Output 1
[2] => FileName Output 2
[3] => FilePath Output 2

我希望它有点清楚,有人可以帮助我。

格尔茨

2 个答案:

答案 0 :(得分:1)

我想你需要这样的东西:

$foundResults[] = array(
    'fileName' => $splFile->getBaseName(),
    'pathName' => $splFile->getPathName());

答案 1 :(得分:0)

您可以推送包含键值对的数组,而不是值:

array_push($foundResults,
    array(
        'fileName' => $splFile->getBaseName(),
        'filePath' => $splFile->getPathName()
    )
);