如何指定数组中应显示的订单商品?
我得到一个3级数组,有点像下面这样:
Array
(
[AK] => Array
(
[ACO] => Array
(
[FFS] => Array
(
[Medicaid] => -
[CHIP] => -
)
)
[ZCO] => Array
(
[FFS] => Array
(
[Medicaid] => -
[CHIP] => -
)
)
[Other] => Array
(
[FFS] => Array
(
[Medicaid] => -
[CHIP] => -
)
)
)
[AR] => Array
(
[Other] => Array
(
[Other] => Array
(
[Medicaid] => -
[CHIP] => -
)
)
[ACO] => Array
(
[FFS] => Array
(
[Medicaid] => -
[CHIP] => -
)
)
[ZCO] => Array
(
[FFS] => Array
(
[Medicaid] => -
[CHIP] => -
)
)
)
)
如何通过第二级命名元素(我需要它们都是[AK]
)使它们排序第一个元素[ACO], [ZCO], [Other]
?
答案 0 :(得分:1)
字典是没有订单的数据结构。
但是 php 默认情况下按顺序迭代项目,以便粘贴哪些元素 因此,您可以按必要的顺序简单地重新创建数组 或者使用php usort方法编写自定义排序函数。
小例子:
function compare($a, $b)
{
$order = array('ACO', 'ZCO', 'Other');
$ai = array_search($a, $order);
$bi = array_search($b, $order);
return $ai<$bi ? -1 : $ai>$bi;
}
$a = array('ZCO'=>1, 'Other'=>1, 'ACO'=>1);
uksort($a, "compare");
print_r($a);
答案 1 :(得分:0)
// CREATE THE TEST DATA
$arr = Array
( 'AK' => Array
( 'ACO' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
, 'ZCO' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
, 'Other' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
)
, 'AR' => Array
( 'Other' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
, 'ACO' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
, 'ZCO' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
)
)
;
// ACTIVATE THIS TO ANSWER THE QUESTION: DOES IT LOOK RIGHT?
// print_r($arr);
// GET THE ORDER FOR THE [AK] ARRAY
$pat = array_keys($arr['AK']);
// ITERATE OVER THE ARRAY TO PRODUCE THE ORDERED OUTPUT
$out = array();
foreach($arr as $key => $inner)
{
// SKIP THE SIGNAL ARRAY
if ($key == 'AK')
{
$out[$key] = $inner;
continue;
}
// REORDER THE OTHER ARRAYS
else
{
$temp = array();
foreach ($pat as $inner_key)
{
$temp[$inner_key] = $inner[$inner_key];
}
$out[$key] = $temp;
}
}
// SHOW THE WORK PRODUCT
print_r($out);