dtn36013
2014-09-08 18:41
浏览 93
已采纳

PHP多维数组 - 在循环中添加其他记录

I am trying to populate a multidimensional array while looping through a regular array, but I am finding my code is overwriting each record as opposed to adding a new key. here is a condensed version of what i am working on - $AdjustePerWeekSorted is the regular array

$CSVKey = 0;
foreach ($AdjustedPerWeekSorted as $item) {
$data = array (
            $CSVKey => 
                array (
                'Overall Rank' => (array_search($item,$AllAdjustedPerWeekSorted) + 1),
                'Name' => $AdjustedNames[array_search($item,$AdjustedPerWeek)],
                'Adjusted Positional Score' => $item,
                'Position' => $AdjustedPOS[array_search($item,$AdjustedPerWeek)]
                ),      
            );
        $CSVKey = $CSVKey + 1;              
}   

my thought process is this: for $AdjustedPerWeekSorted[0] -> populate $data[$CSVKey] = MultiDarray add 1 to $CSVKey loop to next record

for $AdjustedPerWeekSorted[1] -> populate $data[$CSVKey + 1] = MultiDarray and so on...

all of my post processing of this is working fine, however I am only populating the $data array with the LAST record's data in the $AdjustedPerWeekSorted array. Anybody willing to point me in the correct direction? any help would be greatly appreciated.

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我试图在循环遍历常规数组时填充多维数组,但我发现我的代码覆盖了每个 记录而不是添加新密钥。 这是我正在处理的精简版本 - $ AdjustePerWeekSorted是常规数组

  $ CSVKey = 0; 
foreach($ AdjustedPerWeekSorted as $ item){
 $  data = array(
 $ CSVKey => 
 array(
'整体排名'=>(array_search($ item,$ AllAdjustedPerWeekSorted)+ 1),
'名称'=> $ AdjustedNames [array_search(  $ item,$ AdjustedPerWeek)],
'调整后的位置分数'=> $ item,
'位置'=> $ AdjustedPOS [array_search($ item,$ AdjustedPerWeek)] 
),
); \  n $ CSVKey = $ CSVKey + 1;  
} 
   
 
 

我的思维过程是: $ $ AdjustedPerWeekSorted [0] - > 填充$ data [$ CSVKey] = MultiDarray 将$ CSVKey 循环加1到下一条记录

for $ AdjustedPerWeekSorted [1] - > 填充$ data [$ CSVKey + 1] = MultiDarray等...

我对此的所有后期处理工作正常,但是我只填充$ data数组 $ AdjustedPerWeekSorted数组中的最后记录数据。 有人愿意指出我正确的方向吗? 任何帮助将不胜感激。

谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanlaiyin2356 2014-09-08 18:44
    已采纳

    This will build an array using $CSVKey as the index instead of overwriting:

    $data[$CSVKey] = array (
                    'Overall Rank' => (array_search($item,$AllAdjustedPerWeekSorted) + 1),
                    'Name' => $AdjustedNames[array_search($item,$AdjustedPerWeek)],
                    'Adjusted Positional Score' => $item,
                    'Position' => $AdjustedPOS[array_search($item,$AdjustedPerWeek)]
                    );
    
    打赏 评论
  • douxin2002 2014-09-08 18:56

    You coud simple do it this way:

    <?php
    
    
    $data = array();    
    foreach ($AdjustedPerWeekSorted as $item) {
        $data[]
            = array(
            'Overall Rank'              => (array_search(
                    $item,
                    $AllAdjustedPerWeekSorted
                ) + 1),
            'Name'                      => $AdjustedNames[array_search(
                $item,
                $AdjustedPerWeek
            )],
            'Adjusted Positional Score' => $item,
            'Position'                  => $AdjustedPOS[array_search(
                $item,
                $AdjustedPerWeek
            )]
    
        );
    
    }   
    

    If you create Integer keys array and starts from 0 there's no need to use $CSVKey

    打赏 评论

相关推荐 更多相似问题