【问题标题】:I want to console.log value of var_dump of array. How do I do that?我想 console.log 数组的 var_dump 值。我怎么做?
【发布时间】:2020-05-27 19:33:36
【问题描述】:

如果我这样写,它会返回一个 json 对象。

public function getElementsAction()
{
    $currency = App::$app->getProperty('currency');
    if(!empty($_POST))
    {
        $sql = 'SELECT name, price, file, time FROM listings WHERE id_item IN (' . implode(',', $_POST) . ')';
        $items = \R::getAll($sql);
        echo(json_encode($items));
    }
}

但我想要的是获取一个 var_dump 字符串并在 console.log 中打印出来以进行进一步调试,那我该怎么办?我尝试了两种方法,它在 console.log 中给了我错误:((

public function getElementsAction()
{
    $currency = App::$app->getProperty('currency');
    if(!empty($_POST))
    {
        $sql = 'SELECT name, price, file, time FROM listings WHERE id_item IN (' . implode(',', $_POST) . ')';
        $items = \R::getAll($sql);
        //echo(json_encode(var_dump($items))); // error
        //echo(json_encode(strval(var_dump($items)))); // error
        //echo(json_encode("Hi"); // No error
    }
}

或者它是我可以在网络的某个地方看到的东西?谢谢!

【问题讨论】:

    标签: javascript php json fetch jsonencoder


    【解决方案1】:

    如果你想在 PHP 中使用 console.log(),你可以这样做:

    function console($value) {
        $log = '<script>console.log(' . json_encode($value, JSON_HEX_TAG) . 
        ');' . '</script>';
        echo $log;
    }
    

    编辑:试试here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 2023-01-05
      相关资源
      最近更新 更多