0

我有以下数组:

$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 将是一个不同的变量源,而不是在这个数组键中设置它)。

如果有人可以帮助解释我在这两条语句中做错了什么,foreach我将不胜感激更好的理解。

亲切的问候

4

1 回答 1

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

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

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

于 2013-02-08T10:20:04.237 回答