Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all values with a certain key value [duplicate]

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.

like image 381
Brooke. Avatar asked Jun 05 '26 22:06

Brooke.


1 Answers

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'
    )
);
// Replace 'service_column' with actual column name
$filtered = array_column($myData, 'service_column');

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

like image 96
asnyder Avatar answered Jun 08 '26 13:06

asnyder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!