streetparade
streetparade

Reputation: 32908

How to Create an Array an Look if a value is in Array

How do I create an array in smarty from a given string like 22||33||50 and look if the given number is like the numbers above in smarty ?

I have a string say

{$test->strings} // contains 33||12||80 

I want to look if one of the numbers in {$test->strings} is equal to {$test->myday}

Upvotes: 0

Views: 159

Answers (3)

Crozin
Crozin

Reputation: 44386

You shouldn't do a such things in template. You should do necessary operations in you application logics (PHP) and pass the results to the template.

By the way: Smarty (and all Smarty-like engines) is a piece of... rubbish.

Upvotes: 0

nickf
nickf

Reputation: 546263

You really shouldn't be getting your view (smarty template) to perform any data manipulation, in my opinion. I would convert your string to an array before you send it to the template.

$str = "33||12||80";
$array = explode("||", $str);  // [33, 12, 80]

if (in_array($test->myday, $array)) {
    // it's in there
}

Upvotes: 1

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124828

Don't know about Smarty but this is how you'd do it in pure PHP:

if(in_array($test->myday, explode('||', $test->strings))) {
    // strings contains myday
}

Hope that helps.

Upvotes: 1

Related Questions