分配给逗号数组的自定义键逗号数组

时间:2013-02-08 10:14:00

标签: php arrays foreach

我有以下数组:

$doc_id = (3,5,4,6)
$requirement2 = (2,0,0,1)

我正在尝试使用以下代码将$doc_id作为$requirement2的关键元素:

<?php

include "../includes/auth.php";
include "../includes/header.php";


$jat_id = intval($_GET['id']);
$div_id = intval($_GET['div_id']);


    $order = "SELECT * FROM document_assoc WHERE jat_id = '$jat_id'";
    $result = mysql_query($order);
    $row = mysql_fetch_array($result);

    $doc_id3 = $row['doc_id'];
    $doc_id = explode(",", $doc_id3);

    $requirement = $row['requirement'];
    $requirement2 = explode(",", $requirement);

    foreach ($doc_id as $doc_id4) {
    $requirement3 = array($doc_id4=>$requirement3);

    foreach ($requirement2 as $requirement3) {
    $requirement3 = array($doc_id4=>$requirement3);

    print_r ($requirement3);

    }}
include "../includes/footer.php";

?>

但是我在print_r()上收到以下内容:

Array ( [3] => 2 ) Array ( [3] => 0 ) Array ( [3] => 0 ) Array ( [3] => 1 ) Array ( [5] => 2 ) Array ( [5] => 0 ) Array ( [5] => 0 ) Array ( [5] => 1 ) Array ( [4] => 2 ) Array ( [4] => 0 ) Array ( [4] => 0 ) Array ( [4] => 1 ) Array ( [6] => 2 ) Array ( [6] => 0 ) Array ( [6] => 0 ) Array ( [6] => 1 ) 

到目前为止,我的距离最近。但显然还是错的。

根据我的阅读,我需要它看起来像这样:

Array ( [3] => 2 [5] => 0 [4] => 0 [6] => 1 ) 

以后我可以通过$requirement3[$doc_id]来调用它。 ($ doc_id在调用它时将是一个不同的变量源,而不是在此数组键中设置它。)

如果有人可以帮助解释我在2 foreach语句中做错了什么,我将非常感谢您更好地理解。

亲切的问候

1 个答案:

答案 0 :(得分:2)

$requirement3 = array_combine($doc_id,$requirement2);

http://www.php.net/manual/en/function.array-combine.php

演示:http://codepad.org/bDhBw2aZ