Print $_POST variable name along with value

前端 未结 4 665
既然无缘
既然无缘 2020-11-29 05:51

I have a POST in PHP for which I won\'t always know the names of the variable fields I will be processing.

I have a function that will loop through the values (howev

4条回答
  •  生来不讨喜
    2020-11-29 06:43

    If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:

    print_r($_POST);
    

    To recursively print the contents of an array:

    printArray($_POST);
    
    function printArray($array){
         foreach ($array as $key => $value){
            echo "$key => $value";
            if(is_array($value)){ //If $value is an array, print it as well!
                printArray($value);
            }  
        } 
    }
    

    Apply some padding to nested arrays:

    printArray($_POST);
    
    /*
     * $pad='' gives $pad a default value, meaning we don't have 
     * to pass printArray a value for it if we don't want to if we're
     * happy with the given default value (no padding)
     */
    function printArray($array, $pad=''){
         foreach ($array as $key => $value){
            echo $pad . "$key => $value";
            if(is_array($value)){
                printArray($value, $pad.' ');
            }  
        } 
    }
    

    is_array returns true if the given variable is an array.

    You can also use array_keys which will return all the string names.

提交回复
热议问题