这些是我的测试变量...
$session = array();
$panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);
$req = array(
array(1,'one','Titre1','Prealable1','NbHeure1','Session1','Credit1'),
array(2,'two','Titre2','Prealable2','NbHeure2','Session2','Credit2'),
array(3,'three','Titre3','Prealable3','NbHeure3','Session3','Credit3'),
array(4,'four','Titre4','Prealable4','NbHeure4','Session4','Credit4'),
array(5,'five','Titre5','Prealable5','NbHeure5','Session5','Credit5'),);
我写的代码不完全依赖你的数据库和数据,因为我不知道它是什么......不知道。切换其中一些代码行以使用您的数据库内容。
// $req = mysql_query("SELECT * FROM cours", $connection); uncomment this line for your code
if ($req!==false)
{
foreach( $req as $i => $resultat ) // testing row, remove for real
// while( $resultat = mysql_fetch_row($req) ) // real row, uncomment for you
{
$compare = $resultat[0];
// we will be using $compare to see if this row
// matches one in $panier.. this needs to be changed
// appropriately. I don't have enough info to define it....
// this needs to be a unique field, or it should be
// it is going to be used like this: if($resultat[$i] == $panier[$j][0])
$result[ $compare ] = $resultat;
}
$hi = 0;
foreach( $panier as $index => $panier_row )
{
$compare = $panier_row[0];
if (isset($result[$compare]))
{
$H = 0;
// change $session to $_SESSION for your code...
$session['Panier']['Id'][$hi][$H] = $result[$compare][0];
$session['Panier']['CodeCours'][$hi][$H] = $result[$compare][1];
$session['Panier']['Titre'][$hi][$H] = $result[$compare][2];
$session['Panier']['Prealable'][$hi][$H] = $result[$compare][3];
$session['Panier']['NbHeure'][$hi][$H] = $result[$compare][4];
$session['Panier']['Session'][$hi][$H] = $result[$compare][5];
$session['Panier']['Credit'][$hi][$H] = $result[$compare][6];
$H++;
$hi++;
}
}
}
我的结果显示$session['Panier']['Credit']
为 1, 2, 3 但不是 4 或 5 因为它们不匹配而具有三样东西$panier
。
最后是什么$session
:
array (
'Panier' =>
array (
'Id' =>
array (
0 =>
array (
0 => 1,
),
1 =>
array (
0 => 2,
),
2 =>
array (
0 => 3,
),
),
'CodeCours' =>
array (
0 =>
array (
0 => 'one',
),
1 =>
array (
0 => 'two',
),
2 =>
array (
0 => 'three',
),
),
'Titre' =>
array (
0 =>
array (
0 => 'Titre1',
),
1 =>
array (
0 => 'Titre2',
),
2 =>
array (
0 => 'Titre3',
),
),
'Prealable' =>
array (
0 =>
array (
0 => 'Prealable1',
),
1 =>
array (
0 => 'Prealable2',
),
2 =>
array (
0 => 'Prealable3',
),
),
'NbHeure' =>
array (
0 =>
array (
0 => 'NbHeure1',
),
1 =>
array (
0 => 'NbHeure2',
),
2 =>
array (
0 => 'NbHeure3',
),
),
'Session' =>
array (
0 =>
array (
0 => 'Session1',
),
1 =>
array (
0 => 'Session2',
),
2 =>
array (
0 => 'Session3',
),
),
'Credit' =>
array (
0 =>
array (
0 => 'Credit1',
),
1 =>
array (
0 => 'Credit2',
),
2 =>
array (
0 => 'Credit3',
),
),
),
)
不过,就个人而言……您应该mysql_fetch_assoc
像这样使用和执行它:
$session = array();
$panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);
$req = array(
array('id'=>1,'var'=>'one','Titre'=>'Titre1','Prealable'=>'Prealable1','NbHeure'=>'NbHeure1','Session'=>'Session1','Credit'=>'Credit1'),
array('id'=>2,'var'=>'two','Titre'=>'Titre2','Prealable'=>'Prealable2','NbHeure'=>'NbHeure2','Session'=>'Session2','Credit'=>'Credit2'),
array('id'=>3,'var'=>'thr','Titre'=>'Titre3','Prealable'=>'Prealable3','NbHeure'=>'NbHeure3','Session'=>'Session3','Credit'=>'Credit3'),
array('id'=>4,'var'=>'fou','Titre'=>'Titre4','Prealable'=>'Prealable4','NbHeure'=>'NbHeure4','Session'=>'Session4','Credit'=>'Credit4'),
array('id'=>5,'var'=>'fiv','Titre'=>'Titre5','Prealable'=>'Prealable5','NbHeure'=>'NbHeure5','Session'=>'Session5','Credit'=>'Credit5'),);
// $req = mysql_query("SELECT * FROM cours", $connection);
if ($req!==false)
{
foreach( $req as $i => $resultat ) // testing row, remove for real
// while( $resultat = mysql_fetch_assoc($req) ) // real row, uncomment for you
// mysql_fetch_assoc !!
{
$compare = $resultat['id'];
// we will be using $compare to see if this row
// matches one in $panier.. this needs to be changed
// appropriately. I don't have enough info to define it....
// this needs to be a unique field, or it should be
$result[ $compare ] = $resultat;
}
foreach( $panier as $index => $panier_row )
{
$compare = $panier_row[0];
if (isset($result[$compare]))
{
$session[] = $result[$compare];
}
}
}
这是$session
array (
0 =>
array (
'id' => 1,
'var' => 'one',
'Titre' => 'Titre1',
'Prealable' => 'Prealable1',
'NbHeure' => 'NbHeure1',
'Session' => 'Session1',
'Credit' => 'Credit1',
),
1 =>
array (
'id' => 2,
'var' => 'two',
'Titre' => 'Titre2',
'Prealable' => 'Prealable2',
'NbHeure' => 'NbHeure2',
'Session' => 'Session2',
'Credit' => 'Credit2',
),
2 =>
array (
'id' => 3,
'var' => 'thr',
'Titre' => 'Titre3',
'Prealable' => 'Prealable3',
'NbHeure' => 'NbHeure3',
'Session' => 'Session3',
'Credit' => 'Credit3',
),
)