创建循环数组

时间:2017-03-02 22:27:59

标签: php arrays multidimensional-array

我有两张桌子,“标题”和“马赫”:

头:

STATO |ID |CAMPIONATO
Italy |5  |Serie A
Spain |1  |Primera Division
France|2  |Coppe de France

马赫:

STATO |home     |away     |scoreH|scoreA|
Italy |juve     |Milan    |0     |0     |
Italy |Lazio    |Roma     |0     |1     |
Spain |Deportivo|Bilbao   |1     |0     |
Spain |A Madrid |Sevilla  |1     |2     |
France|Lille    |Perigord |0     |0     |

我使用以下PHP代码来提取数据。我应该创建一个包含标题的数组,并且组匹配

$num=0;
$mach=-1;
foreach($first as $row2){  // cycling once

           foreach($header as $row){   //    cycles x times

                    ....        //Here recover the header data
                    $stato;
                    $id;
                    $campionato



                        for($i=0;$i<$count;++$i){   //   in $count know how many times I have to do the cycle for each header (stato)
                        $mach=$mach+1;
                           ....            // Here recover the mach data
                           $home;
                           $away;
                           $scoreH;
                           $scoreA;

                        $info[$mach]= array('home'=>$home,'away'=>$away,'scoreH'=>$scoreH,'scoreA'=>$scoreA);
                        $groups[$num]=array($info[$mach]);
                        }//end for cycles

                        $headerArray[$num]=array('header'=>['stato'=>$stato,'id'=>$id,'campionato'=>$campionato],'mach'=>$groups[$um]);
            $num++;
           } //end header cycles

                       $blockArray[]=array('incontri'=>$headerArray);

} //end first cycle
                       print_r($blockArray);

问题在于返回所有日期匹配的$ groups。(意大利,西班牙,法国)不仅仅是标题组。 如果你使用这行PHP代码:

array_push ($groups,$info[$mach]);

而不是:

$Groups[$num] = array($info[$mach]);

只返回每个标题的最后一个匹配(循环)。

我想创建这个数组

['incontri'=>
[
header=>
    ['stato'=>Italy,'id'=>5,'campionato'=>Serie A],

mach=>
    ['home'=>Juve,'away'=>Milan,'scoreH'=>0,'scoreA'=>0],
    ['home'=>Lazio,'away'=>Roma,'scoreH'=>0,'scoreA'=>0]

],
[....],
]

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我无法对你的代码产生任何精神上的牵引力,所以我自己重写了一遍。 如果我理解你的目标,我已经精心设计了一个更精简,更清晰的过程来生成你想要的多维数组。

*有些注意事项:

  1. 如果变量只使用一次,请不要理会变量;除非它在很大程度上提高了可读性。
  2. 在foreach循环中使用可用的数组索引,而不是创建其他增量变量。
  3. $adeguata_mach_keys搜索$mach中查找与STATO值匹配的header STATO值的所有行,并返回键。
  4. 以下是我测试成功的代码:

    $first=array();  // only cycles once, so don't bother looping it, just call it by key (I am assuming 0, but that may be wrong)
    $first[0]=array(
        array("STATO"=>"Italy","ID"=>5,"CAMPIONATO"=>"Serie A"),
        array("STATO"=>"Spain","ID"=>1,"CAMPIONATO"=>"Primera Division"),
        array("STATO"=>"France","ID"=>1,"CAMPIONATO"=>"Coppe de France")
    );
    $mach=array(
        array("STATO"=>"Italy","home"=>"juve","away"=>"Milan","scoreH"=>"0","scoreA"=>"0"),
        array("STATO"=>"Italy","home"=>"Lazio","away"=>"Roma","scoreH"=>"0","scoreA"=>"1"),
        array("STATO"=>"Spain","home"=>"Deportivo","away"=>"Bilbao","scoreH"=>"1","scoreA"=>"0"),
        array("STATO"=>"Spain","home"=>"A Madrid","away"=>"Sevilla","scoreH"=>"1","scoreA"=>"2"),
        array("STATO"=>"Spain","home"=>"Lille","away"=>"Perigord","scoreH"=>"0","scoreA"=>"0")
    );
    
    foreach($first[0] as $header_index=>$header_row){
        foreach($header_row as $header_row_key=>$header_row_val){
            $result[$header_index]["header"][$header_row_key]=$header_row_val;
            // identify which mach rows hold matching STATO values
            $adeguata_mach_keys=array_keys(array_intersect(array_column($mach,"STATO"),array($header_row["STATO"])));
            foreach($adeguata_mach_keys as $mach_index=>$mach_key){
                foreach($mach[$mach_key] as $mach_row_key=>$mach_row_val){
                    $result[$header_index]["mach"][$mach_index][$mach_row_key]=$mach_row_val;
                }
            }
        }
    }
    $result=array("incontri"=>$result);  // I wouldn't do this, but if it is necessary for your case, fine.
    print_r($result);
    

    这是结果(没有意外的子阵列覆盖):

    array (
        'incontri' => array (
            0 => array (
                'header' => array (
                    'STATO' => 'Italy',
                    'ID' => 5,
                    'CAMPIONATO' => 'Serie A',
                ),
                'mach' => array (
                    0 => array (
                        'STATO' => 'Italy',
                        'home' => 'juve',
                        'away' => 'Milan',
                        'scoreH' => '0',
                        'scoreA' => '0',
                    ),
                    1 => array (
                        'STATO' => 'Italy',
                        'home' => 'Lazio',
                        'away' => 'Roma',
                        'scoreH' => '0',
                        'scoreA' => '1',
                    ),
                ),
            ),
            1 => array (
                'header' => array (
                    'STATO' => 'Spain',
                    'ID' => 1,
                    'CAMPIONATO' => 'Primera Division',
                ),
                'mach' => array (
                    0 => array (
                        'STATO' => 'Spain',
                        'home' => 'Deportivo',
                        'away' => 'Bilbao',
                        'scoreH' => '1',
                        'scoreA' => '0',
                    ),
                    1 => array (
                        'STATO' => 'Spain',
                        'home' => 'A Madrid',
                        'away' => 'Sevilla',
                        'scoreH' => '1',
                        'scoreA' => '2',
                    ),
                ),
            ),
            2 => array (
                'header' => array (
                    'STATO' => 'France',
                    'ID' => 1,
                    'CAMPIONATO' => 'Coppe de France',
                ),
                'mach' => array (
                    0 => array (
                        'STATO' => 'France',
                        'home' => 'Lille',
                        'away' => 'Perigord',
                        'scoreH' => '0',
                        'scoreA' => '0',
                    ),
                ),
            ),
        ),
    )