Jump to content


Photo

Resorting an Associative Array


  • Please log in to reply
10 replies to this topic

#1 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 16 June 2006 - 10:39 PM

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?


//...(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"
    }
  }
}
*/


#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 16 June 2006 - 10:44 PM

$list[$item->color] = array();

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

#3 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 16 June 2006 - 10:56 PM

[!--quoteo(post=384796:date=Jun 16 2006, 05:44 PM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 16 2006, 05:44 PM) View Post[/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:

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

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

#4 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 16 June 2006 - 11:00 PM

how about taking away $list=array();

seeing as you'll be defining the keys anyway
z..z..z..z..z..z..z..z..

#5 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 16 June 2006 - 11:10 PM

[!--quoteo(post=384808:date=Jun 16 2006, 06:00 PM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 16 2006, 06:00 PM) View Post[/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;



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"
    }
  }
}
*/



#6 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 16 June 2006 - 11:27 PM

I even tried this...
function sortByColor($array){
    $list = array();
    foreach($array as $item){
      if($list[$item->color] == null)
        $list["test"] = array();
      $list["test"][] = $item;
    }
    return $list;
}
And got...
array(1) {
  [1]=>      //Why doesn't this say ["test"]???
  array(1) {
    ["Zar"]=>
    object(item)(1) {
      ["name"]=>
      string(3) "Zar"
      ["color"]=>
      string(4) "blue"
    }
  }
}


#7 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 16 June 2006 - 11:31 PM

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);
z..z..z..z..z..z..z..z..

#8 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 16 June 2006 - 11:40 PM

$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...

#9 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 16 June 2006 - 11:51 PM

no no not a solution just wondering what'll happen, perhaps $item is overwriting the keys
z..z..z..z..z..z..z..z..

#10 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 26 June 2006 - 07:30 PM

This returend properly:
$aList = array(
    "ofoo" => $oFoo, 
    "obar" => $oBar, 
    "ozar" => $oZar
);

Still looking for a solution, though...

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 26 June 2006 - 10:09 PM

you were close to start with
function sortByColor($array){
    $list = array();
    foreach($array as $item){
      $list[$item->color][] = $item->name;  
    }
    return $list;
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users