Reputation: 601
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.
Upvotes: 2
Views: 4379
Reputation: 11869
store it in array
and return the array at the end:
function __EachReturn($Array){
$List=array();
foreach($Array as $Key=>$Arr){
$List[] = $Arr;//change is made here use array to store all values
}
return $List;//return the array.
}
UPDATE: seeing the
Before I'm using like this
part from your question, i'll suggest to use your function __EachReturn
like this:
function __EachReturn($Key,$Array){
$Styled=array();
foreach($Array as $key=>$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;
}
$ItemsList=array();
foreach($Style as $Key=>$Items){
$ItemsList[] = __EachReturn($Key,$Items);//$ItemsList will consist all the script/link ....
}
Upvotes: 4
Reputation: 18577
Try this,
function __EachReturn($Array){
$List = [];
foreach($Array as $Key=>$Arr){
$List[] = $Arr; // your loop record was replacing in every loop. now will be captured in array.
}
return $List;
}
You need to capture foreach data in array.
Upvotes: 2
Reputation: 12834
It is not returning first value from array, it is returning the last value.
function __EachReturn($Array){
foreach($Array as $Key=>$Arr){
$List = $Arr; //here $List is always getting overwritten by $Arr
}
return $List; //when loop finishes, final $Arr is in $List and it is returned
}
In order to have all the values returned, store in array as @Suchit suggests.
Upvotes: 1