0

我正在尝试使用 foreach as list 命令提取每个子数组。

更新:

我现在正在尝试这个:

  foreach ($data as $element) {
  list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location) = $element;
  findcontinent($coords);
  echo $coords;
  echo '<br>';
  echo $cont;
  echo '<br>';}

我调用的函数如下。例如,当我从数组 $data[0]['coords'] 静态调用特定坐标时,我知道此函数有效。

function findcontinent($coords){
            $tc2 = explode(":",$coords);
            $tcx = (int)(trim($tc2[0],"'\'"));
            $tcy = (int)(trim($tc2[1],"'"));
            $tcx2 = round((($tcx)*(600/383)),0);
            $tcy2 = round((($tcy)*(600/360)),0);

            if($tcx2 < 100) // checking for continents ending with 0
            {
            $tcx3 = '0';
            $xtrans = substr((string)$tcx3,0,1);
            }
            else
            {
            $xtrans = substr((string)$tcx2,0,1);
            }           

            if($tcy2 < 100) // checking for continents starting with 0
            {
            $tcy3 = '0';
            $ytrans = substr((string)$tcy3,0,1);
            }
            else
            {
            $ytrans = substr((string)$tcy2,0,1);
            }
    $cont = C.$ytrans.$xtrans;
    return($cont);

}

然后我想运行一组 SQL 命令来更新数据库。我有正确的语法吗?

我的数组如下 - 示例。实际的数据集可能是动态的,许多单独的子数组?

[data] => Array
    (
        [0] => Array
            (
                [id] => 16515340
                [owner_id] => 3475
                [owner] => Player1
                [coords] => '268:252
                [name] => AC2013
                [score] => 11863
                [city_type] => castle
                [location] => land
            )

        [1] => Array
            (
                [id] => 16515335
                [owner_id] => 3475
                [owner] => Player1
                [coords] => '263:252
                [name] => AC2013
                [score] => 7
                [city_type] => castle
                [location] => water
            )

        [2] => Array
            (
                [id] => 17891610
                [owner_id] => 3523
                [owner] => Player2
                [coords] => '282:273
                [name] => City of Repoman9900
                [score] => 1978
                [city_type] => castle
                [location] => water
            )

        [3] => Array
            (
                [id] => 10616856
                [owner_id] => 73
                [owner] => Player2
                [coords] => '024:162
                [name] => 1killer
                [score] => 1308
                [city_type] => castle
                [location] => water
            )

        [4] => Array
            (
                [id] => 10813465
                [owner_id] => 2862
                [owner] => Player3
                [coords] => '025:165
                [name] => City of vuvuzea991
                [score] => 1091
                [city_type] => castle
                [location] => land
            )

        [5] => Array
            (
                [id] => 17367317
                [owner_id] => 84
                [owner] => Player4
                [coords] => '277:265
                [name] => Dreadland
                [score] => 776
                [city_type] => castle
                [location] => water
            )

        [6] => Array
            (
                [id] => 2162850
                [owner_id] => 2989
                [owner] => Player5
                [coords] => '162:033
                [name] => City of Dinoeyez
                [score] => 157
                [city_type] => castle
                [location] => water
            )

        [7] => Array
            (
                [id] => 2818192
                [owner_id] => 556
                [owner] => Player6
                [coords] => '144:043
                [name] => City of wildfire123
                [score] => 7
                [city_type] => castle
                [location] => water
            )

    )

由于看起来关联数组无法分解为列表,这本来不错,因此尝试运行此循环以提取甚至 1 条记录,但失败了

$计数 = 0; // 循环遍历数组

foreach($data as $data=>$inner_array){
    $c2 = (string)$count;   
    $id = $data[$count]['id'];
    echo $id;
    echo '<br>';
    echo $count;
    echo '<br>';    
 //then increment counter and move to next
     $count = $count+1;
    }

计数器返回得很好,但我似乎无法解析子数组中的任何变量。回显输出如下:

<br>0<br><br>1<br><br>2<br><br>3<br><br>4<br><br>5<br>

我现在使用的数据集是 6 个子数组,所以我知道它正确地通过了循环。

4

1 回答 1

3
foreach ($data as list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location)) {
  // commands
}

由于这种使用list只允许在 PHP 的 beta 版本中使用,因此更便携的语法需要两个步骤:

foreach ($data as $element) {
  list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location) = $element;
  // commands
}

foreach ($data=>inner_array as xxx)无效。var=>var 语法仅在 之后有效as,在其之前无效。我不确定你为什么嵌套了 for 循环——你只需要迭代一个数组。

当子数组是关联数组时,我会非常谨慎地使用这种语法。列表分配基于子数组中元素的顺序,它不会尝试将变量名与键匹配。owner_id因此,如果您有一个元素位于元素之前的子数组id,则列表分配将设置$id为所有者 ID 和$owner_idID。关联数组中元素的顺序是它们添加到数组中的顺序(除非您使用函数重新排列它们,例如array_sort()or array_splice()),所以除非您确定始终以相同的顺序添加键,否则可以是危险的。

更新:

像这样的列表分配不适用于关联数组。根据PHP 列表文档

list()仅适用于数字数组,并假定数字索引从 0 开始。

因此,您需要单独分配变量:

foreach ($data as $element) {
  $id = $element['id'];
  $owner_id = $element['owner_id'];
  // and so on
}
于 2013-04-03T19:23:56.517 回答