因此,我有将这类项目分成几个省的项目。我有两个多维数组与table1和table2,我一直试图用foreach函数等回显它,但仍然出错。这是我的数组:
array(2) {
[0]=>
array(1) {
[1]=>
array(2) {
["table1"]=>
array(12) {
[0]=>
string(1) "1"
["id_province"]=>
string(1) "1"
[1]=>
string(13) "Province A"
["nm_province"]=>
string(13) "Province A"
}
["table2"]=>
array(2) {
[0]=>
array(58) {
[0]=>
string(2) "43"
["id_news"]=>
string(2) "43"
[1]=>
string(1) "1"
["id_province"]=>
string(1) "1"
[2]=>
string(23) "News A"
["nm_news"]=>
string(23) "News A"
}
[1]=>
array(58) {
[0]=>
string(3) "123"
["id_news"]=>
string(3) "123"
[1]=>
string(1) "1"
["id_province"]=>
string(1) "1"
[2]=>
string(21) "News B"
["nm_news"]=>
string(21) "News B"
}
}
}
}
[1]=>
array(1) {
[2]=>
array(2) {
["table1"]=>
array(12) {
[0]=>
string(1) "2"
["id_province"]=>
string(1) "2"
[1]=>
string(23) "Province B"
["nm_province"]=>
string(23) "Province B"
}
["table2"]=>
array(2) {
[0]=>
array(58) {
[0]=>
string(2) "44"
["id_news"]=>
string(2) "44"
[2]=>
string(1) "2"
["id_province"]=>
string(1) "2"
[5]=>
string(24) "News A Province B"
["nm_news"]=>
string(24) "News A Province B"
}
[1]=>
array(58) {
[0]=>
string(3) "127"
["id_news"]=>
string(3) "127"
[2]=>
string(1) "2"
["id_province"]=>
string(1) "2"
[5]=>
string(13) "News B Province B"
["nm_news"]=>
string(13) "News B Province B"
}
}
}
}
}
我不知道如何在数组中检索2个以上的表。所以我再说一遍,我想在第一张省有2条新闻和第二张省也有2条新闻回显这2个表,我要做的是在省份中回显这2条新闻。
Ps。这是我的代码,用于显示数组输出
<?php
$a=mysql_query("select * from province");
while($m1=mysql_fetch_array($a)){
$result[]=$m1;
}
$output=[];
$i=0;
foreach($result as $r){
$b=$r['id_province'];
$c=mysql_query("select * from news where id_province=".$b);
$output[$i][$b]['table1']=$r;
$dummy=[];
while($response = mysql_fetch_array($c)){
$dummy[] = $response;
}
$output[$i][$b]['table2']=$dummy;
$i++;
}
感谢您的帮助。
答案 0 :(得分:1)
更新以下代码,它将为您服务。
foreach($result as $r){
$b=$r['id_province'];
$c=mysql_query("select * from news where id_province=".$b);
$output[$i]['table1']=$r;
$dummy=[];
while($response = mysql_fetch_array($c)){
$dummy[] = $response;
}
$output[$i]['table2']=$dummy;
$i++;
}
在这里,我从[$b]
数组中删除了另外的$output
。
现在复制下面的代码以打印数组。
foreach($output as $out){
$table1= $out['table1'];
$table2= $out['table2'];
echo $table1['nm_provinsi'].'<br>';
echo 'Data from table 2';
foreach($table2 as $tab2){
echo "\t".$tab2['nm_berita'].'<br>';
}
}
答案 1 :(得分:0)
我想说,您需要先清理数组,以确保尝试引用正确的对象,似乎所有内容都重复了,并带有索引和数组中的键。
[0]=>"123",
["id_news"]=>"123",
[1]=>"1",
["id_province"]=>"1",
[2]=>"News B",
["nm_news"]=>"News B",
// -----编辑----- //
我已经分解了数组,以便您可以清楚地理解我的意思
array(
'table1' => array(
'0' => 1,
'id_province' => 1,
'1' => 'Province A',
'nm_province' => 'Province A'
),
'table2' => array(
array(
'0' => '43',
'id_news' => '43',
'1' => '1',
'id_province' => '1',
'2' => 'News A',
'nm_news' => 'News A'
),
array(
'0' => '123',
'id_news' => '123',
'1' => 1,
'id_province' => '1',
'2' => 'New B',
'nm_news' => 'News B')
)
),
array(
'table1' => array(
'0' => "2",
'id_province' => "2",
'1' => "Province B",
'nm_province' => 'Province B'
),
'table2' => array(
array(
'0'=>'44',
'id_news' => '44',
'2' => '2',
'id_province' => '2',
'5' => 'News A Province B',
'nm_news' => 'News A Province B'
),
array(
'0'=>'127',
'id_news' => '127',
'2'=>'2',
'id_province' => '2',
'5' => 'News B Province B',
'nm_news' => 'News B Province B'
)
)
)
答案 2 :(得分:-1)
尝试
$c=mysql_query("select * from news where id_province='$b'");