PHP object array navigation - php

I'm pulling data from a Blogger RSS feed. I have most of it narrowed down how I would like it to be, except for one thing. In the following object, how would I get the string in the term section? I've tried about every syntax I can think of, but I honestly have run out of ideas.
object(SimpleXMLElement)#7 (1) {
["#attributes"]=>
array(2) {
["scheme"]=>
string(31) "http://www.blogger.com/atom/ns#"
["term"]=>
string(7) "happens"
}
}
I've tried to var_dump $item->attributesand $item->#attributes with no luck.

Use the attributes() method:
$atts = $xml->attributes();
echo $atts['term'];
Alternatively, you could also use:
$xml->attributes()->{'term'};

var_dump($object->{'#attributes'}['term']);
or
$tmp = '#attributes';
var_dump($object->{$tmp}['term']);

Related

How can I extract the value from this SimpleXml?

How can I extract the value from this SimpleXml? I keep getting an empty array, and I don't know what I'm doing wrong.
I just want to extract the string "Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT.".
object(SimpleXMLElement)#13 (2) {
["#attributes"]=>
array(2) {
["Name"]=>
string(5) "Title"
["Type"]=>
string(6) "String"
}
[0]=>
string(86) "Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT."
}
SimpleXMLElement has a __toString() method. For the element you showed in your question, you should be able to just echo the string content.
echo $yourElement;
or if you want it in a variable, you can call __toString() explicitly
$someVar = $yourElement->__toString();
or trigger it by treating the element as a string;
$someVar = "$yourElement";
Just convert it into a string.
var_dump((string) $element);
Anyway, it depends on your xpath.
Thanks for the help, everyone. Turns out I figured it out. I simply had to access it from the array.
$title = $pub->xpath('Item[#Name="Title"]')[0];

PHP Illegal String Offset Warning

Disclaimer: I'm not a PHP programmer. I don't really consider myself a programmer at all. I was just unfortunate enough to inherit some code that doesn't work.
So I have this code. I believe it was written for PHP4, but we're using PHP5.
// Retrieve project list
$project = new CProject();
$proj = $project->getAllowedRecords( $AppUI->user_id, 'project_id, project_name');
foreach ( $proj as $k => $p ) {
$project_select_list[$k] = $p['project_name'];
}
This is supposed to populate a pick list based on the user's role, but it doesn't work. I get the "illegal string offset" warning on the line inside of the foreach loop. I think understand what it's trying to do, but I don't enough about PHP to fix it. I did a vardump on $proj and it returned this.
array(5) {
[1]=> string(4) "Roof"
[2]=> string(4) "Wall"
[3]=> string(4) "Yard"
[4]=> string(7) "Kitchen"
[5]=> string(8) "Bathroom"
}
Anyone got any hints on how to fix it? Thanks.
This seems to be produced by this:
$p['project_name'];
If $p variable is a string (as the var_dump() said), you don't have an array to access nowhere. The most probably solution is this:
foreach ( $proj as $k => $p ) {
$project_select_list[$k] = $p;
}

Foreach through an array with objects and return the values and keys

array(14) {
[0]=>
object(stdClass)#2 (2) {
["set_id"]=>
int(44)
["name"]=>
string(7) "Cameras"
}
[1]=>
object(stdClass)#3 (2) {
["set_id"]=>
int(38)
["name"]=>
string(11) "Cell Phones"
}
[2]=>
object(stdClass)#4 (2) {
["set_id"]=>
int(39)
["name"]=>
string(8) "Computer"
}
The Above is my Data.
I want to return the object names ["Set_ID"] etc and the value.
I have googled and googled and tried examples from here with various failures and now I'm giving up.
I have tried to simply manually return data - ie
foreach($result as $results2)
{
foreach($results2->name as $item)
{
echo "<pre>";
print_r($item);
echo "</pre>";
}
}
I have tried all sorts of flavors of it I had kind of hoped the above would at least return data and it didn't - just errored.
In the end, I'd like to be able to both pull names and data. I don't want to have to manually find element names and code it as $result->SET_ID or whatever - I want to be able to just feed SET_ID in as a variable. I think my problem lies with it being an array of objects and I cant access that object name and all..
Im a noob, so any kind of like detailed explanation wouldn't hurt my feelings so I can learn something.
Instead of foreach($results2->name as $item) use foreach($results2 as $item). $results2->name is not an array thus causing the error.
You can read about object iteration in PHP here
foreach($result as $results2)
{
echo $results2->name.' '.$results2->set_id;
}

Get elements of json_encoded array in javascript

i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.
i print it out in the console.log() and i get this array:
array(1) {
[16]=>
array(2) {
[3488]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "999184"
["article_name_internal"]=>
string(29) "Geschenkbox Kerzenschein 2011"
}
}
[2615]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "700469"
["article_name_internal"]=>
string(29) "Hotelscheck RomantischeTagef2"
}
}
}
}
This is about right. How can i access the article_name of the second array, with the ID 2615?
found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.
EDIT:
As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.
Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.
For example you can access:
jsonObject[2615][0]["article_name_internal"]
if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.
var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]

How do I get my values out of their SimpleXMLObjects?

After an xPath, I'm left with this var_dump:
array(1) {
[0]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(11) "22-99586795"
}
}
echoing the damn thing only gives me "Array()"
How do I get the bloody string out?
Thanks
It's an array with one item, so you need to do:
$myelement[0];
or
$myelement[0][0];
(I can't tell from your question which element you're referring to)
Try casting it to string
print (string)$yourarray[0];

Categories