我正在尝试使用 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 个子数组,所以我知道它正确地通过了循环。