pepebe
8/6/2013 - 7:01 AM

MODX Snippet: Simple snippet to check if something is part of a comma delimited list.

MODX Snippet: Simple snippet to check if something is part of a comma delimited list.

<?php
/*
[[!in_array? 
  &needle=`[[*template]]` 
  &haystack=`3,4` 
  &condition=`in_array` 
  &then=`do this` 
  &else=`do that` 
]]
*/

$n = !empty($needle)    ? $needle    : "";   // needle
$h = !empty($haystack)  ? $haystack  : "";   // haystack
$c = !empty($condition) ? $condition : "in_array"; // condition

if(empty($n) || empty($h) || empty($c) ) {
    return;
}

$h = explode(",",$h);

switch($c){
    case "!in_array":
        if(!in_array($n,$h)){
            return $then;
        }
        else {
            return $else;
        }
        break;
    default:
        if(in_array($n,$h)){
            return $then;
        }
        else {
            return $else;
        }
        break;
}