2

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.

7
  • You are overriding the value of $List in each execution of your foreach loop. You need to declare $List as an array and use $List[] = $Arr; in your foreach. Commented Jan 17, 2017 at 11:06
  • 1
    Still, I don't understand the point of this function which transforms an array into... an array. Commented Jan 17, 2017 at 11:07
  • why that function though, you could use foreach directly Commented Jan 17, 2017 at 11:20
  • $List = $Arr; Over writes the scalar variable $List so you only see that last one added. Use $List[] = $Arr; to build an array _But I see no point in that code being a function Commented Jan 17, 2017 at 11:22
  • because without function i need to use foreach couple of time. so i don't want to do this. Actually i'm new in php so I thought my code will be more shrink if i do that. Commented Jan 17, 2017 at 11:23

3 Answers 3

4

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 ....
}
Sign up to request clarification or add additional context in comments.

6 Comments

I have updated my question with complete code kindly check it this is what i'm trying to do.
@MuhammadHamzaNisar i'll suggest not to use the function, you can directly use $Items(it is the resultant array any how from your function). Let the key in switch be dynamic.You don't need foreach here.
it's did not work i did it. So that is why i use foreach if remove foreach it's give me error undefined index like rel,href
In that case best option for you is to move your switch case in your function. looks like $Items is a multidimensional array. your function __EachReturn is not doing anything just returning the same array.
can you update your answer with this option as you suggest.
|
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.