简体   繁体   English

用不同的表回显两个多维数组

[英]Echo two multidimensional array with different table

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.我有两个带有 table1 和 table2 的多维数组,我一直试图用 foreach 函数等来回显它,但仍然出错。 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.我不知道如何在我的数组中检索 2 个以上的表。 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.所以我再重复一遍我想echo这个2表,第1个省有2条新闻,第2个省也有2条新闻,我想做的是用省回显这2条新闻。

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.谢谢你的帮助。

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.在这里,我从$output数组中删除了额外的[$b]

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

        }

I'd say you'd need to clean up your array first to make sure you are trying to reference the right object, you seem to have everything duplicated with an index and a key in the array.我会说你需要先清理你的数组以确保你正在尝试引用正确的对象,你似乎用数组中的索引和键复制了所有内容。

[0]=>"123",
["id_news"]=>"123",

[1]=>"1",
["id_province"]=>"1",

[2]=>"News B",
["nm_news"]=>"News B",

Edit编辑

I've broken down your array so you can see what I mean clearly我已经分解了你的数组,所以你可以清楚地看到我的意思

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'
        )
    )
)

尝试

    $c=mysql_query("select * from news where id_province='$b'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM