lundi 5 janvier 2015

PHP Using an If-else inside multidimensional associative array

My multidimensional associative array :



$search_cookies = array(
"type_catalog" => $type_array,
"size_catalog" => $size_array,
"color_catalog" => $color_array,
);


I need to do that :



$search_cookies = array(
if(isset($type_array){
"type_catalog" => $type_array,
}
elseif(isset($size_array)){
"size_catalog" => $size_array,
}
elseif(isset($color_array)){
"color_catalog" => $color_array,
}
);


Here is the entire code if you think it must be some other way :



$first_array = array('t-1', 's-32', 't-2', 's-36');

function removeLetters($row){
return preg_replace("/[^0-9,.]/", "", $row);
}

foreach($first_array as $row){
$exp_key = explode('-', $row);
if($exp_key[0] == 't'){
$type_array[] = removeLetters($row);
}
if($exp_key[0] == 's'){
$size_array[] = removeLetters($row);
}
if($exp_key[0] == 'c'){
$color_array[] = removeLetters($row);
}
}

$search_cookies = array(
"type_catalog" => $type_array,
"size_catalog" => $size_array,
"color_catalog" => $color_array,
);

Aucun commentaire:

Enregistrer un commentaire