So I've been trying to figure this out for a while now and seem to be stuck. I have a multidimensionalarray and I'm trying to output all values with a certain key. Here is the code:
//My Array:
$services = array(
array( service_name => 'facebook',
service_title => 'Facebook',
service_order => 0,
service_type => 'Social Networking'
),
array( service_name => 'twitter',
service_title => 'Twitter',
service_order => 0,
service_type => 'Social Networking'
),
array( service_name => 'tumblr',
service_title => 'MySpace',
service_order => 0,
service_type => 'Blogging'
)
);
The problem is I want to output the values by service_type
for example "Social Networking" or "Blogging" I'm able to do this if I use a foreach statement like so:
foreach($services as $service) {
if ($service['service_type'] == 'Social Networking') {
echo($service['service_title']);
}
if ($service['service_type'] == 'Blogging') {
echo($service['service_title']);
}
}
which works fine until I try to add a title in between types like
foreach($services as $service) {
if ($service['service_type'] == 'Social Networking') {
echo($service['service_title']);
}
echo ('<h3> Blogging</h3>');
if ($service['service_type'] == 'Blogging') {
echo($service['service_title']);
}
}
I've learned enough to know that is because of the way foreach
loops work so I've been trying to do this with something like while($service['service_type'] == 'Social Networking')
(which creates a fun infinite loop) and various other ways.
Ideally I want to sort the array by service_type
and then display the results without having to run the foreach
loop in between headers.
Any help on this would be awesome or even other suggestions on ways to do this.