0

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

1 Answer 1

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

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

Demo: http://codepad.org/bDhBw2aZ

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Guys! My problem was that I did not need foreach at all haha! Works a treat! Cheers - Will accept in 5 mins when it allows me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.