array to get object's value failed in php [duplicate] - php

This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 8 years ago.
why is this wrong?
[enclosure] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345&
[length] =>
[type] => image/jpeg
)
)
I want to get the url to get the image file
I wrote print_r($eachItem->enclosure['#attributes']->url) it doesn't work. Why?

That is not the correct way of getting the attribute value. Use ->attributes() method:
echo (string) $eachItem->enclosure->attributes()['url'];
// as of PHP 5.4 (dereferencing)
Or
// PHP 5.3 below
$eachItem_attribute = $eachItem->enclosure->attributes();
echo (string) $eachItem_attribute['url'];

Right & Quick Format
$eachItem->enclosure->attributes()->{'url'};

Related

PHP Pull data out of an array [duplicate]

This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];

Parsing Json From Web Service With PHP [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I'm new with json and web service. After using insert command, I do print_r($result) and got this data:
Array ( [error_code] => 0 [error_desc] => [result] => Array ( [error_code] => 999 [error_desc] => Format input 'date' is wrong ) )
I want to print the error_desc only which is show Format input 'date' is wrong.
I try to parsing it with PHP but I only got empty result. Here is my code:
if (is_array($result)) {
if ($result['error_code'] =='999') {
echo $result['error_desc'];
}
}
Please help. Thanks.
I already got the answer. I just know after print the error_code. Thanks.

Get output from XML array [duplicate]

This question already has answers here:
PHP get values from SimpleXMLElement array
(5 answers)
Closed 9 years ago.
I'm using XML for the use of different languages. First of all, I'm not used to work with XML so I want to ask if I'm doing it right. Here is my XML code:
<lang>
<one>
<ENG>Text1</ENG>
<NL>Text2</NL>
</one>
</lang>
When I load this in php I get this array:
SimpleXMLElement Object ( [one] => SimpleXMLElement Object ( [ENG] => Text1 [NL] => Text2 ) )
I'm now trying to get each single element out of the XML, I have this code:
$xml = simplexml_load_file($file);
$result = $xml['one']['ENG'];
But it doesn't return any result, please help.
Thanks.
Use object accessor syntax instead:
$result = $xml->one->ENG;

Extract values from php array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I am using SOAP to get data from the server and in response i am getting a php array like this
Array
(
[BookResult] => stdClass Object
(
[PNR] => 5WPODU
[BookingId] => 31149
[Status] => stdClass Object
(
[StatusCode] => 03
[Description] => Fare is not available at the time of booking
[Category] => BK
)
[SSRDenied] => N
[ProdType] => Flight
)
)
All i want to know is how can i extract "PNR" and "StatusCode" value in separate variables so that i can store them in database.
Tried this not working
$p = (object) $array;
echo $p->StatusCode;
Try this:
$PNR = $array["BookResult"]->PNR;
$StatusCode= $array["BookResult"]->Status->StatusCode;
$array is an array. So first dive is $array['BookResult'].
BookResult is stdClass instance so next goes $array['BookResult']->Status (get object's property).
Status is also stdClass instance so get it's property: $array['BookResult']->Status->StatusCode
var_dump($array['BookResult']->PNR);
var_dump($array['BookResult']->Status->StatusCode);
Assuming results are being stored in $array
echo $array['BookResult']->Status->StatusCode;
echo $array['BookResult']->PNR;

PHP: Access the value of an object where the key contains a '$' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I access a PHP object attribute having a dollar sign?
How do I access the value of an object where the key contains a '$'. I have an array named "entries" as follows:
Array
(
[0] => stdClass Object
(
[id] => stdClass Object
(
[$t] => xyz
)
)
)
I want to access the value of [$t], ie. xyz. The following command returns null:
echo $entries[0]->id->$t;
What syntax should I use?
Use the {expression} syntax:
$entries[0]->id->{'$t'}
In case you are curious: echo $entries[0]->id->$t; would work if you did $t = '$t'; first.

Categories