0

I have an array called $plugins that looks something like this :

Array
(
    [path/to/file.php] => Array
        (
            [Name] => somevalue_a
            [TextDomain] => somevalue_b
            [value_c] => somevalue_c
            [value_d] => somevalue_d
            ...
            ...
            ..

        )
    [path/to/file2.php] => Array
        (
            [Name] => somevalue_a
            [TextDomain] => somevalue_b
            [value_c] => somevalue_c
            [value_d] => somevalue_d
            ...
            ...
            ..
        )
)

Now, I am having trouble to get the KEY name (which is path)of each array element ..

function get_plugin_data(){

    foreach ($plugins as $plugin => $data) {
    $plugin_data = $plugins[$plugin];

    // Start simple DEBUG
    echo '</br>===============================</br>' ;
    echo '</br><b>Plugin Name : </b>'. $data[Name]; .'</br>' ;
    echo '</br><b>Plugin Path : </b>'. key($plugins)   .'</br>' ; // <-- Problem here
    echo '</br>TextDomain set  : '. $data[TextDomain] .'</br>' ;
    echo '</br>===============================</br>' ;
    // End DEBUG
    }
}

When using key($plugins) it gives me always the same value (first one). When using key($data) it is giving me the FIRST LETTER only.. (??)

How can I get the this key of each nested array ?

3

3 Answers 3

1

just return $plugin, not key($plugin). $plugin should already be the key.

to elaborate, when you use the syntax:

foreach ($plugins as $plugin => $data)

it is setting $plugin to the key, and $data to it's value.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, Got it . But then why key($data) is giving me ONE (first) letter ?
@ObmerkKronen that's an interesting question, I would imagine it should error, as $plugin in this case is a string, not an array. Even if strings were treated as an array, it should return '0' not the first character of the string. I have no clue.
1

Your foreach loop indicates that the path are available as $plugin. Use that

   foreach ($plugins as $plugin => $data) {
                      // ^ This represents the key of the array item
    $plugin_data = $plugins[$plugin];

    // Start simple DEBUG
    echo '</br>===============================</br>' ;
    echo '</br><b>Plugin Name : </b>'. $data[Name]; .'</br>' ;
    echo '</br><b>Plugin Path : </b>'. $plugin .'</br>' ; // <-- Problem here
    echo '</br>TextDomain set  : '. $data[TextDomain] .'</br>' ;
    echo '</br>===============================</br>' ;
    // End DEBUG
   }

1 Comment

thanks, I got to realize from kennzpu´s answer a simple fact (that I need more sleep :-) ) .. Thanks a lot .
0

Check this modification to your code, it works now.

<?php
    $plugins = Array
    (
        'array1' => Array
            (
                'name' => 'somevalue_a',
                'TextDomain' => 'somevalue_b',
                'value_c' => 'somevalue_c',
                'value_d' => 'somevalue_d'

            ),
        'array2' => Array
            (
                'name' => 'somevalue_a',
                'TextDomain' => 'somevalue_b',
                'value_c' => 'somevalue_c',
                'value_d' => 'somevalue_d'

            )
    );

    function get_plugin_data($plugins){

        foreach ($plugins as $plugin => $data) {
        $plugin_data = $plugins[$plugin];

        // Start simple DEBUG
        echo '</br>===============================</br>' ;
        echo '</br><b>Plugin Name : </b>'. $data['name'] .'</br>' ;
        echo '</br><b>Plugin Path : </b>'. key($plugins)   .'</br>' ; // <-- Problem here
        echo '</br>TextDomain set  : '. $data['TextDomain'] .'</br>' ;
        echo '</br>===============================</br>' ;
        // End DEBUG
        }
    }
    get_plugin_data($plugins);
    //print_r($plugins);
?>

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.