I have this array $node_actions
:
Array
(
[new] => yes
[fav] => no
[eye] => no
[edit] => yes
[delete] => yes
)
And sometimes it comes mince some element( like new
or eye
for example).So I need to define a variable that has this actions:
$actions = $new.' '.$fav.' '.$eye.' '.$edit.' '.$delete;
Here's how I do it :
if(!isset(($node_actions['new'])){
$actions =$fav.' '.$eye.' '.$edit.' '.$delete;
}elseif(!isset(($node_actions['fav'])){
$actions =$new.' '.$eye.' '.$edit.' '.$delete;
}elseif(!isset(($node_actions['eye'])){
$actions =$new.' '.$fav.' '.$edit.' '.$delete;
}elseif(!isset(($node_actions['edit'])){
$actions =$new.' '.$fav.' '.$eye.' '.$delete;
}elseif(!isset(($node_actions['delete'])){
$actions =$new.' '.$fav.' '.$eye.' '.$edit;
}elseif(!isset(($node_actions['new']) && !isset(($node_actions['new']) ){
$actions =$eye.' '.$edit.' '.$delete;
}else{
} //.... and so on
is there an elegant easy way to do the same ? Much appreciated.
try doing:
$actions = "";
foreach($yourArr as $key => $val) {
$actions .= $$key. " ";
}
$actions = trim($actions);
try with loop
foreach($arr as $k=>$v) {
if(!empty($v)) {
$actions .= $v.' '; // yes no no yes yes
//$actions .= $k.' '; //new fav eye edit delete
}
}
echo $actions;
i prefer this approach in this $new = 1; $fav = 2; $eye = 4; $edit = 8; $delete = 16; are variables bound to some specific task and they are matched to some variable ($action in this case ) coming from GET or POST
<?php
$new = 1;
$fav = 2;
$eye = 4;
$edit = 8;
$delete = 16;
$action = $fav | $eye; // OR $fav | $eye | $delete OR $new
if($action & $new) {
echo "new";
}
if($action & $fav) {
echo "fav";
}
if($action & $eye) {
echo "eye";
}
if($action & $edit) {
echo "edit";
}
if($action & $delete) {
echo "delete";
}
?>
Hope this will help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.