I' ve got a XML, which I parsed into a PHP array. It looks like this:
Array
(
[Album] => Array
(
[id] => 12
[name] => My album
[id_parent] => 0
[row] => Array
(
[Album] => Array
(
[row] => Array
(
[id] => 14
[name] => Another album
[id_parent] => 12
[Photos] => Array
(
[row] => Array
(
[0] => Array
(
[id] => 1078
[name] => My first photo
[Album] => Array
(
[row] => Array
(
[name] =>Another album
[id] => 15
[Photos] => Array
(
[row] => Array
(
[0] => Array
(
[id] => 1069
[name] => Summer photo
[checked] => [checked]
)
and so on.
How can I parse it into a HTML list (in nested ul's), to get something like
My album
-Another album
--My first photo
---Another album
----Summer photo
---[end Another album]
--[end My first photo]
-[end Another album]
[end My album]
Albums and photos are just samples. i need to find a way how to parse it to HTML.
EDIT:
Okay, I add something what I really meant - "checked" node. I simply need to put the parent node into tags.
I would use a quick XSLT transform to do it.
If you post a sample of the base XML I can point you in the right direction, but otherwise, check out the examples at these two links. With a little modification they can do what you want.
http://www.w3schools.com/xsl/xsl_for_each.asp
http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
Related
I have an array like:
Array
(
[0] => Array
(
[title] => Title 1
[value] => Value 1
[id] => 1428735262
)
[1] => Array
(
[title] => Title 2
[value] => Value 2
[id] => 2428735262
)
[2] => Array
(
[title] => Title 3
[value] => Value 3
[id] => 3428735262
)
)
With mustache I am able to iterate overall of them using:
{{#values}}
<h1>{{title}}</h1>
{{{value}}}
{{/values}}
But what if I only want to show specific value, for example only second element of the array (one with Title 2)?
I tried like:
{{#values}}
{{#2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/2428735262}}
{{/values}}
and:
{{#values.2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/values.2428735262}}
But none worked. Does anyone know how to get specific item from array ?
PS. I am using PHP version of mustache. Thanks
You can use this:
{{values.1.value}}
Read more at:
https://github.com/bobthecow/mustache.php/wiki/Variable-Resolution
I'm trying to get info out of this information:
Array (
[result] => success
[totalresults] => 1
[startnumber] => 0
[numreturned] => 1
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
[deptid] => 1
[userid] => 39
[name] => Mark Lønquist
[email] => mark.loenquist#outlook.com
[cc] =>
[c] => 79rzVBeJ
[date] => 2013-04-25 16:14:24
[subject] => test
[status] => Open
[priority] => Medium
[admin] =>
[attachment] =>
[lastreply] => 2013-04-25 16:14:24
[flag] => 0
[service] =>
)
)
)
)
The results are printed using:
print_r($results);
Usually, I've been able to do a simple:
$var = $results['something'];
To get it out, but it wont work with this :( Any help is appreciated.
After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:
Array (
[result] => success
[totalresults] => 1
...
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
...
)
)
)
)
You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].
If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):
foreach ($results['tickets']['ticket'] as $ticket) {
echo $ticket['tid'];
}
To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.
Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.
However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].
I am trying to create various, standard HTML list styles from PHP arrays for a movie database I'm building as part of my final year project at university, however I'm having some trouble accessing/understanding how to do this. I've got the relevant data from the various APIs I'm using, but I'd like to create the entire list dynamically, dropping in the correct bits of information where appropriate.
Details of the two list styles I'm looking to create are below.
List style 1 - Character/Cast details
For the first style of list, I'd like to create it as:
<ul>
<li>Cast name - Character name</li>
<li>Cast name - Character name</li>
<li>etc. etc.</li>
<ul>
My data set direct from the array looks like this:
Array
(
[abridged_cast] => Array
(
[0] => Array
(
[name] => Daniel Craig
[id] => 162687443
[characters] => Array
(
[0] => James Bond
)
)
[1] => Array
(
[name] => Javier Bardem
[id] => 162661456
[characters] => Array
(
[0] => Silva
)
)
[2] => Array
(
[name] => Judi Dench
[id] => 162652435
[characters] => Array
(
[0] => M
)
)
[3] => Array
(
[name] => Ralph Fiennes
[id] => 162653681
[characters] => Array
(
[0] => Gareth Mallory
[1] => Mallory
)
)
[4] => Array
(
[name] => Naomie Harris
[id] => 162705781
[characters] => Array
(
[0] => Eve
)
)
)
In the above example, there's five cast members w/ their respective character names, however on some other examples, there's more or less than five, so, ideally, I'd want the system to know however many cast members were given and create a list for each, up to a maximum of 10 names.
List style 2 - Videos
Similar to the last style, however this list will hopefully look like this:
<ul>
<li><a href='<http://youtube.com/<dynamically generated URL direct to the clip>'><img src='dynamically generated URL to the clip's thumbnail' /></a></li>
<li>same as above...</li>
</ul>
The source array looks like this:
Array
(
[id] => 37724
[youtube] => Array
(
[0] => Array
(
[name] => Trailer
[size] => HQ
[source] => 24mTIE4D9JM
)
[1] => Array
(
[name] => Official Trailer
[size] => HD
[source] => 6kw1UVovByw
)
[2] => Array
(
[name] => Trailer 1
[size] => HD
[source] => 1Uyjf5Pp0Ko
)
[3] => Array
(
[name] => Trailer 2
[size] => HD
[source] => 5Ejo9_3iUpw
)
)
)
Apologies for the length of this post, thanks in advance to anyone who may be able to assist!
The thing you are looking for is called a foreach loop. For your first example it would look something like this:
foreach($dataSet['abridged_cast'] as $castMember){
echo '<li>'.$castMember['name'].'-'.implode(', ', $castMember['characters']). '</li>';
}
Now this example is purely illustratory though it most definetely be the begginers approach.
Still, I would strongly recommend investigating MVC patterns and PHP frameworks, like for example YII. If deployed and configured right YII with all its goodness should make all the repetitive work done for you here.
I've seen that your question contains two parts, though I am not willing to write a sample for the second one, since it is practically the same as the first and with proper attention - you will get on track and figure it out.
The best way for me to explain this is to show you. Seems like a float() error in a 64bit system.
when i call /anotherfeed/feed or any page for that matter, posts with story_tags return some of the id's as a float error.
sample story tag with float error in id. [id] => 1.7153566624E+14
My question is, how do i fix this, or what am i doing wrong? all i am doing is looping in a foreach statement.
if($fvalue[story_tags]){
echo 'Tags: ';
$sTags=$fvalue[story_tags];
foreach ($sTags as $skey=>$svalue){
foreach ($svalue as $gkey=>$hvalue){
$id=$hvalue[id];
echo ''.$hvalue[name].' '.$id.' ';
}
}
}
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
Array
(
[data] => Array
(
[0] => Array
(
[id] => 171535666239724_156133294510726
[from] => Array
(
[name] => Another Feed
[category] => App page
[id] => 171535666239724
)
[story] => Another Feed shared Non-Profits on Facebook's photo.
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
[20] => Array
(
[0] => Array
(
[id] => 41130665917
[name] => Non-Profits on Facebook
[offset] => 20
[length] => 23
[type] => page
)
)
)
[picture] => http://photos-d.ak.fbcdn.net/hphotos-ak-ash3/557037_10150932300320918_1908237167_s.jpg
[link] => http://www.facebook.com/photo.php?fbid=10150932300320918&set=a.85612830917.95996.41130665917&type=1
[name] => Wall Photos
[caption] => Have you heard of the #[159208207468539:274:One Day without Shoes] (ODWS) campaign? ODWS is an annual initiative by #[8416861761:274:TOMS] to bring awareness around the impact a pair of shoes can have on a child's life.
During the 2012 campaign, #[20531316728:274:Facebook] drove 20% of traffic to the ODWS microsite and TOMS even launched a Facebook-exclusive "Barefoot & Blue" giveaway with #[25266987484:274:Essie Nail Polish] for the second year in a row.
We think this is a pretty cool example of creating exclusive content around an important initiative that keeps people engaged and involved!
[properties] => Array
(
[0] => Array
(
[name] => By
[text] => Non-Profits on Facebook
[href] => http://www.facebook.com/nonprofits
)
)
[icon] => http://static.ak.fbcdn.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif
[type] => photo
[object_id] => 10150932300320918
[application] => Array
(
[name] => Photos
[id] => 2305272732
)
[created_time] => 2012-07-02T17:57:23+0000
[updated_time] => 2012-07-02T17:57:23+0000
[comments] => Array
(
[count] => 0
)
)
solution:
cURL - had to use number format with PHP_EOL to solve in cURL.
// $locs = curl call to graph api for /anotherfeed/feed, still need solution for foreach.
$locs=json_decode($returned, true);
$stId=number_format($locs[data][1][story_tags][0][0][id], 0, '', '').PHP_EOL;
echo $stId;
PHP-SDK
solution is same, long numbers in the foreach loop need to be ran through number_format.
I have an file uploading site, it has an option of uploading through urls, what I am trying to do is whenever a user uploads through url, I check my database if a file exists that was uploaded through same url it displays the download url directly instead of uploading it again.
The data sent to uploading script is in array form like:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
[1] => http://i41.tinypic.com/28cfub7.jpg
[2] => http://i41.tinypic.com/12dsa32.jpg
)
and the array used for outputing the results is in form like this:
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
[1] => Array
(
[id] => 44
[name] => 28cfub7.jpg
[size] => 105544
[descr] =>
[password] =>
[delete_id] => D392
[upload_id] => 6676FD881
)
[2] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)
Now I want is that if the link http://i41.tinypic.com/28cfub7.jpg is already upload I just add it to output array but maintain it in a order (if the link added was 2nd in array the output result should also show it in 2nd)
So what function should be used to remove the matched urls from input array and a function to add it output array in the order no.
// edited
Yes unset will do the thing but I want to maintain the order:
For example after unsetting the array looks like this:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
// [1] was removed
[2] => http://i41.tinypic.com/12dsa32.jpg
)
but the output array would be
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
// this will become [1], so how can i add another output[1] and shift other
// items after it to [2], [3] and so on...
[1] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)
Well, you can add it to the output array by doing something like:
$OutArray[2] = $element;
Where $element is another Array with the id, name, size (etc...) elements.
As for removing from the array:
unset($OutArray[2]);
You may want to read Array (PHP manual).
If you have an indexed array, you can remove a value by doing:
unset ($array[2]);
If you want to add an item to an array, use this shorthand of array_push (you don't need to specify an index!):
$array[] = "new object";
All documentation is on php.net/arrays
Why don't use an if statement and/or file_exists() to see if the file is there. If you already have an array with the values then it just won't be uploaded again.