1

I'm having some troubles getting information from an array. I'm in need to get all information per key basis but I can't get it.

I have this array:

Array(
[en] => Array(
    [a] => Array(
        [0] => [C][C]
        [1] => [L][L][C]
    ) [b] => Array(
        [0] => Tackle
        [1] => RazorLeaf
    ) [c] => Array(
        [0] => 
        [1] =>
    ) [d] => Array(
        [0] => 20
        [1] => 50
    )
) [pt] => Array(
    [a] => Array(
        [0] => [C][C]
    ) [b] => Array(
        [0] => Pontapé
    ) [c] => Array(
        [0] =>
    ) [d] => Array(
        [0] => 20
    )
)
)

In a foreach (or multiple), I'm in need to get [en][a][0], [en][b][0], [en][c][0] and [en][d][0] to insert data into database. In the next loop is supposed to get [en][a][1], [en][b][1], [en][c][1] and [en][d][1] . Last but not least, after change from [en] to [pt] it should get [pt][a][0], [pt][b][0], [pt][c][0] and [pt][d][0]

My approach:

                        foreach($result as $language => $index){
                        foreach($index as $attinfo => $index2){
                            //echo "$language <br/> $attinfo <br/>";
                            foreach($index2 as $valorfinal => $index3){
                                echo $index[$attinfo][$valorfinal][$index3]."<br/>";
                            }
                        } 

                    }

My approach seems not to work as expected. Could someone guide me please? I would like to save in vars each key to then save them in database in each foreach loop. Thanks.

3
  • user nested foreach() to get that Commented Oct 3, 2018 at 16:13
  • 1
    I have an answer to this problem on another question, and this answer works on ANY dimensional size array, "Iterate through every single key and value in a multi-dimensional associative array PHP" : stackoverflow.com/a/52508096/2430549 Commented Oct 3, 2018 at 16:28
  • @HoldOffHunger your approach is interesting. Thanks for sharing that :) Commented Oct 4, 2018 at 11:13

1 Answer 1

3

I've re-organised the loops and corrected the indexes used in the final data access (you are using [$index3] as an index when it's a value)...

foreach($result as $index){
    foreach($index['a'] as $key => $value){
        $data = [];
        foreach ( $index as $key1 => $value1)   {
            $data[] = $index[$key1][$key];
        }
        print_r($data);
    }
}

this outputs...

Array
(
    [0] => [C][C]
    [1] => Tackle
    [2] => 
    [3] => 20
)
Array
(
    [0] => [L][L][C]
    [1] => RazorLeaf
    [2] => 
    [3] => 50
)
Array
(
    [0] => [C][C]
    [1] => Pontapé
    [2] => 
    [3] => 20
)

To include the language in the output...

$output = [];
foreach($result as $language => $index){
    foreach($index['a'] as $key => $value){
        $data = [];
        foreach ( $index as $key1 => $value1)   {
            $data[] = $index[$key1][$key];
        }
        $data[] = $language;
        $output[] = $data;
    }
}

print_r($output);

Gives (partial output)...

Array
(
    [0] => [C][C]
    [1] => Tackle
    [2] => 
    [3] => 20
    [4] => en
)
Sign up to request clarification or add additional context in comments.

8 Comments

your approach is right, but it confused me. So now I don't have the first key [en] or [pt] that is in array. I need that value to insert it in database too. Also, with your approach, with a foreach, how should I access all information one by one?
To get the first key, use foreach($result as $language => $index){ and $language should be the [en] value. Not sure about what you mean by how should I access all information one by one
I got that answer and before asking I achieved the same result , but I would like to have it as key too [4]. Instead of language before each pack of values,have it inside [0][1][2][3][4] - where the 4 would be the language. Is this possible?
I've updated the answer (last code segment) (changed language addition to $data[] = $language;)
It creates everything in the same "array" instead of creating as much instances as exists: Array( [0] => [C][C][1] => Tackle[2] => [3] => 20[4] => en[5] => [L][L][C][6] => RazorLeaf[7] => [8] => 50[9] => en[10] => [C][C][11] => Pontapé[12] => [13] => 20[14] => pt )
|

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.