我正在尝试获取包含 x/y 坐标和方向信息(横向或向下)的数据数组,并将共享单元格与递增的数字链接起来。这是一个填字游戏。这是原始数组:
Array
(
[0] => Array
(
[id] => 1
[puzzle_id] => 1
[word] => hello
[hint] =>
[direction] => across
[grid_x] => 3
[grid_y] => 1
)
[1] => Array
(
[id] => 2
[puzzle_id] => 1
[word] => loot
[hint] =>
[direction] => down
[grid_x] => 5
[grid_y] => 1
)
[2] => Array
(
[id] => 3
[puzzle_id] => 1
[word] => hellotest
[hint] =>
[direction] => down
[grid_x] => 3
[grid_y] => 1
)
[3] => Array
(
[id] => 4
[puzzle_id] => 1
[word] => looking
[hint] =>
[direction] => across
[grid_x] => 3
[grid_y] => 3
)
)
我想为每个名为“num”的条目添加一个新键,该键将从 1 开始并递增 1,但棘手的部分是如果它们共享一个起始单元格(共享一个 grid_x 和 grid_y )。这是我现在拥有的代码,但它不会产生我正在寻找的正确数字。
$puzzle_data2 = $puzzle_data;
$across_counter = 1;
foreach($puzzle_data as $rkey=>$row)
{
if($row['direction'] == 'across')
{
$puzzle_data[$rkey]['num'] = $across_counter++;
foreach($puzzle_data2 as $rkey2=>$row2)
{
if($row['direction'] == 'down')
{
if($row['grid_x'] == $row2['grid_x'] && $row['grid_y'] == $row2['grid_y'])
{
$puzzle_data[$rkey2]['num'] = $across_counter;
break;
}
}
}
}
}
foreach($puzzle_data as $rkey=>$row)
{
if(!isset($row['num']))
{
$puzzle_data[$rkey]['num'] = $across_counter++;
}
}
我最终得到了这个结果:
Array
(
[0] => Array
(
[id] => 1
[puzzle_id] => 1
[word] => hello
[hint] =>
[direction] => across
[grid_x] => 3
[grid_y] => 1
[num] => 1
)
[1] => Array
(
[id] => 2
[puzzle_id] => 1
[word] => loot
[hint] =>
[direction] => down
[grid_x] => 5
[grid_y] => 1
[num] => 3
)
[2] => Array
(
[id] => 3
[puzzle_id] => 1
[word] => hellotest
[hint] =>
[direction] => down
[grid_x] => 3
[grid_y] => 1
[num] => 4
)
[3] => Array
(
[id] => 4
[puzzle_id] => 1
[word] => looking
[hint] =>
[direction] => across
[grid_x] => 3
[grid_y] => 3
[num] => 2
)
)
我正在寻找 ID 1 和 3 都是“1”。如果您想到一个纵横字谜游戏,其中向下和横向共享相同的起始单元格,其在横向和向下键中标有相同的数字,这就是我试图用我的数据集完成的。
谢谢你的时间。