简体   繁体   中英

I want to console.log value of var_dump of array. How do I do that?

It works fine it returns me an json object after all if I write like this.

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));
    }
}

But what I want is get a string of var_dump and print out in console.log for further debuggin, what do I do then? I tried both ways and it gives me error in 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
    }
}

or is it something I can see in Network somewhere? thank you!

If you want to console.log() in PHP you can do this:

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

EDIT: Try it here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM