2017-06-29 40 views
-2

我有一個數組,如下所示。我想按升序對srp的值進行排序。我怎樣才能在PHP中做到這一點。我是初學者,我認爲它可以使用usort(),但我不知道如何去做。帶有鍵值的PHP排序數組排序

Array 
(
    [0] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d3 
      [title] => Zen M4S(Silver) 
      [mrp] => 0 
      [srp] => 1900 
     ) 

    [1] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d2 
      [title] => Zen M4S(Silver) 
      [mrp] => 0 
      [srp] => 1000 
     ) 

    [2] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d4 
      [title] => JIVI X525(Red) 
      [mrp] => 0 
      [srp] => 1250 
     ) 

) 

所需的輸出:

Array 
(
    [0] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d2 
      [title] => Zen M4S(Silver) 
      [mrp] => 0 
      [srp] => 1000 
     ) 

    [1] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d4 
      [title] => JIVI X525(Red) 
      [mrp] => 0 
      [srp] => 1250 
     ) 

    [2] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d3 
      [title] => Zen M4S(Silver) 
      [mrp] => 0 
      [srp] => 1900 
     ) 
) 

我的班級是這樣的:

class TopDealsDAOMongoImpl implements TopDealsDAO{ 
    //put your code here 
    public function getTopDeals() { 
     $topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get())); 
     function my_sort($a,$b) 
     { 
      if ($a->srp == $b->srp) return 0; 
      return ($a->srp < $b->srp)?-1:1; 
     } 

     uasort($topDeals, 'my_sort'); 

    } 


} 
+4

的可能的複製[如何通過在PHP給定的鍵的值進行排序關聯數組的數組?](https://stackoverflow.com/questions/1597736/如何對數組中的關聯數組進行排序) –

+1

那裏提供的答案會產生錯誤:不能使用stdClass類型的對象作爲數組 – Dexter

+1

之前在SO上張貼問題。嘗試自我解決和研究。當您發佈重複問題時,您會造成網站膨脹並浪費志願者的時間。 – mickmackusa

回答

1

嘗試下面的代碼, 希望這是有幫助的。

PHP Script.

<?php 
    // Static array. 
    $array=array(); 
    $array[0]=array(
     '_id'=>'5911af8209ed4456d069b1d3', 
     'title'=>'Zen M4S(Silver) 1', 
     'srp'=>1900 
    ); 
    $array[1]=array(
     '_id'=>'5911af8209ed4456d069b1d2', 
     'title'=>'Zen M4S(Silver) 2', 
     'srp'=>1000 
    ); 
    $array[2]=array(
     '_id'=>'5911af8209ed4456d069b1d4', 
     'title'=>'Zen M4S(Silver) 1', 
     'srp'=>1250 
    ); 

    // For acending sorting. 
    function asc_sort_json($array1,$array2){ 
     $on = 'srp'; 
     if ($array1[$on] == $array2[$on]) { 
      return 0; 
     } 
     return ($array1[$on] < $array2[$on]) ? -1 : 1; 
    } 
    // For decending sorting. 
    function desc_sort_json($array1,$array2){ 
     $on = 'srp'; 
     if ($array1[$on] == $array2[$on]) { 
      return 0; 
     } 
     return ($array1[$on] < $array2[$on]) ? 1 : -1; 
    } 

    $array_json = json_encode($array); // encode array in json 
    $array_json1 = json_decode($array_json); // decode json from array because get json object 
    $array_json2 = json_decode(json_encode($array_json1),true); // parse json into array 

    usort($array_json2, "asc_sort_json"); // Call function for acending order. 
    $array_json2 = json_decode(json_encode($array_json2)); // Array to json. 

    $array_json1 = json_encode($array); // encode array in json 
    $array_json2 = json_decode($array_json1); // decode json from array because get json object 
    $array_json3 = json_decode(json_encode($array_json2),true); // parse json into array 

    usort($array_json3, "desc_sort_json"); // Call function for decending order. 
    $array_json4 = json_decode(json_encode($array_json2)); // Array to json. 
?> 

Outputs.

echo "<pre>"; 
print_r($array_json2); 
echo "<pre>"; 
print_r($array_json4); 

// Asc 
Array 
(
    [0] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d2 
      [title] => Zen M4S(Silver) 2 
      [srp] => 1000 
     ) 

    [1] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d4 
      [title] => Zen M4S(Silver) 1 
      [srp] => 1250 
     ) 

    [2] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d3 
      [title] => Zen M4S(Silver) 1 
      [srp] => 1900 
     ) 

) 

