1

I have the following multidim array;

[features] => Array
    (
        [0] => Array
            (
                [value] => Foo
                [id_feature] => 9
            )

        [1] => Array
            (
                [value] => Bar
                [id_feature] => 10
            )

    )

Being very new to Smarty and its often butt-backwards approach to things, I have no clue where to begin, if I wanted to get a specific array based on the value of id_feature of that array.

That is to say, I'd need to put into a variable the array that for instance contains "9" for id_feature.

How would I go about this?

2
  • 1
    Just a suggestion: why not make id_feature the key for the parent array? Commented May 29, 2012 at 3:55
  • Love to, but I can't change the content of the array, as it's generated by Prestashop - and I'm looking to just make a theme more than change the behavior of the core. :) Commented May 29, 2012 at 3:58

3 Answers 3

1

Try this smarty code:

{foreach from=$features item=f}

  {if $f.id_feature eq 9}

   {$f.value}

   {$f.id_feature}

  {/if}

{/foreach}
Sign up to request clarification or add additional context in comments.

Comments

1

What about using in_array?

<?php
function getArray($search) {
   foreach($features as $val) {
      if(in_array($search, $val)){
        return $val;
      }
   }
 }
?>

Comments

1

Provided that you assigned something like this in PHP:

$features = array( 
    array("value" => "Foo", "id_feature" => 9), 
    array("value" => "Bar", "id_feature" => 10) );
$smarty->assign('features', $features);

In Smarty template, {$features[0].value} will give you Foo.

You can also traverse the array using {for} or {foreach} - check documentation http://www.smarty.net/docsv2/en/

Or even use {php} tag to write PHP directly:

{php}
  global $features;
  print_r($features);
{/php}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.