2016-10-05 39 views
1

目前我有此列出的陣列,並將它們以最高最低排序,但它不會,數字排序他們的第一個數字如何排序數字PHP

<?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> 

感謝

+0

公制將使這容易:-) – nogad

+0

嗨@nogad但我是新來的這些東西,你能幫我讓我開始:-) –

+0

你可以發佈你的'get_posts'返回的對象 – Perspective

回答

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()功能和順序是工作的罰款