YouTube Feed has an object called #attributes how can I access it? - php

I am trying to get a YouTube RSS feed to work but I am struggling to get one of the attributes I need out of it. I have never seen part of the array starting with an # sign so I think it may be some sort of a special element but I'm not sure. Code below and what I have already tried after.
Feed:
<?php
$xml->entry =
SimpleXMLElement::__set_state(array(
'id' => 'yt:video:DjwM9SHJznM',
'title' => 'JD19AT - Joomla! in der Uni - Community-Arbeit als Lehrveranstaltung',
'link' =>
SimpleXMLElement::__set_state(array(
'#attributes' =>
array (
'rel' => 'alternate',
'href' => 'https://www.youtube.com/watch?v=DjwM9SHJznM',
),
)),
'author' =>
SimpleXMLElement::__set_state(array(
'name' => 'J and Beyond e.V.',
'uri' => 'https://www.youtube.com/channel/UCy6ThiEDnalZOd_pgtpBk1Q',
)),
'published' => '2019-03-30T16:49:53+00:00',
'updated' => '2019-05-09T16:56:18+00:00',
));
?>
Code:
$feed = $youtubeChannelFeed;
$xml = simplexml_load_file($feed);
$html = "";
This works $xml->entry->title;
but this doesn't $xml->entry->link it just says "SimpleXML Object"
As it says object I then tried using both -> arrow and ['attribute'] notation.
I tried escaping the # with a \# but that just caused an error.
How can I traverse the tree and get the value of to #attributes->href ?

The way I always try to remember this is that you can use an arrow or brackets to access data in an array or object.
Array begins with A, but it chooses the one that's does not begin with A. Object is the one left over. That's how I remember it at least.
In this case, although it was calling it a SimpleXMLObject it was actually showing me that it's an array in the print_r. So I had to use brackets to access like so:
$xml->entry->link[0]['href']
I couldn't work out how to access #attributes but I remember now that you don't need to know the name to access it, you can do it using number format too.
If I'm honest I don't really know how I could access the first part with arrows as that appears to be an array too.

Related

Get value from an array PHP - illegal offset

This is the print_r from the $form_data variable / array in php log.
[28-Sep-2018 18:04:03 UTC] Array
(
[cfdb7_status] => unread
[meno] => data
[email] => data
[telefon] => phone
[meno-ucastnika__1] => data
[email-ucastnika__1] => data
[meno-komory__1] =>
[registracne-cislo__1] =>
[_wpcf7_groups_count] => Array
(
[emails] => 1
)
[obchodne-meno] => obchod
[obchodne-sidlo] => fs
[ico] => 50426508
[dic] => dic
[icdph] => icdicko
)
How can I get the value of _wpcf7_groups_count key?
If I want email I simply wrote $form_data['email']. Everything goes like this except _wpcf7_groups_count.
$form_data['_wpcf7_groups_count']
$form_data['_wpcf7_groups_count'][0]['emails']
$form_data['_wpcf7_groups_count']['emails']
Anything from above doesn't work. The first is giving me an illegal offset.
From the data you posted(*),
$form_data['_wpcf7_groups_count']['emails']
should work, and yield 1.
Note that the parent key value is an array, so if the form data goes through some sort of templating engine, the problem might lie there.
I find it strange that you get an error for the first method, and not for the others: in PHP, if you can't reference an array key, you cannot reference any of the descendants, and you still get the error from the parent key. This is what makes me suspect that something else is afoot.
==========
(*) I assumed that you have deleted some information while keeping the formatting. Otherwise writing
[meno-komory__1] =>
[registracne-cislo__1] =>
could possibly be interpreted as a nested key. I saw no 'Array', so I assumed there was just some data missing. But next time write it explicitly to avoid any ambiguity:
[meno-komory__1] => "(redacted)",
[registracne-cislo__1] => "(array, redacted)",

PHP - Format string

I have problems when trying to print on screen this string:
[(E/X)^x]^2
I'm not able to render it correctly here too, but (E/X) should be elevated to x, and then everything to 2.
I hope I was clear.
Thanks!
EDIT
I try to explain myself better. I have $colHeaders, which is an array of array used to give data to a table. In the second child array, title field, is the string I want to render as I asked in the question.
The string must be formatted.
$colHeaders = array(
array(
'title' => $this->translator->trans('price'),
'rowspan' => 2,
'colspan' => 0
),
array(
'title' => '[(E/X)^x]^ 2',
'rowspan' => 0,
'colspan' => 2
),
);
You don't need PHP, it's a simple HTML formatting using the tag. Basically the output you want is:
[(E/X)<sup>x</sup>]<sup>2</sup>
In PHP, you just need to echo that, as such:
echo '[(E/X)<sup>x</sup>]<sup>2</sup>';

Using PHP array retrieved from REST service

