我正在尝试使用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;
}
计数器返回正常,但我似乎无法解析子数组中的任何变量。回声输出如下:
<br>0<br><br>1<br><br>2<br><br>3<br><br>4<br><br>5<br>
我现在使用的数据集是6个子数组,所以我知道它正在循环正确。
答案 0 :(得分:3)
foreach ($data as list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location)) {
// commands
}
由于仅在PHP的beta版本中允许使用list
,因此更易于使用的语法需要两个步骤:
foreach ($data as $element) {
list($id,$ownerid,$owner,$coords,$name,$score,$citytype,$location) = $element;
// commands
}
foreach ($data=>inner_array as xxx)
无效。 var =&gt; var语法仅在as
之后有效,而不是在它之前。我不确定为什么你有嵌套for循环 - 你只需要迭代一个数组。
当子数组是关联数组时,我会非常谨慎地使用这种语法。列表赋值基于子数组中元素的顺序,它不会尝试将变量名与键匹配。因此,如果您有owner_id
元素位于id
元素之前的子数组,则列表分配会将$id
设置为所有者ID,将$owner_id
设置为ID 。关联数组中元素的顺序是它们被添加到数组的顺序(除非您使用函数重新排列它们,例如array_sort()
或array_splice()
),所以除非您确定你总是按照相同的顺序添加密钥,这可能是危险的。
更新:
像这样的列表赋值不适用于关联数组。根据{{3}}:
list()仅适用于数值数组,并假设数字索引从0开始。
所以你需要单独分配变量:
foreach ($data as $element) {
$id = $element['id'];
$owner_id = $element['owner_id'];
// and so on
}