0

I'm trying to create a sort of multidimensional array which takes 2 values from a database and stores in into 1 index in the array

Example x[0] = Jille, 595

I'm doing this as such

while ($row = mysql_fetch_array($result2)) 
{ 
    $opponents[] = $row['opponents']; 
    $fixId= array($row['fixture_id'] => $opponents) ; //Is this line correct??
}

Then later on in my code I want to use the $fixId array which should hold 2 values per index I do this as such:

foreach($fixid as $id => $oppname){
    echo "<option value=\"$oppname\" >".$oppname;"</option>"; 
}

However it is not working the values $id and $oppname does not have a value or take on some strange value.

What am I doing wrong?

1
  • Why don't you do foreach ($opponents? Commented Oct 3, 2013 at 1:04

2 Answers 2

2

You can do it like that :

while ($row = mysql_fetch_array($result2)) 
{ 
     $opponents[] = array('oppname' => $row['opponents'], 'oppid' => $row['fixture_id']) ; 
}


foreach ($opponents as $opp) {
     echo '<option value="'.$opp['oppid'].'">'.$opp['oppname'].'</option>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. I tried it however the foreach loop has a syntax error. I looked it over but cant find the error, would you mind just giving it a scan. cheerz
Sorry one more question if you dont mind. How do I go about extracting $opp['name'] in the $opposition array on a new page? At the moment the oppid gets passed when the form is submited..Is something like this correct? foreach ($_REQUEST['opponents'] as $opponents){ print $opponents; echo'<br>'; } It only gives me the id though and not the name...
0

Try this:

$fixId = array();
while ($row = mysql_fetch_array($result2)) 
{ 
 $opponents[] = $row['opponents']; 
 $fixId[] = array('fixture_id' => $opponents) ;       
}

I made this test:

<?php
 $fixId = array();

 $opponents[] ="Jille, 595"; 
 $fixId[] = array('fixture_id' => $opponents) ;

 var_dump($fixId);
?>

Showing: array(1) { [0]=> array(1) { ["fixture_id"]=> array(1) { [0]=> string(10) "Jille, 595" } } }

Comments

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.