Jump to content

Archived

This topic is now archived and is closed to further replies.

Zaxnyd

Resorting an Associative Array

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]

Share this post


Link to post
Share on other sites
$list[$item->color] = array();

you have only defined $list as an array you need one for each key value this should fix it

Share this post


Link to post
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\" /]

Share this post


Link to post
Share on other sites
how about taking away $list=array();

seeing as you'll be defining the keys anyway

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
have u checked the result of $item->color? I suppose you have. The only other thing I can think of just for curiosity is to set

$aList = array([ofoo] => $oFoo,[obar] => $oBar,[ozar] => $oZar);

Share this post


Link to post
Share on other sites
$item->color is returning correctly. Your most recent solution unfortunately won't suffice in my situation, as I won't actually know the number of items or colors being dealt with...

Share this post


Link to post
Share on other sites
no no not a solution just wondering what'll happen, perhaps $item is overwriting the keys

Share this post


Link to post
Share on other sites
This returend properly:
[code]$aList = array(
    "ofoo" => $oFoo,
    "obar" => $oBar,
    "ozar" => $oZar
);
[/code]

Still looking for a solution, though...

Share this post


Link to post
Share on other sites
you were close to start with
[code]
function sortByColor($array){
    $list = array();
    foreach($array as $item){
      $list[$item->color][] = $item->name;  
    }
    return $list;
}[/code]

Share this post


Link to post
Share on other sites

×

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.