-1

I am using php and decode json format to array as following code

$sub_cats_ids=array();
$sub_cats_ids=json_decode($_POST['sub_cats']);

I want to echo first item in array to test if it works fine as following code

echo current($sub_cats_ids);

but I get this error message

Object of class stdClass could not be converted to string

I tried this code also

echo $sub_cats_ids[0];

but I get the same error message how I can solve this issue

Mokhtar Ghaleb
  • 423
  • 13
  • 26

1 Answers1

0

The error message is crystal clear, isn't it? The current element in the array is an object, not a string. You cannot "echo" an object, but only a string. So php tries to convert the object into a string but fails, since no such conversion is defined for an object of the generic standard class.

You need to use a function to output an object instead of the echo command, or you need to to convert that object into a string which you can output:

<?php
//...
var_export(current($sub_cats_ids));

Or to echo the object:

<?php
//...
echo var_export(current($sub_cats_ids), true);

UPDATE:

Your comment below suggests that unlike what you wrote in the question you do not want to output the object itself, but only a specific property of that object. That means you need to access that property in the object, php cannot somehow magically guess that you want to do that.

Without you posting additional information all I can do here is guess what you actually need to do:

<?php
//...
$object = current($sub_cats_ids); 
echo $object->IT;
arkascha
  • 41,620
  • 7
  • 58
  • 90