如何在php中为每个循环组合并设置两个数组?

时间:2015-08-29 13:03:57

标签: php algorithm foreach logic

我有两个数组,一个是键,第二个是值。我想在foreach循环中结合在一起,但我没能创建逻辑。请看代码我希望你能理解我到底想要什么?

$keys = Array ( [0] => name [1] => qualification [2] => Major Subject [3] => Matric [4] => Conferred Date: [5] => Attendance From: [6] => Attendance To: [7] => AK [8] => AK [9] => AK ) 
$values = Array ( [0] => ayaz [1] => matric [2] => Chemistry [3] => Olevel [4] => 2015-08-12 [5] => 2015-08-22 [6] => 2015-08-14 [7] => AK [8] => AK [9] => AK )

我想将它组合在foreach循环中并设置为键值。所以请指导我怎么做。谢谢你提前。

我试过这个但未能获得更好的结果

foreach(array_map(null, $savvion_key, $savvion_value) as $combined) {
            print_r($combined);
        }

2 个答案:

答案 0 :(得分:0)

试试这种方式

foreach ($keys as k => v ) {
    print_r( v . ' ' . $value[k]);
}

答案 1 :(得分:0)

<强>解决方法1:

File recordingFile = new File(recording.getFilePath());
Uri fileUri = Uri.fromFile(recordingFile);
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("audio/mp3");
i.putExtra(Intent.EXTRA_STREAM, fileUri);
try {
    startActivity(Intent.createChooser(i, "Share " + recording.getName()));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(this, "There are no app installed to share your audio file.", Toast.LENGTH_SHORT).show();
}

<强>溶液2:

function array_merge_keys($ray1, $ray2) {
    $keys = array_merge(array_keys($ray1), array_keys($ray2));
    $vals = array_merge($ray1, $ray2);
    return array_combine($keys, $vals);
}

以上示例将输出:

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>