3

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.

Brooke.
  • 3,691
  • 13
  • 49
  • 80

3 Answers3

12

With php > 5.5 you can do

$myData = 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'
    )
);

$filtered = array_column($myData, 'service_column');

see: http://www.php.net/manual/en/function.array-column.php

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
asnyder
  • 711
  • 8
  • 16
3

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>'
}
Hammerite
  • 21,755
  • 6
  • 70
  • 91
  • Thanks that got me on the right track. I am getting this error though `Warning: in_array() [function.in-array]: Wrong datatype for second argument` Not sure what that means but I'll play with it a little bit. I would also love it if you could explain the code a little. I understand most of it but the more I understand the better. – Brooke. Sep 13 '11 at 06:05
  • _service_type_ should be quoted. `$service[service_type]` should be `$service['service_type']` on lines 3 and 4. Also, if you want to sort by service types, just add `sort($service_types);` after the first `foreach`. – Herbert Sep 13 '11 at 06:11
  • I added some comments. I'm not sure why you would get that warning, the code looks correct at a glance. – Hammerite Sep 13 '11 at 06:14
  • 1
    I did that first but it seems that after asking my friend Google the problem was that `$service_types` isn't defined when the loop first runs so it was throwing a warning. I added `$service_types=array();` before the first `if` and it worked fine. – Brooke. Sep 13 '11 at 06:15
  • You missed one: `$service_types[] = $service['service_type']` :) – Herbert Sep 13 '11 at 06:16
  • `$service_types[] = $service['service_type']` was the line having the error. (did you just add the `$service_types=array();` or did I miss that too? – Brooke. Sep 13 '11 at 06:18
  • @BandonRandon: `$service_types=array();` needs to come _before_ the foreach loop; otherwise, you'll be emptying the array on each iteration. – Herbert Sep 13 '11 at 06:19
  • @Herbert yep got that now. Thanks :P – Brooke. Sep 13 '11 at 06:21
  • `$service_types = array();` was there from the start. – Hammerite Sep 13 '11 at 06:34
  • @BandonRandon: Happy to be of assistance... if I was. Hammerite did all the work. :) – Herbert Sep 13 '11 at 06:38
  • @Hammerite, oh must have just missed it when I copied it over. Thanks for all your help. – Brooke. Sep 13 '11 at 06:48
0

I haven't followed all of your question, but you asked if you can sort the array by service type - so you can build a user defined sorting function.

function sortByServiceType($a, $b) {  
  // This will sort them by service type alphabetically
  return strcmp($a['service_type'], $b['service_type']);
}

uasort($services, 'sortByServiceType');
yoavmatchulsky
  • 2,962
  • 1
  • 17
  • 22