1

我正在尝试获取包含 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”。如果您想到一个纵横字谜游戏,其中向下和横向共享相同的起始单元格,其在横向和向下键中标有相同的数字,这就是我试图用我的数据集完成的。

谢谢你的时间。

在此处输入图像描述

4

1 回答 1

0

您应该首先对数据进行排序,以便左上角的条目首先穿过该行,然后再向下移动到下一行。即我们阅读的方式。

我们可以取 grid_x 和 grid_y 并计算哪个是第一个。

然后我们可以使用这些对数据进行排序。

function cmp($a, $b)
{
    $a_index=($a['grid_y']*100)+$a['grid_x'];
    $b_index=($b['grid_y']*100)+$b['grid_x'];
    if ($a_index == $b_index)
    {
        return 0;
    }
    return ($a_index < $b_index) ? -1 : 1;
}
usort($puzzle_data, "cmp");

我在这里使用了 100 以使事情变得更容易,但您应该使用网格的宽度以获得更高的准确性。

x1,y1 将给出 101
x3,y1 将给出 103
x1,y3 将给出 301
y3,y3 将给出 303

所以顺序是 101, 103, 301, 303

然后使用 grid_x 和 grid_y 将数字应用于数据以测试是否已经完成。

$number=1;
$grid_numbers=array();
foreach($puzzle_data as $rkey=>$row)
{
    if(isset($grid_numbers[$row['grid_x']][$row['grid_y']]))
    {
        $puzzle_data[$rkey]['num'] = $grid_numbers[$row['grid_x']][$row['grid_y']];
    }
    else
    {
        $grid_numbers[$row['grid_x']][$row['grid_y']]=$number;
        $puzzle_data[$rkey]['num']=$number;
        $number++;
    }
}

那应该就是一切

于 2013-02-27T10:31:11.390 回答