使用usort对数组的动态标头进行排序

时间:2018-05-18 14:41:34

标签: php

我有以下数组,结果是

Loc    L    M   S   XS
001    2    3   3   1
006    0    2   3   1
012    2    2   2   2

我正在尝试按如下方式对此数组的标头进行排序: XS,S,M,L,XL(服装尺码从小到特大)

我已经弄清楚usort函数如何与下面的示例代码一起工作,但我无法弄清楚如何在我的数组中实现它,因为我不知道如何识别我的数组L中的键, M,S,XS?

样品:

<?php
$order = array("XS","S", "M", "L", "XL");
$array = array(
    array('id' => "M", 'title' => 'Some Title'),
    array('id' => "L", 'title' => 'Some Title'),
    array('id' => "S", 'title' => 'Some Title'),
    array('id' => "XL", 'title' => 'Some Title'),
);

usort($array, function ($a, $b) use ($order) {
    $pos_a = array_search($a['id'], $order);
    $pos_b = array_search($b['id'], $order);
    return $pos_a - $pos_b;
});

var_dump($array);
?>

数组:

Array
(
    [0] => Array
        (
            [Loc] => 001
            [L] => 2
            [M] => 3
            [S] => 3
            [XS] => 1
        )

    [1] => Array
        (
            [Loc] => 006
            [L] => 0
            [M] => 2
            [S] => 3
            [XS] => 1
        )

    [2] => Array
        (
            [Loc] => 012
            [L] => 2
            [M] => 2
            [S] => 2
            [XS] => 2
        )

)

我的php:

echo '<tr>';
//headers
foreach( $result as $key => $value )
{
    if( is_array($value) ) 
    { 
        foreach($value as $key => $column) {
            echo '  <th colspan="1" style="width: 30px">'.$key.'</th>';
        }
    }
    else 
    {
        echo '<th colspan="1">Niks</th>';
    }
    break;
}
echo '</tr>';
//Data
foreach( $result as $key => $value )
{
echo '<tr>';
    if( is_array($value) ) 
    { 
        foreach($value as $key => $column) {
            echo '<td>'.$column.'</td>';
        }
    }
    else 
    {
        echo '<td>'.$value.'</td>';
    }
echo '</tr>';
}

2 个答案:

答案 0 :(得分:2)

我实际上不会使用14070f7c4ffc49efb1fc1709cc4a7267|90c6efd2cf8342859756d8e3705417a3。您可以根据usort数组输出。

$order

对于标题,只需迭代订单数组并在$order = array("XS","S", "M", "L", "XL"); s中使用这些值。

<th>

对于数据,迭代每行的顺序数组,并使用这些顺序值作为键来访问行中的值。

echo '<tr>';
//headers
foreach($order as $key)
{
    echo '  <th colspan="1" style="width: 30px">'.$key.'</th>';
}
echo '</tr>';

答案 1 :(得分:0)

为每一行建立一个模板数组,然后使用array_replace()复制每个项目的相关部分应该可以解决问题。

$result = Array
(
    Array
    (
        "Loc" => "001",
        "L" => 2,
        "M" => 3,
        "S" => 3,
        "XS" => 1
        ),
    Array
    (
        "Loc" => "006",
        "L" => 0,
        "M" => 2,
        "S" => 3,
        "XS" => 1
        ),
    Array
    (
        "Loc" => "012",
        "L" => 2,
        "M" => 2,
        "S" => 2,
        "XS" => 2
        )
    );

$order = array("Loc", "XS","S", "M", "L", "XL");
$order = array_fill_keys($order, 0);
$newResult = [];
foreach ( $result as $item )    {
    $newResult[] = array_replace($order, $item);
}
print_r($newResult);

输出...

Array
(
    [0] => Array
        (
            [Loc] => 001
            [XS] => 1
            [S] => 3
            [M] => 3
            [L] => 2
            [XL] => 0
        )

    [1] => Array
        (
            [Loc] => 006
            [XS] => 1
            [S] => 3
            [M] => 2
            [L] => 0
            [XL] => 0
        )

    [2] => Array
        (
            [Loc] => 012
            [XS] => 2
            [S] => 2
            [M] => 2
            [L] => 2
            [XL] => 0
        )

)