I don't understand why foreach is only return first value from array.
function __EachReturn($Array){
foreach($Array as $Key=>$Arr){
$List = $Arr;
}
return $List;
}
This is my complete function:
// Styled Function
function Returnstyled($Style){
$Styled = array();
function __EachReturn($Array){
foreach($Array as $Key=>$Arr){
$List = $Arr;
}
return $List;
}
foreach($Style as $Key=>$Items){
$Items = __EachReturn($Items);
switch($Key){
case 'CSS':
$Styled[] = sprintf('<link rel="%s" href="%s" type="%s">',$Items['rel'],$Items['href'],$Items['type']);
break;
case 'JS':
$Styled[] = sprintf('<script src="%s" integrity="%s" crossorigin="%s"></script>',$Items['src'],$Items['integrity'],$Items['crossorigin']);
break;
}
}
return $Styled;
}
Before I'm using like this:
function Returnstyled($Style){
$Styled = array();
foreach($Style as $Key=>$Items){
switch($Key){
case 'CSS':
foreach($Items as $Item){
$Styled[] = sprintf('<link rel="%s" href="%s" type="%s">',$Item['rel'],$Item['href'],$Item['type']);
}
break;
case 'JS':
foreach($Items as $Item){
$Styled[] = sprintf('<script src="%s" integrity="%s" crossorigin="%s"></script>',$Item['src'],$Item['integrity'],$Item['crossorigin']);
}
break;
}
}
return $Styled;
}
So What i did. I don't want to use foreach inside switch case couple of time so that is why i try to generate nested function i create inside it one more function EeachReturn but it's only return first from array.
It has to be return complete array.
$Listin each execution of yourforeachloop. You need to declare$Listas an array and use$List[] = $Arr;in yourforeach.$List = $Arr;Over writes the scalar variable$Listso you only see that last one added. Use$List[] = $Arr;to build an array _But I see no point in that code being a functionfunctioni need to useforeachcouple of time. so i don't want to do this. Actually i'm new inphpso I thought my code will be more shrink if i do that.