PHP将2个值存储到1个数组索引中

时间:2021-04-30 22:29:27

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

我正在尝试创建一种多维数组,它从数据库中获取2个值并存储到数组中的1个索引中

Example x[0] = Jille, 595

例子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:

然后在我的代码中我想使用$ fixId数组,它应该为每个索引保存2个值,我这样做:

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.

但是它没有工作值$ id和$ oppname没有值或取一些奇怪的值。

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


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

#2


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

显示:array(1){[0] => array(1){[“fixture_id”] => array(1){[0] => string(10)“Jille,595”}}}

#1


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

#2


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

显示:array(1){[0] => array(1){[“fixture_id”] => array(1){[0] => string(10)“Jille,595”}}}