将数组项与条件交换

发布于 2024-11-19 08:12:38 字数 2680 浏览 4 评论 0 原文

我遇到了将数组与条件交换的情况。我的数组绘制如下。

  • pid=>就像mysql中的主键一样,所以它可能会丢失它的顺序
  • type=> type表示产品类型。数组有3种类型的产品。
  • 姓名=>简单地表示产品名称

首先我尝试了mysql中的解决方案,但我没有得到任何好的迹象。其中一些建议我数组交换是更好的解决方案。但我不知道如何获得这个。

我的问题

是我有物品清单。当我列出产品时,类型 3(材料)不应出现在 5、10、15 (即 5 个位置的模块)。如果它使我的设计崩溃。

屏幕截图说明

  1. 完美位置
  2. 折叠设计

所以我想确保类型3(主)永远不会出现在 5 位置的模式。我该怎么做 帮助我

我之前在 mysql 中的尝试 此处

    Array
    (
        [0] => Array
            (
                [pid] => 1
                [type] => 1
                [name] => A
            )

    [1] => Array
        (
            [pid] => 2
            [type] => 1
            [name] => B
        )

    [2] => Array
        (
            [pid] => 3
            [type] => 2
            [name] => D
        )

    [3] => Array
        (
            [pid] => 4
            [type] => 3
            [name] => E(master)
        )

    [4] => Array
        (
            [pid] => 5
            [type] => 3
            [name] => f(sub)
        )

    [5] => Array
        (
            [pid] => 6
            [type] => 1
            [name] => A1
        )

    [6] => Array
        (
            [pid] => 7
            [type] => 2
            [name] => B1
        )

    [7] => Array
        (
            [pid] => 8
            [type] => 1
            [name] => C1
        )

    [8] => Array
        (
            [pid] => 9
            [type] => 2
            [name] => D1
        )

    [9] => Array
        (
            [pid] => 10
            [type] => 3
            [name] => E1(master)
        )

    [10] => Array
        (
            [pid] => 11
            [type] => 3
            [name] => A2(sub)
        )

    [11] => Array
        (
            [pid] => 12
            [type] => 2
            [name] => B2
        )

    [12] => Array
        (
            [pid] => 13
            [type] => 1
            [name] => C2
        )

    [13] => Array
        (
            [pid] => 14
            [type] => 2
            [name] => D2
        )

    [14] => Array
        (
            [pid] => 15
            [type] => 1
            [name] => E2
        )

提前致谢

I have the situation to swap array with condition.My array is plotted bellow.

  • pid => like primary key in mysql so it may loose it order
  • type=> type indicates, product type.array having 3 type of products.
  • Name=> Simply indicates product name

First I tried solution in mysql, But I didn't got any good sign.Some of them suggest me array swapping is better one. But I don't know How to get this one.

My problem

I am having list of items.When i listing products, type 3(mater) should not come at 5,10,15
(i.e modules of 5 ) positions.If it does my design getting collapse.

Screen Shot explanation

  1. Perfect placement
  2. Collapsed design

So i want to make sure type 3(master) never comes at mod of 5 position.How can i do this
Help me

My previous try in mysql here

    Array
    (
        [0] => Array
            (
                [pid] => 1
                [type] => 1
                [name] => A
            )

    [1] => Array
        (
            [pid] => 2
            [type] => 1
            [name] => B
        )

    [2] => Array
        (
            [pid] => 3
            [type] => 2
            [name] => D
        )

    [3] => Array
        (
            [pid] => 4
            [type] => 3
            [name] => E(master)
        )

    [4] => Array
        (
            [pid] => 5
            [type] => 3
            [name] => f(sub)
        )

    [5] => Array
        (
            [pid] => 6
            [type] => 1
            [name] => A1
        )

    [6] => Array
        (
            [pid] => 7
            [type] => 2
            [name] => B1
        )

    [7] => Array
        (
            [pid] => 8
            [type] => 1
            [name] => C1
        )

    [8] => Array
        (
            [pid] => 9
            [type] => 2
            [name] => D1
        )

    [9] => Array
        (
            [pid] => 10
            [type] => 3
            [name] => E1(master)
        )

    [10] => Array
        (
            [pid] => 11
            [type] => 3
            [name] => A2(sub)
        )

    [11] => Array
        (
            [pid] => 12
            [type] => 2
            [name] => B2
        )

    [12] => Array
        (
            [pid] => 13
            [type] => 1
            [name] => C2
        )

    [13] => Array
        (
            [pid] => 14
            [type] => 2
            [name] => D2
        )

    [14] => Array
        (
            [pid] => 15
            [type] => 1
            [name] => E2
        )

)

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2024-11-26 08:12:38

我尝试了一下,下面的代码应该可以解决问题。我建议您将一个块分成几个定义块的子部分。此外,它不是非常干燥的代码,但它会给你一个想法:

<?php

$grid = array(); $blocks = array();

$blocks[] = array(array("pid" => 1, "type" => 1, "name" => "A")); 
$blocks[] = array(array("pid" => 2, "type" => 1, "name" => "B")); 
$blocks[] = array(array("pid" => 3, "type" => 2, "name" => "D")); 
$blocks[] = array(array("pid" => 4, "type" => 3, "name" => "E(master)"), array("pid" => 5, "type" => 3, "name" => "F (sub)")); 
$blocks[] = array(array("pid" => 6, "type" => 1, "name" => "A1")); 
$blocks[] = array(array("pid" => 7, "type" => 2, "name" => "B1")); 
$blocks[] = array(array("pid" => 8, "type" => 1, "name" => "C1")); 
$blocks[] = array(array("pid" => 9, "type" => 2, "name" => "D1")); 
$blocks[] = array(array("pid" => 10, "type" => 3, "name" => "E1 (master)"), array("pid" => 11, "type" => 3, "name" => "A2 (sub)")); 
$blocks[] = array(array("pid" => 12, "type" => 2, "name" => "B2")); 
$blocks[] = array(array("pid" => 13, "type" => 1, "name" => "C2")); 
$blocks[] = array(array("pid" => 14, "type" => 2, "name" => "D2")); 
$blocks[] = array(array("pid" => 14, "type" => 1, "name" => "E2"));

$current_row = 0;

for ($n=0;$n < count($blocks);$n++) {

    //Check if current row exists in grid
    if (!isset($grid[$current_row]))
    {
        //Create new empty row in grid
        $grid[$current_row] = array();
    }

    //check if current block fits
    if (count($grid[$current_row]) + count($blocks[$n]) > 5)
    {
        // Block doesn't fit: Search for block that fits
        for ($i=$n;$i < count($blocks);$i++)
        {
            if (count($grid[$current_row]) + count($blocks[$i]) <= 5)
            {
                //place parts in block on current row
                foreach ($blocks[$i] as $part)
                {
                    $grid[$current_row][] = $part;
                }

                //unset block from queue
                unset($blocks[$i]);
            }
        }

        //place current block on new row
        $current_row++;
    }

    //place parts in block in grid
    foreach ($blocks[$n] as $part)
    {
        $grid[$current_row][] = $part;
    }
}

print_r($grid); 
?>

I gave it a try, the following code should do the trick. What I recommend is that you split up a block in the several subparts that define a block. Furthermore, it's not very DRY code, but it will give you an idea:

<?php

$grid = array(); $blocks = array();

$blocks[] = array(array("pid" => 1, "type" => 1, "name" => "A")); 
$blocks[] = array(array("pid" => 2, "type" => 1, "name" => "B")); 
$blocks[] = array(array("pid" => 3, "type" => 2, "name" => "D")); 
$blocks[] = array(array("pid" => 4, "type" => 3, "name" => "E(master)"), array("pid" => 5, "type" => 3, "name" => "F (sub)")); 
$blocks[] = array(array("pid" => 6, "type" => 1, "name" => "A1")); 
$blocks[] = array(array("pid" => 7, "type" => 2, "name" => "B1")); 
$blocks[] = array(array("pid" => 8, "type" => 1, "name" => "C1")); 
$blocks[] = array(array("pid" => 9, "type" => 2, "name" => "D1")); 
$blocks[] = array(array("pid" => 10, "type" => 3, "name" => "E1 (master)"), array("pid" => 11, "type" => 3, "name" => "A2 (sub)")); 
$blocks[] = array(array("pid" => 12, "type" => 2, "name" => "B2")); 
$blocks[] = array(array("pid" => 13, "type" => 1, "name" => "C2")); 
$blocks[] = array(array("pid" => 14, "type" => 2, "name" => "D2")); 
$blocks[] = array(array("pid" => 14, "type" => 1, "name" => "E2"));

$current_row = 0;

for ($n=0;$n < count($blocks);$n++) {

    //Check if current row exists in grid
    if (!isset($grid[$current_row]))
    {
        //Create new empty row in grid
        $grid[$current_row] = array();
    }

    //check if current block fits
    if (count($grid[$current_row]) + count($blocks[$n]) > 5)
    {
        // Block doesn't fit: Search for block that fits
        for ($i=$n;$i < count($blocks);$i++)
        {
            if (count($grid[$current_row]) + count($blocks[$i]) <= 5)
            {
                //place parts in block on current row
                foreach ($blocks[$i] as $part)
                {
                    $grid[$current_row][] = $part;
                }

                //unset block from queue
                unset($blocks[$i]);
            }
        }

        //place current block on new row
        $current_row++;
    }

    //place parts in block in grid
    foreach ($blocks[$n] as $part)
    {
        $grid[$current_row][] = $part;
    }
}

print_r($grid); 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文