Convert multidimensional array into single array

后端 未结 20 1808
时光取名叫无心
时光取名叫无心 2020-11-22 10:39

I have an array which is multidimensional for no reason

/* This is how my array is currently */
Array
(
[0] => Array
    (
        [0] => Array
                


        
20条回答
  •  一整个雨季
    2020-11-22 11:27

    I had come across the same requirement to flatter multidimensional array into single dimensional array than search value using text in key. here is my code

    $data = '{
        "json_data": [{
                "downtime": true,
                "pfix": {
                    "max": 100,
                    "threshold": 880
                },
                "ints": {
                    "int": [{
                        "rle": "pri",
                        "device": "laptop",
                        "int": "Ether3",
                        "ip": "127.0.0.3"
                    }],
                    "eth": {
                        "lan": 57
                    }
                }
            },
            {
                "downtime": false,
                "lsi": "987654",
                "pfix": {
                    "min": 10000,
                    "threshold": 890
                },
                "mana": {
                    "mode": "NONE"
                },
                "ints": {
                    "int": [{
                        "rle": "sre",
                        "device": "desk",
                        "int": "Ten",
                        "ip": "1.1.1.1",
                        "UF": true
                    }],
                    "ethernet": {
                        "lan": 2
                    }
                }
            }
        ]
    }
    ';
    
    $data = json_decode($data,true);
    
    
    $stack = &$data;
    $separator = '.';
    $toc = array();
    
    while ($stack) {
        list($key, $value) = each($stack);
        unset($stack[$key]);
        if (is_array($value)) {
            $build = array($key => ''); # numbering without a title.
            foreach ($value as $subKey => $node)
                $build[$key . $separator . $subKey] = $node;
            $stack = $build + $stack;
            continue;
        }
        if(!is_numeric($key)){
            $toc[$key] = $value;
        }
    }
    
    
    echo '
    ';
    print_r($toc);
    
    
    

    My output:

    Array
    (
        [json_data] => 
        [json_data.0] => 
        [json_data.0.downtime] => 1
        [json_data.0.pfix] => 
        [json_data.0.pfix.max] => 100
        [json_data.0.pfix.threshold] => 880
        [json_data.0.ints] => 
        [json_data.0.ints.int] => 
        [json_data.0.ints.int.0] => 
        [json_data.0.ints.int.0.rle] => pri
        [json_data.0.ints.int.0.device] => laptop
        [json_data.0.ints.int.0.int] => Ether3
        [json_data.0.ints.int.0.ip] => 127.0.0.3
        [json_data.0.ints.eth] => 
        [json_data.0.ints.eth.lan] => 57
        [json_data.1] => 
        [json_data.1.downtime] => 
        [json_data.1.lsi] => 987654
        [json_data.1.pfix] => 
        [json_data.1.pfix.min] => 10000
        [json_data.1.pfix.threshold] => 890
        [json_data.1.mana] => 
        [json_data.1.mana.mode] => NONE
        [json_data.1.ints] => 
        [json_data.1.ints.int] => 
        [json_data.1.ints.int.0] => 
        [json_data.1.ints.int.0.rle] => sre
        [json_data.1.ints.int.0.device] => desk
        [json_data.1.ints.int.0.int] => Ten
        [json_data.1.ints.int.0.ip] => 1.1.1.1
        [json_data.1.ints.int.0.UF] => 1
        [json_data.1.ints.ethernet] => 
        [json_data.1.ints.ethernet.lan] => 2
    )
    

提交回复
热议问题