foreach多个数组和由每个数组上的ID值确定的组值



我试图使用foreach循环来循环多个数组,并将值分为2组,由每个数组上的ID值确定,然后回显它们,最终到一个表。

例如,我有一个给定的二维数组如下:(我只使用了3个作为例子,但现实中会有更多)

Array
(
    [results] => Array
        (
            [numbpeople] => 3
            [people] => Array
                (
                    [0] => Array
                        (
                            [name] => bert
                            [mainId] => 2
                            [beens] => 0
                            [coins] => 8
                            [total] => 3
                            [ammount] => 2
                            [place] => 15
                            [type] => 0
                        )
                    [1] => Array
                        (
                            [name] => bungle
                            [mainId] => 1
                            [beens] => 0
                            [coins] => 4
                            [total] => 0
                            [ammount] => 10
                            [place] => 13
                            [type] => 0
                        )
                    [2] => Array
                        (
                            [name] => fred
                            [mainId] => 2
                            [beens] => 0
                            [coins] => 3
                            [total] => 1
                            [ammount] => 8
                            [place] => 11
                            [type] => 0
                        )
                  )
            )
      )

我尝试了下面的代码,但它没有按预期的方式工作。

//foreach ($data['results']['people'] as $pValue) {
foreach ($data['results']['people'] as $Key => $pValue) {
$numPeople = $data["results"]["numbpeople"]; //[$i];
    if($numPeople>-1)
    {
        $MainId1="";
        $MainId2="";
        // set up header
        echo "<strong>People:<br>name, beens, coins, total.</strong><br>";
        echo "<br>";
        // calculate who's got what?
        $i=0;       
        while($i<$numPeople)
        {
                $pname[$i]=$pValue["name"][$i];
                $pmainId[$i]=$pValue["mainId"][$i];
                $pbeens[$i]=$pValue["beens"][$i];
                $pcoins[$i]=$pValue["coins"][$i];
                $ptotal[$i]=$pValue["total"][$i];
                if(!stristr($pValue["mainId"],"1") && !stristr($pValue["mainId"],"2")) {

                    if($pmainId[$i]!=$MainId1 && $MainId1=="") {
                        $p1Name=$pname[$i];         // people1 name
                        $p1Beens=$pbeens[$i];       // people1 beens
                        $p1Coins=$pcoins[$i];       // people1 coins
                        $p1Total=$ptotal[$i];       // people1 total
                    }
                    if($pmainId[$i]!=$MainId1 && $MainId1!="" && $MainId2=="") {
                        $p2Name=$pname[$i];         // people2 name
                        $p2Beens=$pbeens[$i];       // people2 beens
                        $p2Coins=$pcoins[$i];       // people2 coins
                        $p2Total=$ptotal[$i];       // people2 total
                    }   
                    // Id
                    if($pmainId[$i]!=$MainId1 && $MainId1=="") {
                        $MainId1=$pmainId[$i];
                    }
                    if($pmainId[$i]!=$MainId1 && $MainId1!="" && $MainId2=="") {
                        $MainId2=$pmainId[$i];
                    }       
                } else {
                    $MainId1="1";
                    $MainId2="2";
                }
            // output people ?
                echo $i.". -> ".$p1Name[$i].", ".$p1Beens[$i].", ".$p1Coins[$i].", ".$p1Total[$i];
                //    1            bungle             0                 4                 0
                echo "<br>";
                echo $i.". -> ".$p2Name[$i].", ".$p2Beens[$i].", ".$p2Coins[$i].", ".$p2Total[$i];
                //    1             bert              0                 8                 3
                //    2             fred              0                 3                 1
                echo "<br>";
                echo $MainId1, $MainId2;
                //       1         2
                echo "<br>";

            $i++;
        }
                }else   {
                echo "No people";
        }
    }

查看代码后的一些建议:

  • 在发送数据到视图之前准备数据
  • 下一步尝试从视图中分离逻辑

我的答案显示了如何在数组中通过id:

分隔数组。
$people=$results["results"]["people"];
$result=[];
$count=count($people);// i don't need special numbpeople in array
for ($i=0; $i<$count; $i++){
  $id=$people[$i]['mainId'];
  if (!isset($result[$id]))
  $result[$id]=[];//create new array for this id
  $result[$id][]=$people[$i]; //add people to this collection
}

在上面的结果循环之后,我们有了这样的结构:

[ 
1 => [ [peopley],[peoplex] ]
2 => [ [peoplez],[peoplew] ]
....
]

该结构按mainId分组。

如何显示来自上层结构的人员:

$size=count($result)
for ($i=0;$i<$size; $i++){
    //loop on groups
    $peoples=$result[$i];//array with people in this group
    $groupSize=count($peoples);
    for ($j=0;$j<$groupSize; $j++){
      //loop in group
      echo $peoples[$j]["name"];//example show name
    }
}

相关内容

  • 没有找到相关文章

最新更新