phparraysloopsmultidimensional-arrayforeach

Get all values with a certain key value


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.


Solution

  • I think you are trying to do something like this:

    // Create an array containing all of the distinct "service types"
    // from the $services array
    $service_types = array();
    foreach ($services as $service) {
        // If we have not yet encountered this entry's "service type",
        // then we need to add it to our new array
        if (!in_array($service['service_type'], $service_types)) {
            $service_types[] = $service['service_type'];
        }
    }
    if (count($service_types)) {
        // For each "service type", we want to echo a header and then
        // a list of services of that type
        foreach ($service_types as $current_type) {
            echo '<h3>'.$current_type.'</h3>';
            // Loop through the services and echo those that are
            // of the correct type
            foreach ($services as $service) {
                if ($service['service_type'] == $current_type) {
                    echo($service['service_title']);
                }
            }
        }
    } else {
        echo '<h3>No services</h3>'
    }