创建循环数组 [英] Creating array of loop

查看:57
本文介绍了创建循环数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,"header"和"mach":

I have 2 tables, "header" and "mach":

标题:

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代码提取数据.我是否应该创建一个包含标题和组匹配项的数组

I use the following PHP code to extract data. Should I create an array that contains the header and the group matches

$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代码:

The problem is in the $groups that returns all date match.(italy,Spain,France) no only groups of header. If you use this line of PHP code:

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

代替:

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

相反,仅返回每个标头的最后一个匹配项(循环).

instead returns only the last match (cycle for) for each header.

我想创建这个数组

['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]

],
[....],
]

我在做什么错了?

推荐答案

我对您的代码没有任何想法,所以我自己重写了它.如果我了解您的目标,那么我将设计一种更精简,更简洁的过程来生成所需的多维数组.

I can't get any mental traction on your code, so I rewrote it myself. If I am understanding your goal, I have crafted a much leaner, cleaner process for generating your desired multidimensional array.

*一些注意事项:

  1. 如果变量只使用一次,就不用麻烦声明它们了;除非这样做可以大大提高可读性.
  2. 在foreach循环中使用可用的数组索引,而不是创建其他增量变量.
  3. $ adeguata_mach_keys $ mach 中的所有行进行搜索,以查找与 header STATO 匹配的 STATO 值>值,并返回键.
  1. Don't bother declaring variables if they will only be used once; unless of course it largely improves readability to do so.
  2. Use available array indexes within foreach loops instead of creating additional incremental variables.
  3. $adeguata_mach_keys does a search of all rows in $mach looking for STATO values that match the header STATO value, and returns the keys.

这是我测试成功的代码:

Here is the code that I tested for success with:

$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);

这是结果(不会意外覆盖子数组):

This is the result (no accidental sub-array overwriting):

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

这篇关于创建循环数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