Aaron Mahlke Aaron Mahlke - 10 months ago 85
PHP Question

Get the difference between one sub-Array or multiple sub-Arrays in a multidimensional Array

I have following multidimensional Array and I want to get the difference, if there is just one sub Array or multiple in that array.

For Example:


  • In Array [1] there is just one sub Array [example]

  • In Array [2] there are two sub Arrays [example]





[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)





Now to get the [value] from the first Array I would try:

foreach ($content as $example) {
echo($content['example']['value']);
}


And to get each [value] from the second Array I would try:

foreach ($content as $example) {
foreach ($example as $values) {
echo($value['value']);
}
}


So far so good but how do I decide which function to run? Am I missing something?
Is there an if-statement which can help me there?

Something like:

if(multiple sub-arrays){
// do first code example
} else {
// do second code example
}


I simply want a method to get all values called [value] out of the array.

Thank you in advance!

Answer Source

The most obvious solution is to change function which generates your content array so as it always generates sub arrays in a format like:

[content] => Array
        (
            [...]
            [1] => Array
                (
                    [example] => Array
                        (
                            [0] => Array
                                (
                                    [value] => GET THIS
                                    [attr] => Array
                                        (
                                            [...]
                                        )
                                )
                        )
                )
            [2] => Array
                (
                    [example] => Array
                        (
                            [0] => Array
                                (
                                    [value] => GET THIS
                                    [attr] => Array
                                        (
                                           [...]
                                        )
                                )
                            [1] => Array
                                (
                                    [value] => GET THIS
                                    [attr] => Array
                                        (
                                           [...]
                                        )
                                )
                        )
                )

But if you don't have such option - then use a simple check:

foreach ($content as $item) {
    // here check if your `$item` has an `value` subkey under `example` key
    if (array_key_exists('value', $item['example'])) {
         echo($item['example']['value']);
    } else {
         foreach ($item['example'] as $values) {
             echo ($values['value']);
         }
    }
}