0

I want to convert multidimensional array to single dimension array with combine keys, maybe my question is not understandable, so that i explain with example:

I have array like following :

// JSON
{"a":{"a":{"a":1},"b":{"a":1},"c":{"a":1},"d":{"a":1},"e":{"a":1,"b":1,"c":1,"d":1},"f":{"a":1}}}

Array
(
    [a] => Array
        (
            [a] => Array
                (
                    [a] => 1
                )

            [b] => Array
                (
                    [a] => 1
                )

            [c] => Array
                (
                    [a] => 1
                )

            [d] => Array
                (
                    [a] => 1
                )

            [e] => Array
                (
                    [a] => 1
                    [b] => 1
                    [c] => 1
                    [d] => 1
                )

            [d] => Array
                (
                    [a] => 1
                )

        )

)

Outout that i want

Array
(
    [0] => '[a][a][a]'
    [1] => '[a][b][a]'
    [2] => '[a][c][a]'
    [3] => '[a][d][a]'
    [4] => '[a][e][a]'
    [5] => '[a][e][b]'
    [6] => '[a][e][c]'
    [7] => '[a][e][d]'
    [8] => '[a][f][a]'
)

Hope that understandable
I try many ways but not success, please help

How to Flatten a Multidimensional Array?, this answer is not the solution of my question, please compare

Jagjeet Singh
  • 1,564
  • 1
  • 10
  • 23

3 Answers3

0

You may try my custom code to get array as you want

<?php
$json = '{"a":{"a":{"a":1},"b":{"a":1},"c":{"a":1},"d":{"a":1},"e":{"a":1,"b":1,"c":1,"d":1},"f":{"a":1}}}';
$array = json_decode($json, true);
$returnarray = [];
$i = 0;
$main = array_keys($array);
foreach($array['a'] as $key => $val) {
    $arr = [];
    array_push($arr, $main[0]);
    $keysarr = array_keys($val);
    array_push($arr, $key);
    if(count($keysarr) > 1) {
        for($j = 0; $j < count($keysarr); $j++) {
            array_push($arr, $keysarr[$j]);
            $returnarray[$i] = $arr;
            array_pop($arr);
            $i++;
        }
    } else {
        array_push($arr, $keysarr[0]);
        $returnarray[$i] = $arr;
        $i++;
    }
}
echo "<pre>";
print_r($returnarray);
?>
Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
0

Not an efficient one but do the job if your array is exactly like that the way you have shown in the example.

<?php

$dataSource = array
(
    'a' => array
    (
        'a' => array
        (
            'a' => 1
        ),

        'b' => array
        (
            'a' => 1
        ),

        'c' => array
        (
            'a' => 1
        ),

        'd' => array
        (
            'a' => 2
        ),

        'e' => array
        (
            'a' => 1,
            'b' => 1,
            'c' => 1,
            'd' => 1
        ),

        'f' => array
        (
            'a' => 1
        )
    )
) ;

echo "<pre>";
print_r($dataSource);
echo "</pre>";

$resultData = [];
foreach($dataSource as $key => $val)
{
    foreach($val as $key1 => $val1)
    {     
        foreach($val1 as $key2 => $val2)   
        {
            $resultData[] = '['.$key.']['.$key1.']['.$key2.']';
        }

    }
}

echo "<pre>";
print_r($resultData);
echo "</pre>";
Suresh
  • 5,687
  • 12
  • 51
  • 80
0

This solve my issue, its tricky but work for my case:

$json = '{"a":{"a":{"a":1},"b":{"a":1},"c":{"a":1},"d":{"a":1},"e":{"a":1,"b":1,"c":1,"d":{"a":1}},"f":{"a":1}},"b":{"a":{"a":1}}}';
$data = json_decode($json, true);

function array_str($data = array()) {
 $result = [];
 $data = explode('&', urldecode(http_build_query($data)));
 foreach ($data as $value) {
  $main = explode('[', $value)[0];
  $result[] = preg_replace('/' . $main . '/', '[' . $main . ']', explode('=', $value)[0], 1);  
 }
 return $result;
}
Jagjeet Singh
  • 1,564
  • 1
  • 10
  • 23