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
Related
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
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"] `
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
How can I iterate to get id value? This is my array:
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
tried
<?php
foreach($faculty as $value)
{
echo $value['Id'];
}
?>
Gives an error
Use of undefined constant Id - assumed Id
This is a json which is basically a string, to be more precise the given json contains a list (currently 1 element):
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
You need to convert it to an array first:
$jsonValues = json_decode($json, true); //here you will have an array of users (1 now)
foreach($jsonValues as $faculty) //for each user do something
{
echo $faculty['Id'];
}
This is JSON format. First you have to decode it. Example:
$a = '[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]';
$dec = json_decode($a);
echo $dec[0]->Id;
Result: 216
Decoded you have an array, containing exactly one object. You have to access the object properties with -> then.
With JSON [] brackets means an array, while {} brackets mean objects. Learn more: https://en.wikipedia.org/wiki/JSON
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'];
?>
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"}; ?>