3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $vars = ['len' => '-1', 'start' => '0', 'cNum' => '7', 'cSearch_0' => 'b', 'cSearch_1' => 'c', 'cSearch_2' => 'd', 'cSearch_3' => 'e', 'cSearch_4' => 'f', 'cSearch_5' => 'g', 'cSearch_6' => 'h']; // $v = preg_grep('/[cSearch]\_/', array_keys($vars)); $cSearch = array_filter($vars, function($k){ return preg_match('/(cSearch)/',$k);}, ARRAY_FILTER_USE_KEY); var_dump($cSearch); if(!empty($cSearch)){ $searchColOutStr = ',searchCols: ['; $searchColOut = []; for ($i = 0; $i < $vars['cNum']; $i++){ if(array_key_exists('cSearch_' . $i, $cSearch)){ $searchColOut[] = '{ "search": "' . $cSearch['cSearch_' . $i] . '"}'; }else{ $searchColOut[] = 'null'; } } $searchColOutStr .= implode(',', $searchColOut); $searchColOutStr .= ']'; } var_dump($searchColOutStr);
Output for 7.3.0rc3
array(7) { ["cSearch_0"]=> string(1) "b" ["cSearch_1"]=> string(1) "c" ["cSearch_2"]=> string(1) "d" ["cSearch_3"]=> string(1) "e" ["cSearch_4"]=> string(1) "f" ["cSearch_5"]=> string(1) "g" ["cSearch_6"]=> string(1) "h" } string(133) ",searchCols: [{ "search": "b"},{ "search": "c"},{ "search": "d"},{ "search": "e"},{ "search": "f"},{ "search": "g"},{ "search": "h"}]"