如何排序数字PHP

时间:2016-10-05 01:21:57

标签: php wordpress

目前我有这个列出一个数组,并从最低到最高排序,但它不会,数字排序到他们的第一个数字

<?php 
    $posts = get_posts(
        array(
            'post_type' => 'product',
            'meta_key' => 'size-in-feet',
            'posts_per_page' => -1,
            'order' => 'ASC'
        )
    );

    $meta_values = array();
    foreach( $posts as $post ) {
        $meta_values[] = get_post_meta( get_the_ID(), 'size-in-feet', true );
    }

    $sizearray = $meta_values;
    foreach($sizearray as $sizelist) {
        echo '<button class="button" data-filter=".'.$sizelist.'">'.$sizelist.' ft</button>';
    }

?>

,输出就像这样排序

<button class="button" data-filter=".6x4">6x4 ft</button>
<button class="button" data-filter=".5x4">5x4 ft</button>
<button class="button" data-filter=".9x5">9x5 ft</button>
<button class="button" data-filter=".6x3">6x3 ft</button>
<button class="button" data-filter=".6x4">6x4 ft</button>
<button class="button" data-filter=".21x3">21x3 ft</button>
<button class="button" data-filter=".11x8">11x8 ft</button>
<button class="button" data-filter=".13x12">13x12 ft</button>
<button class="button" data-filter=".6x4">6x4 ft</button>
<button class="button" data-filter=".7x5">7x5 ft</button>
<button class="button" data-filter=".10x6">10x6 ft</button>

我怎样才能让它像这样排序,从最低到最高

<button class="button" data-filter=".5x4">5x4 ft</button>
<button class="button" data-filter=".6x3">6x3 ft</button>
<button class="button" data-filter=".6x4">6x4 ft</button>
<button class="button" data-filter=".6x4">6x4 ft</button>
<button class="button" data-filter=".6x4">6x4 ft</button>
<button class="button" data-filter=".7x5">7x5 ft</button>
<button class="button" data-filter=".9x5">9x5 ft</button>
<button class="button" data-filter=".10x6">10x6 ft</button>
<button class="button" data-filter=".11x8">11x8 ft</button>
<button class="button" data-filter=".13x12">13x12 ft</button>
<button class="button" data-filter=".21x3">21x3 ft</button>

由于

1 个答案:

答案 0 :(得分:2)

我能够提出一个解决方案

<?php 
    $posts = get_posts(
        array(
            'post_type' => 'product',
            'meta_key' => 'size-in-feet',
            'posts_per_page' => -1,
            'order' => 'ASC'
        )
    );

    $meta_values = array();
    foreach( $posts as $post ) {
        $meta_values[] = get_post_meta( get_the_ID(), 'size-in-feet', true );
    }

    $sizearray = array_unique($meta_values);
    $sizearray_sorted = natsort($sizearray);

    foreach($sizearray as $sizelist) {
        echo '<button class="button" data-filter=".'.$sizelist.'">'.$sizelist.' ft</button>';
    }
?>  

我刚刚添加了natsort()功能,订单工作正常