Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following array from PHP:

Array
(
    [0] => Array
        (
            [ContractExhibitsData] => Array
                (
                    [id] => 2
                    [exhibit_id] => 2
                    [parent_id] => 
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [ContractExhibitsData] => Array
                                (
                                    [id] => 98
                                    [exhibit_id] => 2
                                    [parent_id] => 2
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [ContractExhibitsData] => Array
                                                (
                                                    [id] => 99
                                                    [exhibit_id] => 2
                                                    [parent_id] => 98
                                                )

                                            [children] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [ContractExhibitsData] => Array
                                                (
                                                    [id] => 100
                                                    [exhibit_id] => 2
                                                    [parent_id] => 98
                                                )

                                            [children] => Array
                                                (
                                                )

                                        )
                                    )
                                )
                            )
                        )
                    );

It is essentially a tree array, consisting of nodes with child nodes with more data. I want to iterate over this array using a recursive function to generate an array like this:

$return = Array
(
    [2] => '1.',
    [98] => '1.1.',
    [99] => '1.1.1.',
    [100] => '1.1.2.'
);

And so forth. Basically, it will be a bunch of Key-Value pairs where the array key is the 'id' of the element in ContractExhibitsData and the value is the numerical index.

I have the following function that I have been tinkering with, but it isn't getting me quite where I need to be.

private function getIndexes($data, $prefix = null) {

    $indexes = array();
    $count   = 0;

    if ($prefix) {
        $prefix = $prefix . '.';
    }

    if (!empty($data['children'])) {
        foreach($data['children'] as $child) {
            $count++;

            $indexes[$child['ContractExhibitsData']['id']] = $prefix.$count;

            if (is_array($child['children']) && !empty($child['children'])) {
                $subIndex = $this->getIndexes($child, $prefix.$count);
                return $subIndex;
            }
        }
    }

    return $indexes;

}
share|improve this question
    
not answering your question but maybe interesting for you and something you might switch to: en.wikipedia.org/wiki/Nested_set_model –  Andresch Serj Apr 3 at 6:58
1  
How is that supposed to help me? I cannot change the way I am receiving this data, nor the way it is stored. –  Barry Chapman Apr 3 at 6:59
    
As is said, not answering your question, just something that you might be able and want to switch to. Also: What is the outcome of your function? what is the problem with your function? Consider providing a plunkr/ideone or something like that. ideone.com –  Andresch Serj Apr 3 at 7:07
    
you return a subindex in the foreach loop thus, breaking your foreach loop and therefore never ever looping over your full array. –  Andresch Serj Apr 3 at 7:18

1 Answer 1

up vote 0 down vote accepted

Got it! See the working version here.

<?php

function getIndexes($data, $prefix = '') {

    $indexes = array();
    $count   = 1;

    if (!empty($prefix)) {
        $prefix = $prefix . '.';
    }

    foreach ($data as $dataItem) {
        $item   = $dataItem['ContractExhibitsData'];
        $index  = $prefix.$count;

        $indexes[$item['id']] = $index;

        if(!empty($dataItem['children'])) {
            $indexes = array_merge_recursive($indexes,getIndexes($dataItem['children'],$index));
        }
        $count++;
    }

    return $indexes;

}

$yourArray = array
(
    0 => array (
        'ContractExhibitsData' => array
        (
            'id' => 2,
            'exhibit_id' => 2,
            'parent_id' => null
        ),
        'children' => array
        (
            0 => array
            (
                'ContractExhibitsData' => array
                (
                    'id' => 98,
                    'exhibit_id' => 2,
                    'parent_id' => 2
                ),
                'children' => array
                (
                    0 => array
                    (
                        'ContractExhibitsData' => array
                        (
                            'id' => 99,
                            'exhibit_id' => 2,
                            'parent_id' => 98
                        ),
                        'children' => array()

                    ),
                    1 => array
                    (
                        'ContractExhibitsData' => array
                        (
                            'id' => 100,
                            'exhibit_id' => 2,
                            'parent_id' => 98
                        ),
                        'children' => array ()
                    )
                )
            )
        )
    )
);

$result = getIndexes($yourArray);
var_dump($result);

One of your problems was that you had a return in your loop, thus breaking your loop. Also, you added null as a string since that was the default setting of your $prefix. You alos never iterated over the initial array but always just the children.

share|improve this answer
1  
Cheers :) Works perfect –  Barry Chapman Apr 3 at 7:30
    
@BarryChapman you're welcome :D –  Andresch Serj Apr 3 at 7:32

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.