1

I have this Array:

$input_array = array(
    'one' => array(
            'two' => '3',
            'four' => array(5,6,7)
        ),
    'eight' => array(
            'nine' =>  array(
                    'ten' => '11'
                )
        )

);

I want the output to be:

one/two:3 
one/four/0:5 
one/four/1:6 
one/four/2:7 
eight/nine/ten:11

I have managed to make this function/code:

function flatten($array) {
    if (is_array($array)) {
        //get the key and value pair of the array
        foreach ($array as $key => $value) {

            if (is_array($value)) {
                echo $key.'/';
            } else {
                echo $key.":".$value."<br/>\n";
            }

            flatten($value);
        }

    } else {
        return false;
    }


}

flatten($input_array);

Currently it outputs:

one/two:3
four/0:5
1:6
2:7
eight/nine/ten:11

The 1st and last line are correct, but in the middle it's not the output I wanted, I know I am close, just need more modification. Anyone can help? Thanks!

2 Answers 2

3

save path until a leaf item and then echo:

function flatten($array, $prefix='') {
    if (is_array($array)) {
        //get the key and value pair of the array
        foreach ($array as $key => $value) {

            if (is_array($value)) {
                // call with `path to this array` 
                flatten($value, $prefix . $key.'/');
            } else {
                echo $prefix.$key.":".$value."<br/>\n";
            }
        }
    } else {
        return false;
    }
}

demo on eval.in

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

2 Comments

I knew I was close, thanks for this! Didn't think of adding another variable so I can store the old key, I was actually doing it right now but not adding the prefix to the function but inside the loop, and it was outputing wrong! Tagged this as the solution!
You can't echo without collecting because you get a key one time a loop but need to echo it as many times as descendant leaf items present. Glad yo help. Good luck!
0

You can implement a simple recursive strategy:

<?php
$input = [
    'one' => [
        'two' => '3',
        'four' => [5, 6, 7 ]
    ],
    'eight' => [
        'nine' => [
            'ten' => '11'
        ]
    ]
];

$output = [];
$path = [];

function createPath($entry, $path, &$output) {
    if (is_array($entry)) {
        foreach ($entry as $key => $value) {
            createPath($value, array_merge($path, [$key]), $output);
        }
    } else {
        $output[] = implode('/', $path) . ':' . $entry;
    }
};

createPath($input, $path, $output);

print_r($output);

The output of above code obviously is:

Array
(
    [0] => one/two:3
    [1] => one/four/0:5
    [2] => one/four/1:6
    [3] => one/four/2:7
    [4] => eight/nine/ten:11
)

3 Comments

What if I wanted to reverse the array output, and bring it back to it's original multi-dimensional state?
I'd suggest you simply keep the original array structure somewhere ;-)
Lol! just a scenario though, what if I was given a flat array and then need to make it into a multi-dimensional array. I actually posted a new question but it was tagged as duplicate. stackoverflow.com/questions/43844723/…

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.