Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6558085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:09:22+00:00 2026-05-25T13:09:22+00:00

So I’ve been trying to figure this out for a while now and seem

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:09:22+00:00Added an answer on May 25, 2026 at 1:09 pm

    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>'
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.