-1

I have the following Object:

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

I would like to sort the array by the _sName ($obj->getName()).

Is there any way I can achieve this by php?

eidt: I cahnged the _sName because of some confusion :)

Thanks in advance.

Alex
  • 41
  • 2
  • 6

2 Answers2

0

You can sort it with usort,

usort($array, function($a, $b){return intval($a->_sName) - intval($b->_sName);}
LF00
  • 27,015
  • 29
  • 156
  • 295
0

You can build your own sorting function with usort:

uasort($obj->_aList, function($a, $b) { return strcmp($a->_sName, $b->_sName) });
Imanuel
  • 3,596
  • 4
  • 24
  • 46
  • 1
    Yeah, I get it. You're right. I haven't looked that closely at the values, I just wanted to demonstrate how to use `usort`. – Imanuel May 09 '17 at 14:59
  • I changed the Object values - the goal is to sort "strings" - not numbers. Sorry. But for some reason a var_dump of the sorted Object looks exactly the same like the unsorted. For some reson `usort($obj->_aList, function($a, $b) { return strcmp($a->_sName, $b->_sName) });` wont do the job. – Alex May 09 '17 at 15:30
  • Check if you can access all fields you need to. I suspect not because they are `protected` - in this case you need to change `$a->_sName` to `$a->getName()` as you have mentioned and you have to see wether you can extract the array to sort it and then write it back. – Imanuel May 09 '17 at 15:52
  • I get a sorted array, but the indexes of the array are recreated. But I need the old indexes (02ce605576fee3181f11ebd2c87baed0). – Alex May 09 '17 at 15:58
  • Then you simply need to use `uasort` instead of `usort`. Answer is edited. – Imanuel May 09 '17 at 16:20