2

How can i print array values in magento 2? I am using below code to print log in magento 2 but it will only print string values.

\Magento\Framework\App\ObjectManager::getInstance()
    ->get('Psr\Log\LoggerInterface')->debug('message');

Thanks for your reply in advance.

0

2 Answers 2

5

You can try:

OLD WAY

   \Magento\Framework\App\ObjectManager::getInstance()
        ->get('Psr\Log\LoggerInterface')->debug(print_r(['a' => 'b'], true));

NEW WAY

STEP 1:  Add object in construction()
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger;
        parent::__construct($context);
   }
STEP :2 Use logger in function where you want 
            $this->_logger->addDebug("=======START ORDER======"); 
            $this->_logger->addDebug(json_encode($order->getData()));
            $this->_logger->addDebug("=======END ORDER======"); 
0

If you are passing an array to debug you can use:

debug(print_r($arrayValues, true));

Also if you are using IDE like phpstorm you can see if there is a toArray method available.

2
  • print_r by default doesn't return value. You have to pass second parameter true Commented Jul 15, 2016 at 7:22
  • you are correct, will edit
    – belfort1
    Commented Jul 15, 2016 at 8:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.