Jump to content

Recommended Posts

I have objects (with values "color" and "name") in an array. I want a function that will take this array and return a two level, associative array indexed by color. For example, my array initially contains "foo", "bar", and "zar", each having the colors "red", "red", and "blue" respectively. The result I'm looking for is an array containing the arrays "red" (containing "foo" and "bar") and "blue" (containing "zar"). With the function below, instead of getting an associative array as expected, I'm getting a numerated one. Can anyone tell me why?

[code]

//...(class definitions, instantiations, etc)...//

function sortByColor($array){
    $list = array();
    foreach($array as $item){
      $list[$item->color][] = $item;  //this isn't working right... syntax problem?
    }
    return $list;
}

$oFoo->name = "Foo";
$oFoo->color = "red";

$oBar->name = "Bar";
$oBar->color = "red";

$oZar->name = "Zar";
$oZar->color = "blue";

$aList = array($oFoo,$oBar,$oZar);

$aSortedList = sortByColor($aList);

var_dump($aList );
/*yelds:
array(3) {
  [0]=>
  object(item)(1) {
    ["name"]=>
    string(3) "Foo"
    ["color"]=>
    string(3) "red"
  }
  [1]=>
  object(item)(1) {
    ["name"]=>
    string(3) "Bar"
    ["color"]=>
    string(3) "red"
  }
  [2]=>
  object(item)(1) {
    ["name"]=>
    string(3) "Zar"
    ["color"]=>
    string(4) "blue"
  }
}
*/


var_dump($aSortedList );

/*  Desired (not actual) result...
array(2) {
  ["red"]=>
  array(2) {
    ["Foo"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Foo"
      ["color"]=>
      string(3) "red"
    }
    ["Bar"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Bar"
      ["color"]=>
      string(3) "red"
    }
  }
  ["blue"]=>
  array(1) {
    ["Zar"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Zar"
      ["color"]=>
      string(4) "blue"
    }
  }
}
*/
[/code]
Link to comment
https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/
Share on other sites

[!--quoteo(post=384796:date=Jun 16 2006, 05:44 PM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 16 2006, 05:44 PM) [snapback]384796[/snapback][/div][div class=\'quotemain\'][!--quotec--]
$list[$item->color] = array();

you have only defined $list as an array you need one for each key value this should fix it
[/quote]
Thanks for the quick response! I just tried this, to no avail:

[code]
function sortByColor($array){
    $list = array();
    foreach($array as $item){
      if($list[$item->color] == null)
        $list[$item->color] = array();
      $list[$item->color][] = $item;
    }
    return $list;
}
[/code]

I still get a numerated, not associative, array result [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]
[!--quoteo(post=384808:date=Jun 16 2006, 06:00 PM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 16 2006, 06:00 PM) [snapback]384808[/snapback][/div][div class=\'quotemain\'][!--quotec--]
how about taking away $list=array();

seeing as you'll be defining the keys anyway
[/quote]
A great idea. However, still not working... It's strange; it's being grouped properly, but it's just not making it associative. It seems "$list[$item->color][] = $item;" is not passing "$item->color" correctly. It seems like a syntactical error, but I've tried all these to no avail:

$list['$item->color'][] = $item;
$list["$item->color"][] = $item;
$list["".$item->color.""][] = $item;


[code]

var_dump($aSortedList );
/* actual result (not associative)
array(2) {
  [0]=>      //I want this to say ["Red"]
  array(2) {
    ["Foo"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Foo"
      ["color"]=>
      string(3) "red"
    }
    ["Bar"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Bar"
      ["color"]=>
      string(3) "red"
    }
  }
  [1]=>      //I want this to say ["Blue"]
  array(1) {
    ["Zar"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Zar"
      ["color"]=>
      string(4) "blue"
    }
  }
}
*/

[/code]
I even tried this...
[code]
function sortByColor($array){
    $list = array();
    foreach($array as $item){
      if($list[$item->color] == null)
        $list["test"] = array();
      $list["test"][] = $item;
    }
    return $list;
}
[/code]
And got...
[code]
array(1) {
  [1]=>      //Why doesn't this say ["test"]???
  array(1) {
    ["Zar"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Zar"
      ["color"]=>
      string(4) "blue"
    }
  }
}
[/code]
  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.