// Desc 
Array 
(
    [0] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d2 
      [title] => Zen M4S(Silver) 2 
      [srp] => 1000 
     ) 

    [1] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d4 
      [title] => Zen M4S(Silver) 1 
      [srp] => 1250 
     ) 

    [2] => stdClass Object 
     (
      [_id] => 5911af8209ed4456d069b1d3 
      [title] => Zen M4S(Silver) 1 
      [srp] => 1900 
     ) 

) 
0

這給一試:

// Sort array 
uasort($array, 'cmp'); 

function cmp($a, $b) { 
    if ($a['srp'] == $b['srp']) { 
     return 0; 
    } 
    // Return in acsending order 
    return ($a['srp'] < $b['srp']) ? -1 : 1; 
} 

閱讀:Uasort

+0

[ErrorException]uasort()期望參數2是一個有效的回調函數'my_sort'未找到或函數名無效 – Dexter

+0

你能發佈返回錯誤的代碼嗎? my_sort不在我的例子中。 – rhysnhall

+0

也試過你的: uasort($ topDeals,'cmp'); var_dump($ topDeals); 函數cmp($ a,$ b){ if($ a ['srp'] == $ b ['srp']){ return 0; } //按發送順序返回 return($ a ['srp'] <$ b ['srp'])? -1:1; } – Dexter

0

對於簡單的陣列

function my_sort($a,$b) 
{ 
    if ($a['srp'] == $b['srp']) return 0; 
    return ($a['srp'] < $b['srp'])?-1:1; 
} 

uasort($array, 'my_sort'); // replace $array with your variable 

對於StdObject語法將是:

function my_sort($a,$b) 
{ 
    if ($a->srp == $b->srp) return 0; 
    return ($a->srp < $b->srp)?-1:1; 
} 

uasort($array, 'my_sort'); // replace $array with your variable 
+0

[ErrorException] uasort ()期望參數2是一個有效的回調函數'my_sort'未找到或無效的函數名稱 – Dexter

+0

您是否添加了my_sort函數並且還將'$ array'的名稱更改爲您的數組名稱? –

+0

是的,但它顯示這個錯誤 – Dexter

-1

使用usort和傳遞用戶在它定義函數。

$tmp = new stdClass; 
$tmp->_id="5911af8209ed4456d069b1d3"; 
$tmp->title="Zen M4S(Silver)"; 
$tmp->mrp=0; 
$tmp->srp=1900; 

$tmp1 = new stdClass; 
$tmp1->_id="5911af8209ed4456d069b1d2"; 
$tmp1->title="Zen M4S(Silver)"; 
$tmp1->mrp=0; 
$tmp1->srp=1000; 

$tmp2 = new stdClass; 
$tmp2->_id="5911af8209ed4456d069b1d4"; 
$tmp2->title="JIVI X525(Red)"; 
$tmp2->mrp=0; 
$tmp2->srp=1250; 

$new_array = array($tmp,$tmp1,$tmp2); 
function sort_arr($a, $b) 
{ 
    return ($a->srp<$b->srp)?-1:1; 
} 

usort($new_array, "sort_arr"); 
var_dump($new_array); 

從您的編輯使用的是一流的,所以你可以做這樣的:

class TopDealsDAOMongoImpl implements TopDealsDAO{ 
    //put your code here 
    public function getTopDeals() { 
     $topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get())); 


     uasort($topDeals, array($this, "sort_arr")); 

    } 
    public function sort_arr($a,$b) 
    { 
     if ($a->srp == $b->srp) return 0; 
     return ($a->srp < $b->srp)?-1:1; 
    } 


} 

O/P:

array (size=3) 
    0 => 
    object(stdClass)[2] 
     public '_id' => string '5911af8209ed4456d069b1d2' (length=24) 
     public 'title' => string 'Zen M4S(Silver)' (length=15) 
     public 'mrp' => int 0 
     public 'srp' => int 1000 
    1 => 
    object(stdClass)[3] 
     public '_id' => string '5911af8209ed4456d069b1d4' (length=24) 
     public 'title' => string 'JIVI X525(Red)' (length=14) 
     public 'mrp' => int 0 
     public 'srp' => int 1250 
    2 => 
    object(stdClass)[1] 
     public '_id' => string '5911af8209ed4456d069b1d3' (length=24) 
     public 'title' => string 'Zen M4S(Silver)' (length=15) 
     public 'mrp' => int 0 
     public 'srp' => int 1900 
+0

[ErrorException] 提供的無效參數foreach() – Dexter

+0

它必須是不同的東西。在這個q/a中沒有'foreach'循環 –

+0

usort($ topDeals,function($ a,$ b){return $ b-> srp - $ a-> srp;}); var_dump($ topDeals); 這工作 – Dexter