So I have this kind of project to have post divided into several provinces. I have two multidimensional array with table1 and table2, I have been trying to echo it with foreach function and etc but still error. This is my array :
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" } } } } }
I don’t have any idea how to retrieve more than 2 table in my array with. So I repeat again I want to echo this 2 table the 1st province had 2 news and 2nd province also had 2 news, What I want to do is echo this 2 news sorting with province.
Ps. This is my code to show arrays output
<?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++; }
Thanks For helping guys.
Advertisement
Answer
update below code it will work for you..
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++; }
Here i have removed extra [$b]
from $output
array.
Now copy below code to print array.
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>'; } }