-1

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.

4
  • 1
    The "proper" implementation is to not have logic in your templates ...
    – user895378
    Aug 29, 2012 at 15:03
  • That I have. If I got you right - I never put(make) Smarty execute PHP from the templates!
    – Ilia Ross
    Aug 29, 2012 at 15:09
  • I think wat @rdlowrey mean is that you should try to solve this problem using Smarty, even if you used only smart syntax, but like in my answer solve the problem in PHP. Not so much not to execute PHP in you templates.
    – Michael
    Aug 29, 2012 at 16:49
  • I know, I agree and I'm not solving this problem using Smarty. What makes, you guys, think this?
    – Ilia Ross
    Aug 29, 2012 at 16:51

1 Answer 1

4

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}
10
  • Looks nice. I will run it and come back a bit later. Thanks!
    – Ilia Ross
    Aug 29, 2012 at 15:25
  • Everything is perfect! I like it and it works like expected! :) Thank you!
    – Ilia Ross
    Aug 29, 2012 at 15:40
  • What would be the best way to take the first word away from key name. Now the returned array looks like Football Team 1, Football Team 2 and etc., in the result I'd like to see only Team 1, Team 2 and etc. as it's already under the title Football there is not need in saying it again?
    – Ilia Ross
    Aug 29, 2012 at 16:27
  • @IliaRostovtsev I've updated the example to do this. (not tested)
    – Michael
    Aug 29, 2012 at 16:36
  • Maybe it's better to use array_diff?
    – Ilia Ross
    Aug 29, 2012 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.