【问题标题】:Extracting arrays into lists将数组提取到列表中
【发布时间】:2013-04-03 19:16:32
【问题描述】:

我正在尝试使用 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 条记录,但失败了

$count = 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;
    }

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

&lt;br&gt;0&lt;br&gt;&lt;br&gt;1&lt;br&gt;&lt;br&gt;2&lt;br&gt;&lt;br&gt;3&lt;br&gt;&lt;br&gt;4&lt;br&gt;&lt;br&gt;5&lt;br&gt;

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

【问题讨论】:

  • 你从哪里得到语法?您或许应该多花点时间阅读文档 - 对于这个例子,page on foreach structures 应该会有所帮助。
  • 我从php.net/manual/en/control-structures.foreach.php 中获得了语法,这是关于使用 list() 解包嵌套数组的部分。据我所知,我坦率地承认我是这里的新手,你可以将 as 列表直接放在 foreach 中,这是我尝试过的。 @Barmar
  • 我没有意识到 list 被允许在那里,我已经更新了我的答案以使用它。
  • ... 从仍处于测试阶段的 5.5 开始允许。
  • 我质疑的是 $data =&gt; inner_array 部分 :-)

标签: php mysql arrays list foreach


【解决方案1】:
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=&gt;inner_array as xxx) 无效。 var=>var 语法只在as 之后有效,在它之前无效。我不知道你为什么嵌套了 for 循环——你只需要迭代一个数组。

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

更新:

这样的列表赋值不适用于关联数组。根据PHP list documentation

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

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

foreach ($data as $element) {
  $id = $element['id'];
  $owner_id = $element['owner_id'];
  // and so on
}

【讨论】:

  • 子数组格式是固定的,每个子数组都一样,所以顺序不会改变。问题,是否只有 $data 允许我遍历每个子数组并动态获取值?
  • 您似乎没有名为 $data 的变量。您的顶层是一个关联数组,其元素的键为data,因此我在回答中将其称为$array['data']
  • 我已在我的代码中将 $data 定义为变量。基本上我所拥有的是$data=$_POST['data'] 定义的。现在我想遍历 $data 并动态提取子数组值并运行计算,发布到数据库。我在这里发布的只是 $_POST['data'] 的内容,因为其余信息与我正在处理的内容无关。
  • 所以我有$array['data'],你可以使用$data
  • 我需要将它封装在 '$data' 还是只需 $data 就可以了? =$element 在下一行做了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
相关资源
最近更新 更多