PDA

View Full Version : Problem with PHP Arrays



PartickThistle
January 22nd, 2009, 05:02 PM
The answer to this is staring me in the face - I know it - I've done it many times before. I must have coders-block today.

I have an array like this;



[0] => stdClass Object
(
[GroupName] => Benchmarking
[GroupType] => Advisory Group
[numGroups] => 1
)

[1] => stdClass Object
(
[GroupName] => Communities Scotland Procurement Strategy Advisory Group
[GroupType] => Advisory Group
[numGroups] => 1
)

[2] => stdClass Object
(
[GroupName] => Consultation on the National Strategy
[GroupType] => Consultative Group
[numGroups] => 2
)


The database I'm pulling from is in a complete mess and there's no time to normalize it so it has to be PHP data manipulation;

Ideally I'm wanting the data to go into another array, pre-Smarty output that will output the data like so;



GroupType
GroupName (numGroups)
GroupName (numGroups)

GroupType
GroupName (numGroups)
GroupName (numGroups)

etc
etc
etc


Any hints?

Tibuda
January 22nd, 2009, 05:06 PM
Try this. I'm assuming your current data is stored in $data
$types = array();
foreach ($data as $row) {
if (!isset($types[$row->GroupType]))
$types[$row->GroupType] = array();
$text = $row->GroupName.' ('.$row->numGroups.')';
$types[$row->GroupType][] = $text;
}EDIT: I just see your rows are objects, but my first code was using assoc arrays.

PartickThistle
January 22nd, 2009, 06:08 PM
Thanks very much, Daniel.

Got pretty much what I want now.