Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im trying to split navigation position strings into a master array.

For example, If I have an item that's position is 1.2.2

I would like to add it in the master array as follows

 1 =>
    2 =>
        2 => array()

And then if another item has '2.1'

 1 =>
    2 =>
        2 => array()
 2 =>
    1 => array()

and then another '1.2.3'

 1 =>
    2 =>
        2 => array()
        3 => array()
 2 =>
    1 => array()

does anyone know of a way for doing this?

regards

edit

lets say I have a one dimensional array of objectects, I want to loop through them and store as a structured "navigation" like nested array. Each item has a navigation position string, i.e. 1.2.3.6

I then was thinking of $depth = explode( '.', $details['navigation_pos'] ); running it through some kind of array walker to place the object in the correct position.

hope this helps

edit

maybe a better way to put it is this, but more elegantly:

$depth = explode( '.', '1.2.3.4' );
$bar = json_decode( '{"' . implode( '":{"', $depth ) . '":[]' . str_repeat( '}', sizeof( $depth ) ) );
print_r($bar);

which would give

stdClass Object
(
    [1] => stdClass Object
        (
            [2] => stdClass Object
                (
                    [3] => stdClass Object
                        (
                            [4] => Array
                                (
                                )

                        )

                )

        )

)
share|improve this question
What is your input is it a string or a array and what should be the output ? – Prasanth Bendra Feb 19 at 7:02
where your array will be stored? in session? – Bhavik Shah Feb 19 at 7:05
@Luke Snowden, display your exact string – Awais Qarni Feb 19 at 7:08
not quite sure what your asking... the position string, is a string i.e. '1.2.3' and I want to store a reference of the associated item at that point so I can loop through them and display a nested navigation – Luke Snowden Feb 19 at 7:08

1 Answer

You could use the eval() construct, but beware:

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

$final_array = array(); // The output array

/*
    Example:
    $big_array = array(
        '1.1' => 'One-one',
        '2.1.3.4' => 'Two-one-three-four'
    );
*/  

foreach ($big_array as $position_string => $item)
{
    $index_array = explode(".", $position_string);

    foreach ($index_array as $key => $value)
    {       
        // Make sure only integers are put through eval()
        $index_array[$key] = (int)$value;
    }

    $indexes = implode("][", $index_array);

    // TODO: make sure $item is safe to put through eval()!
    eval("\$final_array[{$indexes}] = \$item");
}
share|improve this answer
hmmm, gonna test the above. Managed to get it to work with the following $depth = explode( '.', $details['navigation_pos'] ); $bar = json_decode( '{"' . implode( '":{"', $depth ) . '":{"name" : "' . $details['name'] . '","name" : "' . $details['name'] . '"}' . str_repeat( '}', sizeof( $depth ) ) ); $this->navigation = array_merge_recursive( $this->navigation, (array)$bar ); as well – Luke Snowden Feb 19 at 8:01
You could also try to adapt the accepted answer to this question: stackoverflow.com/questions/2286706/… (so that it sets the value rather than getting it) – Alasjo Feb 19 at 8:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.