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

  • SEARCH
  • Home
  • 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 8463551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:29:09+00:00 2026-06-10T14:29:09+00:00

I have a multidimensional array in PHP that looks like this: Array ( [0]

  • 0

I have a multidimensional array in PHP that looks like this:

Array
(
    [0] => Array
        (
            [category_id] => 1
            [name] => Football Team Name 1
        )
    [1] => Array
        (
            [category_id] => 2
            [name] => Football Team Name 2
        )
    [2] => Array
        (
            [category_id] => 3
            [name] => Hockey Team Name 1
        )
    [3] => Array
        (
            [category_id] => 4
            [name] => Hockey Team Name 2
        )
    [4] => Array
        (
            [category_id] => 5
            [name] => Hockey Team Name 3
        )

The first word (of the array’s key that is called name) I always use as a category title, thus, I would like to but so far couldn’t figure this out of how to make the final output look like this:

Output Example

The solution, in my case, should be eventually used with Smarty.

I would be appreciate for any ideas over 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-06-10T14:29:10+00:00Added an answer on June 10, 2026 at 2:29 pm

    You should prepare the data before parsing it to Smarty.

    You could do like this:

    $result = array(
        array('name' => 'Hockey Team 1', 'category_id' => 1),
        array('name' => 'Hockey Team 2', 'category_id' => 2),
        array('name' => 'Hockey Team 3', 'category_id' => 3),
        array('name' => 'Football Team 1', 'category_id' => 4),
        array('name' => 'Football Team 2', 'category_id' => 5),
        array('name' => 'Football Team 3', 'category_id' => 6)
    );
    
    $sports = array();
    
    foreach ($result as $team) {
        $sport_parts = explode(' ', $team['name']);
        $sport = array_shift($sport_part);
        $team['name'] = join(' ', $sport_parts);
        if (isset($sports[$sport]) === FALSE) {
            $sports[$sport] = array();
        }
        $sports[$sport][] = $team;
    }
    

    This would produce a data set like this, which is easy to iterate over in Smarty:

    array(2) {
      ["Hockey"]=>
      array(3) {
        [0]=>
        array(2) {
          ["name"]=>
          string(13) "Team 1"
          ["category_id"]=>
          int(1)
        }
        [1]=>
        array(2) {
          ["name"]=>
          string(13) "Team 2"
          ["category_id"]=>
          int(2)
        }
        [2]=>
        array(2) {
          ["name"]=>
          string(13) "Team 3"
          ["category_id"]=>
          int(3)
        }
      }
      ["Football"]=>
      array(3) {
        [0]=>
        array(2) {
          ["name"]=>
          string(15) "Team 1"
          ["category_id"]=>
          int(4)
        }
        [1]=>
        array(2) {
          ["name"]=>
          string(15) "Team 2"
          ["category_id"]=>
          int(5)
        }
        [2]=>
        array(2) {
          ["name"]=>
          string(15) "Team 3"
          ["category_id"]=>
          int(6)
        }
      }
    }
    

    An example at a Smarty template:

    {foreach from=$sports key=sport item=teams}
      <h1>{$sport}</h1>
        <ul>
          {foreach from=$teams item=team}
            <li>{$team['name']}</li>
          {/foreach}    
        </ul>
    {/foreach}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an multidimensional array that looks like this: Array ( [0] => Array
I have a multidimensional array that looks like this [0] => Array ( [recordId]
I have a multidimensional array in PHP, something that looks like: array(array(Category => Video,
I have a multidimensional array which, when echoed in PHP, looks like this: {
I have 2 arrays in PHP that look something like this: $rows = array(11,12,14,14,11,13,12,11);
I have a PHP class that stores a complex multidimensional array, and rather than
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
Possible Duplicate: php multi-dimensional array remove duplicate I have an array like this: $a
I have a massive multidimensional array that has been serialised by PHP. It has
I have a large multidimensional array structured like the sample below. [ ['name' =>

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.