【问题标题】:Add key and variable to multidimensional Array based on conditions根据条件将键和变量添加到多维数组
【发布时间】:2013-09-05 13:53:57
【问题描述】:

我想知道是否可以根据某些条件向数组添加键和值。 这段脚本发出一个 api 调用,以根据一个团队 ID 号从多个团队中检索运动结果。

$length = $numberofTeams
for ($i = 0; $i < $length; $i++) {
    $teamID             = $objTeamID[$i]['Teamid'];
    $teamResults        = 'http://api.com/teamresults/' . $Teamid;
    $dataResults        = file_get_contents($teamResults);
    $objResults[]   = json_decode($dataResults, true);
}

结果是一个具有这种结构的数组:

Array ( 
    [0] => Array ( 
            [errorcode] => 9995 
            [message] => No results
     ) 
    [1] => Array ( 
            [errorcode] => 1000 
            [message] => Ok, Schedule follows 
            [List] => Array ( 
                        [0] => Array ( 
                            [MatchID] => 7683403 
                            [Number] => 630 
                            [Result] => 2 - 1 
                            [Datum] => 2013-08-27 
                            [Tijd] => 2000 
                            [CompType] => B )
                        [1] => Array ( 
                            [MatchID] => 7683403 
                            [Number] => 630 
                            [Result] => 4 - 0                               [Datum] => 2013-08-27 
                            [Tijd] => 2000 
                            [CompType] => B ) 
                            )
                ) 
    [2] => Array ( 
    [errorcode] => 9995 
    [message] => No results )
)

在将它保存到 MySql 数据库之前,为了以后使用,我需要将 teamID 变量添加到每个结果中,这样它就会变成:

Array ( 
    [0] => Array ( 
            [errorcode] => 9995 
            [message] => No results
     ) 
    [1] => Array ( 
            [errorcode] => 1000 
            [message] => Ok, Schedule follows 
            [List] => Array ( 
                        [0] => Array ( 
                            [teamID] => 'value from $teamID'
                            [MatchID] => 7683403 
                            [Number] => 630 
                            [Result] => 2 - 1
                            [Datum] => 2013-08-27 
                            [Tijd] => 2000 
                            [CompType] => B )
                        [1] => Array ( 
                            [teamID] => 'value from $teamID'                                [MatchID] => 7683403 
                            [Number] => 630 
                            [Result] => 4 - 0
                            [Datum] => 2013-08-27 
                            [Tijd] => 2000 
                            [CompType] => B ) 
                            )
                ) 
    [2] => Array ( 
    [errorcode] => 9995 
    [message] => No results )
) 

数组的长度不同,结果的数量也不同。我对 api-call 本身的结果没有影响,因为它是由大体育协会设立的。

我绝对不是程序员,所以我无法理解她,但这是业余体育俱乐部的自愿工作,所以没有选择聘请程序员。

Rgds,Bonzyx

【问题讨论】:

    标签: php mysql arrays multidimensional-array


    【解决方案1】:
    if(isset($objResults[1]['List'])){
        foreach($objResults[1]['List'] as &$listItem){
            $listItem['teamID'] = $teamID;
        }
        unset($listItem); //good practice to unset this reference to last array element
    }
    

    你可以用 php 的 array_walk() 函数做同样的事情,但既然你说你不是程序员,我认为 foreach 方法对你来说更清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2019-02-07
      • 1970-01-01
      • 2020-09-07
      • 2015-02-06
      • 2021-01-24
      • 1970-01-01
      相关资源
      最近更新 更多