Tim C
Tim C

Reputation: 5714

PHP store 2 values into 1 array index

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?

Upvotes: 0

Views: 1143

Answers (2)

Fouad Fodail
Fouad Fodail

Reputation: 2643

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>';
}

Upvotes: 2

Hackerman
Hackerman

Reputation: 12305

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" } } }

Upvotes: 0

Related Questions