Question

I dynamically create this associative file array through a PHP script, and now I am wondering how do I select all values inside the root array? I'll explain here in code.

This is the array that is currently getting generated everytime:

Array
(
    [HTML] => Array
        (
            [0] => Index.php
        )

[Javascript] => Array
    (
        [0] => Javascript.js
        [1] => Jquery.js
    )

[0] => New Text Document.txt
[Scripts] => Array
    (
        [0] => Get_Server_files.Script.php
    )

[Style] => Array
    (
        [0] => General.css
        [1] => Menu.css
        [2] => Style.css
    )

[images] => Array
    (
        [WelcomeImages] => Array
            (
                [0] => WelcomeImage0.png
                [1] => WelcomeImage1.png
                [2] => WelcomeImage2.png
                [3] => WelcomeImage3.png
                [4] => WelcomeImage4.png
                [5] => WelcomeImage5.png
                [6] => WelcomeImage6.png
            )

        [0] => bg.jpg
    )

)

From here, how do I select every first value? As in selecting

  • HTML
  • Javascript
  • Scripts
  • Style
  • Images.

And not anything that is inside these arrays.

I have searched but everytime it's not quite what I would like.

Was it helpful?

Solution

You can use array_keys() to get the names and then use array_filter() to remove the 0 values:

$names = array_filter(array_keys($data));
print_r($names);

Output:

Array
(
    [0] => HTML
    [1] => Javascript
    [3] => Scripts
    [4] => Style
    [5] => images
)

Demo.

OTHER TIPS

Use array_keys()

$keys = array_keys($array);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top