2017-05-09 18 views
-1

排序在對象關聯數組我有以下目的:PHP由值

object(oxVariantSelectList)[18927] 
    protected '_sLabel' => string 'Größe' (length=7) 
    protected '_iIndex' => int 1 
    protected '_aList' => 
    array (size=5) 
     '02ce605576fee3181f11ebd2c87baed0' => 
     object(oxSelection)[18965] 
      protected '_sName' => string 'Benny' (length=5) 
      protected '_sValue' => string '02ce605576fee3181f11ebd2c87baed0' (length=32) 
      protected '_blActive' => boolean false 
      protected '_blDisabled' => null 
     '690645228c86f17648b3a7b1286f1946' => 
     object(oxSelection)[18968] 
      protected '_sName' => string 'Arvid' (length=5) 
      protected '_sValue' => string '690645228c86f17648b3a7b1286f1946' (length=32) 
      protected '_blActive' => boolean false 
      protected '_blDisabled' => null 
     '972b08df7f6a1a19405f28c1e984b115' => 
     object(oxSelection)[18969] 
      protected '_sName' => string 'Chris' (length=5) 
      protected '_sValue' => string '972b08df7f6a1a19405f28c1e984b115' (length=32) 
      protected '_blActive' => boolean false 
      protected '_blDisabled' => null 
     '1ba5f38fd6213a22679a4eba30651390' => 
     object(oxSelection)[18970] 
      protected '_sName' => string 'Alex' (length=5) 
      protected '_sValue' => string '1ba5f38fd6213a22679a4eba30651390' (length=32) 
      protected '_blActive' => boolean false 
      protected '_blDisabled' => null 
     'b0a471b33a911ce8fed459d607f0ffb3' => 
     object(oxSelection)[18985] 
      protected '_sName' => string 'Mona' (length=5) 
      protected '_sValue' => string 'b0a471b33a911ce8fed459d607f0ffb3' (length=32) 
      protected '_blActive' => boolean false 
      protected '_blDisabled' => null 
    protected '_oActiveSelection' => null 

我想由_sName ($obj->getName())對數組進行排序。

有沒有什麼辦法可以通過php來實現?

eidt:我cahnged因爲一些混亂:)

提前感謝的_sName。

+0

看一看'usort' [文件](HTTP:// PHP .net/manual/en/function.usort.php) –

+1

[我如何在PHP中對數組和數據進行排序?](http://stackoverflow.com/questions/17364127/how-can-i-sort- array-and-data-in-php) –

+0

如果_aList數組是數據庫中的數據,你可以簡單地執行一個ORDER BY _sName ASC | DESC a你完成了。 –

回答

0

您可以usort建立自己的排序功能:

uasort($obj->_aList, function($a, $b) { return strcmp($a->_sName, $b->_sName) }); 
+0

3cm> 199cm –

+1

是的,我明白了。你是對的。我沒有仔細看過這些值,我只是想演示如何使用'usort'。 – Pharaoh

+0

我改變了對象值 - 目標是對「字符串」進行排序 - 而不是數字。抱歉。但由於某種原因,已排序對象的var_dump看起來與未排序的完全相同。對於一些共振'usort($ obj - > _ aList,函數($ a,$ b){return strcmp($ a - > _ sName,$ b - > _ sName)});'不會做這項工作。 – user1809233

0

你可以用usort排序,

usort($array, function($a, $b){return intval($a->_sName) - intval($b->_sName);}