Zaxnyd Posted June 16, 2006 Share Posted June 16, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/ Share on other sites More sharing options...
joquius Posted June 16, 2006 Share Posted June 16, 2006 $list[$item->color] = array();you have only defined $list as an array you need one for each key value this should fix it Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46490 Share on other sites More sharing options...
Zaxnyd Posted June 16, 2006 Author Share Posted June 16, 2006 [!--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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46501 Share on other sites More sharing options...
joquius Posted June 16, 2006 Share Posted June 16, 2006 how about taking away $list=array();seeing as you'll be defining the keys anyway Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46502 Share on other sites More sharing options...
Zaxnyd Posted June 16, 2006 Author Share Posted June 16, 2006 [!--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] Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46504 Share on other sites More sharing options...
Zaxnyd Posted June 16, 2006 Author Share Posted June 16, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46513 Share on other sites More sharing options...
joquius Posted June 16, 2006 Share Posted June 16, 2006 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); Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46517 Share on other sites More sharing options...
Zaxnyd Posted June 16, 2006 Author Share Posted June 16, 2006 $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... Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46522 Share on other sites More sharing options...
joquius Posted June 16, 2006 Share Posted June 16, 2006 no no not a solution just wondering what'll happen, perhaps $item is overwriting the keys Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-46529 Share on other sites More sharing options...
Zaxnyd Posted June 26, 2006 Author Share Posted June 26, 2006 This returend properly:[code]$aList = array( "ofoo" => $oFoo, "obar" => $oBar, "ozar" => $oZar);[/code]Still looking for a solution, though... Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-49779 Share on other sites More sharing options...
Barand Posted June 26, 2006 Share Posted June 26, 2006 you were close to start with[code]function sortByColor($array){ $list = array(); foreach($array as $item){ $list[$item->color][] = $item->name; } return $list;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12205-resorting-an-associative-array/#findComment-49828 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.