我怎样才能看到这个数组的结构?

时间:2014-03-31 14:12:28

标签: php echo

如何查看$response中存储的数组结构?我试过print_r()但我在chrome开发人员中看不到任何东西(得到了php控制台)。

 function myajax_inputtitleSubmit_func() {

     global $wpdb;
$mp_country_table_info = $wpdb->prefix . 'countries '; //Good practice
$mpcountrys = $wpdb->get_var( "SELECT labour_cost FROM $mp_country_table_info WHERE id='2' ");


        $nonce = $_POST['nextNonce'];   
    if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) )
        die ( 'Busted!');

    $numwelds = isset($_POST['numberofwelds']) ? $_POST['numberofwelds'] : '';
$numconwelds = isset($_POST['numberofconwelds']) ? $_POST['numberofconwelds'] : '';
$fortot2 = 5;
$fortot3 = 2;

if (is_numeric($numwelds) && is_numeric($numconwelds))
{
    $total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
    $total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
    $total['tot3'] = ($numwelds + $numconwelds) + $fortot2 / $fortot3; 
    $response = json_encode($total);
    print_r($total); // added line
    header("Content-Type: application/json");  
    echo $response;
    exit;
} 

更新我已尝试过以下内容&它不起作用:

   $total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
    $total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
    $total['tot3'] = ($numwelds + $numconwelds) + $fortot2 / $fortot3; 
    $response = json_encode($total);
    header("Content-Type: application/json");  
    echo $response;
    exit;
    //added bit
    echo '<pre>';
    echo json_encode($response, JSON_PRETTY_PRINT);
echo '</pre>';

UPDATE TWO这似乎不起作用:

       $total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
    $total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
    $total['tot3'] = ($numwelds + $numconwelds) + $fortot2 / $fortot3; 
    echo '<pre>';
    echo json_encode($response, JSON_PRETTY_PRINT);
echo '</pre>';
    header("Content-Type: application/json");  
    echo $response;
    exit;

这是我第一次尝试使用chrome php控制台。我已经加了'它。通过查看这个网站http://php-console.com/instance/examples/#debug_vars,我也确保我的工作正常。

更新三 - 全部功能

 function myajax_inputtitleSubmit_func() {

 global $wpdb;
$mp_country_table_info = $wpdb->prefix . 'countries '; //Good practice
$mpcountrys = $wpdb->get_var( "SELECT labour_cost FROM $mp_country_table_info WHERE id='2' ");


        $nonce = $_POST['nextNonce'];   
    if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) )
        die ( 'Busted!');

    $numwelds = isset($_POST['numberofwelds']) ? $_POST['numberofwelds'] : '';
$numconwelds = isset($_POST['numberofconwelds']) ? $_POST['numberofconwelds'] : '';
$fortot2 = 5;
$fortot3 = 2;

if (is_numeric($numwelds) && is_numeric($numconwelds))
{
    $total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
    $total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
    $total['tot3'] = ($numwelds + $numconwelds) + $fortot2 / $fortot3; 
    echo '<pre>';
    echo json_encode($total, JSON_PRETTY_PRINT);
echo '</pre>';
    header("Content-Type: application/json");  
    echo $response;
   // exit;
} 
}

更新4 - 尝试var_dump - 也无法正常工作

$total['tot1'] = $numwelds + $numconwelds + $mpcountrys ;
$total['tot2'] = $numwelds + $numconwelds + $fortot2 ;
$total['tot3'] = ($numwelds + $numconwelds) + $fortot2 / $fortot3; 
$response = json_encode($total);
header("Content-Type: application/json");  
echo $response;
exit;
echo  '<pre>';
var_dump($response);
echo '</pre>';

3 个答案:

答案 0 :(得分:2)

尝试这样从PHP打印出JSON

echo '<pre>';
    echo json_encode($response, JSON_PRETTY_PRINT);
echo '</pre>';

修改

也许你的代码中有某个前面的回声

删除所有print / echo / var_dump并尝试以下代码:

ob_end_clean();

header('Content-Type: application/json');
$json = json_encode($total);        
echo $json;

答案 1 :(得分:1)

使用var_dump($response),当值为nullfalse之类的空值时,它会提供更好的输出。

答案 2 :(得分:1)

像这样。

echo '<pre>';
var_dump($response);
echo '</pre>';