0

I am looping though one initial array, and specifying values i want. Then i am storing the values i need in an array. Then i am combining those values into a new array, with keys and values. The new array is only storing the last entry of all the data that has been passed to it.

$exampleArray = array();
for ($i = 0; $i < 100; $i++){
    $exampleArray[] = array(
        $A1 =  $Anotherarray[$i][25],
        $A2 = $Anotherarray[$i][26],
        $A3 = $Anotherarray[$i][24],
        $A4 = $Anotherarray[$i][27],
        $A5 = $Anotherarray[$i][28]
    );

    $secondExample = array();

    foreach( $A1 as $i => $val )
    {
        $secondExample[] = array(
            "Field1" => $val,
            "Field2" => ucfirst($A2[$i]),
            "Field3" => ucfirst($A3[$i]),
            "Field4" => ucfirst($A4[$i]),
            "Field5" => ucfirst($A5[$i])
        );
    }
4
  • 3
    Because you're overwriting it with an empty array on each iteration. Commented Aug 31, 2017 at 14:32
  • You're overwriting the values like that. Commented Aug 31, 2017 at 14:33
  • How can i do this without overwriting the values? Commented Aug 31, 2017 at 14:34
  • array($A1 = 'foo') is the same as $A1 = 'foo'; array('foo'). Somehow I doubt that's the intention. Commented Aug 31, 2017 at 14:36

2 Answers 2

3

You are declaring $secondExample as a new array on every iteration. Do it like this:

$exampleArray = array();
$secondExample = array();

for ($i = 0; $i < 100; $i++){
    $exampleArray[] = array(
        $A1 = $Anotherarray[$i][25],
        $A2 = $Anotherarray[$i][26],
        $A3 = $Anotherarray[$i][24],
        $A4 = $Anotherarray[$i][27],
        $A5 = $Anotherarray[$i][28]
    );

    $secondExample[$i] = array();
    foreach( $A1 as $j => $val) {
        $secondExample[$i][] = array(
        "Field1" => $val,
        "Field2" => ucfirst($A2[$j]),
        "Field3" => ucfirst($A3[$j]),
        "Field4" => ucfirst($A4[$j]),
        "Field5" => ucfirst($A5[$j])
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Doing this in to loops running at the same time is causing my browser to become very slow and not load. I am looping though a lot of data, is there a way to do this and not impact performance?
I think there was kind of a mess with the indices. Try now. Even then, I don't really get what you're trying to do so I'm afraid without knowing more I can't help you simplify this... It is O(n^2) so with a lot of data you'll get bad performance.
Thanks, it loads now, but for some reason i am still only getting the last entry
0

As because you are overwriting every time the loop goes.Use array_push

 $secondExample = array();

foreach( $A1 as $i => $val )
{
    $varArray = array(
        "Field1" => $val,
        "Field2" => ucfirst($A2[$i]),
        "Field3" => ucfirst($A3[$i]),
        "Field4" => ucfirst($A4[$i]),
        "Field5" => ucfirst($A5[$i])
    );
    array_push($secondExample, $varArray);
}

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.