Im looking to use a PHP array obtained from a RESTful service.
They array is just a simple array outputted as follows,
array (
0 =>
stdClass::__set_state(array(
'id' => '375',
'primary_name' => 'Beaufort 3',
'price' => '',
'sqft' => '2435',
'bdrm' => '3',
'bthm' => '2.5',
'display_title' => 'Traditional A1',
'full_img_path' => '',
'thumb_path' => '',
'available_in' => 'a:2:{i:0;s:1:"2";i:1;s:1:"5";}',
'display_first' => '',
)),
)
I'm obtaining the data using file_get_contents but of course its no longer an array at this point. What is the best way to convert this back to a usable array?
I have never seen a service that provides this, probably because it is something people would avoid using, but it does look like it could be something they intend for you to be able to use with eval(). I suppose this could be intended as a convenience. I think they have made an error, though. If you use var_export, (which I assume is how this was generated) on an array like this:
$example = array (
array(
'id' => '375',
'primary_name' => 'Beaufort 3',
'price' => '',
'sqft' => '2435',
'bdrm' => '3',
'bthm' => '2.5',
'display_title' => 'Traditional A1',
'full_img_path' => '',
'thumb_path' => '',
'available_in' => 'a:2:{i:0;s:1:"2";i:1;s:1:"5";}',
'display_first' => '',
)
);
then you would get something you could eval into a variable and use in your code. However, if you var_export an array of anonymous objects instead of an array of associative arrays, you will get the type of response you are getting, which I don't know of any way to use. I would guess they are var_exporting the results of a query and they are using FETCH_OBJ instead of FETCH_ASSOC for the fetch style.
EDIT:
I was looking through the comments on var_export after writing this and came across this way of doing it that should work.
$str = str_replace("stdClass::__set_state", "(object)", $str);
eval('$array=' . $str . ';');
But just because something is possible doesn't mean we should do it.
You can convert it back into an array with eval(). http://php.net/manual/en/function.eval.php
eval('$array='.$response.';');
However, this can be dangerous, because eval will take any PHP code given it - if the service is compromised, your code would execute anything passed to it. JSON, if the service supports it, is much safer and natively supported in PHP since 5.2 via json_decode(). http://php.net/manual/en/function.json-decode.php

Result set(Sub Documents) for a mongodb query in php

I am trying to get the result set output for a query. I was able to write the query and am getting the required result using var_dump.
$comment = array(
"Name" => array( 'FirstName' => $n1, 'LastName' => $n2 )
);
$cursor= $c_users->find( $comment );
$result = $cursor->getNext();
echo var_dump( $result );
But I am failing when i want to get output for a sub document.
In case of a normal document the below code works:
echo $result['variable'];
But consider a case where there is a document structure given below:
array(
"Name" => array( 'FirstName' => $n1, 'LastName' => $n2 )
);
Could anyone help me with the syntax to output the first name. I was trying with below code
but did not succeed.
echo $result['variable.FirstName']
So the question is how to access embedded document of mongodb using PHP?
Thanks and Regards,
Sai
In order to access embedded document key (property) you need to treat embedded document like an array in PHP dot notation won't work in PHP for MongoDB so you should try:
echo $result['Name']['FirstName'];
Accessing embedded document with dot notation will work in mongoshell not in PHP driver.

Mongodb and PHP: how to query nested arrays without using the key name

I'm probably missing something simple here but I can't seem to find a way to build a query that will allow me to update a match in a group of nested values.
I have a document like this for a blog app I've been working on (currently uses MySQL):
array (
'_id' => new MongoId("4bc8dcee8ba936a8101a0000"),
'created' => '20100418-201312 +0000',
'post-title' => 'Some Post Title',
'post-body' => 'Blah Blah Blah Blah.',
'post-blog-name' => 'default',
'post-comments' =>
array (
0 =>
array (
'comment-title' => 'Test1',
'comment-body' => 'asdf1',
'created' => '20100418-214512 +0000',
'owner' => 'User1',
),
1 =>
array (
'comment-title' => 'Test2',
'comment-body' => 'asdf2',
'created' => '20100418-214512 +0000',
'owner' => 'User2',
),
),
'owner' => 'zach',
'updated' => '20100418-201312 +0000',
)
I'd like to be able to build a query that can search 'comment-title' for a match and then allow me to update/change/delete data as needed.
Obviously I can perform an update using a query which includes the key value. Something like this works:
$collection->update(
array("post-comments.0.comment-title" => $_POST['comment-title']),
array('$set' => array('entries.0' => array('comment-title' => $_POST['comment-title'], 'comment-body' => $_POST['comment-body'], 'owner' => $_SESSION['username'], 'updated' => gmdate('Ymd\-His O')))));
But I expect I'm missing something that would allow me to leave out the key and still be able to match one of the nested arrays based on a value (in this example the 'comment-title').
Anyway, sorry, this probably isn't the best example and I probably will end up using the keys in comments to identify them (comment #) but since nesting and creating rather complex objects seem to be a few of Mongodbs strong points I'm just hoping someone can point out what it is I might be missing.
A query to remove or update all comments by a specific user (say a user the blog author just black-listed) might be a better example. I'm not sure how I'd do this short of pulling out the entire document and then iterating through the nested arrays using PHP.
try ... notice I removed the "key"
$collection->update(array("post-comments.comment-title" ...
Cheers!

Categories