Reputation: 362
I have the following arrays:
$doc_id = (3,5,4,6)
$requirement2 = (2,0,0,1)
I am trying to make $doc_id
the key element of $requirement2
using the following code:
<?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";
?>
However I receive the following on the 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 )
Which is the closest I have been so far. But obviously still wrong.
From what I have read I need it to look like this:
Array ( [3] => 2 [5] => 0 [4] => 0 [6] => 1 )
So that later on I can call it by $requirement3[$doc_id]
. (the $doc_id will be a different variable source when calling it than from setting it within this array key).
If someone could help explain what I am doing wrong with the 2 foreach
statements I would greatly appreciate the better understanding.
Kind Regards
Upvotes: 0
Views: 77
Reputation: 4356
$requirement3 = array_combine($doc_id,$requirement2);
http://www.php.net/manual/en/function.array-combine.php
Demo: http://codepad.org/bDhBw2aZ
Upvotes: 2