开发者

Swap array Item with condition

开发者 https://www.devze.com 2023-03-19 04:53 出处:网络
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

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


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); 
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消