我有条件用条件交换数组。我的数组如下所示。
首先我在mysql中尝试了解决方案,但我没有任何好的迹象。其中一些建议我阵列交换更好。但我不知道如何得到这个。
我有物品清单。当我列出产品时,类型3(mater)不应该是5,10,15 (即5个模块的位置。如果我的设计崩溃了。
屏幕截图解释
所以我想确保类型3(主)永远不会出现在5位的mod。我怎么能这样做 帮帮我
我之前在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
)
)
提前致谢
答案 0 :(得分:1)
我试一试,以下代码应该可以解决问题。我建议你在定义一个块的几个子部分中拆分一个块。此外,它不是非常干的代码,但它会给你一个想法:
<?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);
?>