简体   繁体   中英

Parsing a multidimensional array in PHP

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:

输出范例

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

I would be appreciate for any ideas over this.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM