How to get object(stdClass) value in php? [duplicate] - php

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 6 years ago.
I have some object like this :
object(stdClass)#188 (1) { ["0"]=> string(1) "1" }
How could I print that object value in php ?
Thank you.

Try this, Best way for single value
echo $objects->{"0"};
and for multiple values
foreach($objects as $val )
{
echo $val;
}

Very short answer
var_dump($obj->{'0'});

You have access to the internal content of the Object via: ->{"0"}. Assuming you want to use the variable or echo it out for some reasons, you can simply do:
<?php echo $object->{"0"}; ?>

Related

I want dont print "" in php json [duplicate]

This question already has answers here:
PHP json_encode encoding numbers as strings
(18 answers)
Closed 2 years ago.
I get data php sql after i print from json its but i want dont print a item in ""
i want dont print id in ""
require('config.php');
$sql="SELECT * FROM file";
$result=$conn->query($sql);
foreach($result as $rows){
$recordfil=array();
$recordfil['id']=$rows['idfile'];
// $recordfil['file']=$rows['filef'];
$nnfile[]=$recordfil;
}
$filegen=$nnfile;
$arrayovp=[
'ovpn_file'=>
$filegen
];
echo json_encode($arrayovp);
?>
It output :
{"ovpn_file":[{"id":"36"},{"id":"35"}]}
I want only 36 or 35 without ""
PHP supports automatically encoding numeric strings as numbers using the JSON_NUMERIC_CHECK option, which might be easier than explicitly type-casting all relevant fields.
Try
echo json_encode($arrayovp, JSON_NUMERIC_CHECK);
# {"ovpn_file":[{"id":36},{"id":35}]}
Demo here: https://3v4l.org/ANdUM

How can I read this nested json in php? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I have this JSON
{
"AED_USD": {
"2019-08-01": 0.272242
}
}
When dumping this as an array I have:
array(1) { ["AED_USD"]=> array(1) { ["2019-08-01"]=> float(0.272242) } }
I am trying to reach get the float value of 0.272242.
How do I get this value in PHP?
You have to json_decode the string with true as second parameter to make it an array, and then just echo what you want using the name of the keys you have. Like so:
$string = '{
"AED_USD": {
"2019-08-01": 0.272242
}
}';
$decode = json_decode($string, true);
echo $decode['AED_USD']['2019-08-01'];
To get the value in PHP, you can do
$value = $yourArray['AED_USD']['2019-08-01'];
If you want more information: https://www.php.net/manual/pt_BR/language.types.array.php

Show concrete data of array with objects [duplicate]

This question already has answers here:
Fatal error: Cannot use object of type User as array
(1 answer)
PHP Error: Cannot use object of type stdClass as array (array and object issues) [duplicate]
(6 answers)
How can I access an array/object?
(6 answers)
Closed 4 years ago.
I want to show a specific data of an array, I have the following code and I am trying to print the printConcreteStudent function to print a specific student that I indicate passed through the variable $name.
When trying to find the student I get the following error:
Fatal error: Uncaught Error: Can not use object of type Student as
array
The structure of the array is as follows:
array(1) {
[0]=>
object(Student)#1 (4) {
["name"]=>
string(5) "Student1"
["lastname":"Student":private]=>
string(7) "lastName1"
}
}
And the function with which I am trying to print a specific data :
function printConcreteStudent($name) {
foreach($this->students as $key=>$value){
if($value["name"] == $name){
echo $value->getName() . " ";
echo $value->getLastName() . " ";
}
}
}
As you error state you can use object as array. In $this->students every object is of type object(Student) so you can access the name field by using "key" index. You need to change: if($value["name"] == $name){ (because cannot use $value["name"] as $value is object) to:
if($value->getName() == $name){
students is array of object so value is an object so $value->name; is how you will access the name attribute
foreach($this->students as $key=>$value){
if($name==$value->name){
//print the attributes...
}
}
It's better to lower case both of the values and then compare so that it may find the result even if capital letters are entered as input

how do i grab orderId from this array? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
How can i enter this array and grab the orderId ?
array(1) {
["data"] => array(1) {
["orderId"] => int(100)
}
}
ive tried to
print_r ["array"]["1"]["array"]["1"]["orderId"];
ive tried
foreach($array as $data)
echo ["data"]["orderId"] ;
Also note, i am receiving this json as a webhook and decoding it to php object with json_decode($json, true);
What am i missing? Should be simple but i cannot get it.
Thanks
Assuming the JSON data you're receiving is in the $array variable:
$orderId = $array["data"]["orderId"];
Explanation:
First you grab the "data" property from the array
That data property contains another array which has its own properties.
From that array, you can grab the "orderId" property.
I hope this helps :)
If your array is named $that, use $that['data']['orderId'];
Try this $array["data"]["orderId"] `

Echo specific item from array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I want to echo value from this array, so it'll say "true"
::object(stdClass)#25 (2) { ["Name"]=> string(3) "DPV" ["Value"]=> string(4) "true" }
How can I do this in PHP?
assuming the object property of Value is public ( which it is or it wouldn't be output ) you can use PHP's object operator ->
Such as:
echo $obj->Value;
In the case that it isn't public, you would want to use a get menthod, such as this
class obj{
protected $Value = true;
public function getValue(){
return $this->Value;
}
}
$obj = new obj();
echo $obj->getValue();
AS you can see both access the objects property the same basic way ( using the -> ). Which is different then how you access elements in an array. You can implement ArrayAccess on an object, which allows you to use it as if it was an array, but that's a post for another time.
http://php.net/manual/en/class.arrayaccess.php
Simply
<?php
echo $objectVarName->Value;
Note: $objectVarName is not an array, it is an object.
A very common example of arrays in PHP as following:
$array = array(
"foo" => "bar",
"bar" => "foo",
);
Here is simple solution, might be helpful.
<?php
//Let suppose you have this array
$arr = array('id'=>1,'name'=>'alax','status'=>'true');
//Check out array (Basically return array with key and value).
print_r($arr);
// Get value from the particular index
echo "<br/>Your value is:".$arr['status'];
?>

Categories