Walk array recursively and print the path of the walk

前端 未结 9 1512
礼貌的吻别
礼貌的吻别 2020-12-03 08:03

Can someone help me with some code or instructions on how to walk recursively an array and when reaching the last element print the full path to it? A simple echo will work

9条回答
  •  青春惊慌失措
    2020-12-03 08:32

    I found this solution, which also keeps into account if elements of the structure are arrays:

    $file_contents=file_get_contents("data.json");
    $json_dump=json_decode($file_contents); 
    printPath($json_dump, '', "" ,"");
    
    function printPath($the_array, $path, $prevType) {
    // Parse all elements of a structure 
    // and print full PHP path to each one.
        foreach($the_array as $key => $value)  {
            if(is_array($value)) {
                // Array element cannot be directly printed: process its items as     objects:
                printPath($value, $path  . $key , "array");
            } else {            
                if (!is_object($value))  { // If the element is not an object, it can be printed (it's a leaf)
                    if(is_string($value)) { 
                        $finalValue = '"' . $value . '"'; 
                    } else { 
                        $finalValue = $value;
                    }
                    if($prevType == "array") {
                        // If array element, add index in square brackets
                        echo($path  . "["  . $key . "] =  " .  $finalValue . "
    "); } else { echo($path . $key . " = " . $finalValue . "
    "); } } else { // else store partial path and iterate: if($prevType == "array") { // Path of array element must contain element index: printPath($value, $path . "[" . $key . "]->" , "dummy"); } else { printPath($value, $path . $key . "->", "dummy"); } } } } }

    Example output:

    status->connections->DSS-25->band = "X"
    status->connections->DSS-25->endAt = "2019-11-20T20:40:00.000Z"
    status->connections->DSS-25->startAt = "2019-11-20T12:40:00.000Z"
    geometry[0]->obs[0]->name = "UDSC64"
    geometry[0]->obs[0]->hayabusa2->azm = 90.34
    geometry[0]->obs[0]->hayabusa2->alt = -20.51
    

    In case anybody is interested, here it is the port to Javascript:

    function iterate(obj, stack, prevType) {
        for (var property in obj) {
            if ( Array.isArray(obj[property]) ) {
                //console.log(property , "(L="  + obj[property].length + ") is an array  with parent ", prevType, stack);
                iterate(obj[property], stack  + property , "array");
            } else {
                if ((typeof obj[property] != "string")  && (typeof obj[property] != "number"))  {
                    if(prevType == "array") {
                        //console.log(stack + "["  + property + "] is an object, item of " , prevType, stack);
                        iterate(obj[property], stack + "["  +property + "]." , "object");
                    } else {
                        //console.log(stack +    property  , "is " , typeof obj[property] , " with parent ", prevType, stack );
                        iterate(obj[property], stack  + property + ".", "object");
                    }   
                } else {
                    if(prevType == "array") {
                        console.log(stack + "["  + property + "] =  "+  obj[property]);
    
                    } else {
                        console.log(stack +    property  , " =  " ,  obj[property] );                       
                    }   
                }
            }
        }
    }
    
    iterate(object, '', "File")
    

提交回复
热议问题