I'm using SimpleXML to extract images from a public feed # Flickr. I want to put all pulled images into an array, which I've done:
$images = array();
foreach($channel->item as $item){
$url = $path->to->url;
$images[] = $url;
}
From this I can then echo out all the images using:
foreach($images as $image){
//output image
}
I then decided I wanted to have the image title as well as the user so I assumed I would use:
$images = array();
foreach($channel->item as $item){
$url = $path->to->url;
$title = $path->to->title;
$images[$title] = $url;
}
I thought this would mean by using $image['name of title'] I could output the URL for that title, but it gives an Illegal Offset Error when I run this.. and would only have the title and url, but not the user.
After Googling a bit I read you cannot use _ in the array key, but I tried using:
$normal = 'dddd';
$illegal = ' de___eee';
$li[$normal] = 'Normal';
$li[$illegal] = 'Illegal';
And this outputs right, ruling out _ is illegal in array keys (..I think).
So now I'm really confused why it won't run, when I've used print_r() when playing around I've noticed some SimpleXML objects in the array, which is why I assume this is giving an error.
The ideal output would be an array in the format:
$image = array( 0 => array('title'=>'title of image',
'user'=>'name of user',
'url' =>'url of image'),
1 => array(....)
);
But I'm really stumped how I'd form this from a foreach loop, links to reference as well as other questions (couldn't find any) are welcome.
$images = array();
foreach ($channel->item as $item){
$images[] = array(
'title' => $item->path->to->title,
'url' => $item->path->to->url
);
}
foreach ($images as $image) {
echo "Title: $image[title], URL: $image[url]\n";
}
Underscore is not forbidden symbol for array keys, the only problem you might run to, that titles some times overlap and you might loose some items in your array. Most correct way would be deceze example
If you prefer associative array you can use image path as array key and title as value.
Related
If I println($values) I will get the results of the correct attributes for each file fully written as shown in array as key on left but I would like to run the results against the array (valuesCodes) to get the values on right.
Any help would be greatly appreciated, I'm brand new to PHP.
$valuesCodes = array(
"Anfield" => "ANF",
"Lower" => "LOW",
"Burntown"=>"BUR");
foreach ($Files as $file) {
$contents = simplexml_load_file($file);
$values = $contents->Match->attributes()->stadium;
}
For best practice I renamed some variables (Look for PSR).
First of all the $valueCodes should be on top, else they are not known inside the foreach.
As it looked like your stadium value contain whitespaces or line breaks which can be eliminated by trimming them.
$valueCodes = [
"Anfield" => "ANF",
"Lower" => "LOW",
"Burntown" => "BUR",
];
foreach ($files as $file) {
$contents = simplexml_load_file($file);
$value = trim($contents->Match->attributes()->stadium);
echo $valueCodes[$value] ?? 'N/A';
}
Basically I am calling an API to get an array of URLs for an image.
I start off by doing this
$mainResponse = array(
"result" => array(
),
"ack" => "success"
);
I will then make my call and add the image URLs like this:
foreach($resp->Item as $item) {
$picture = $item->PictureURL;
array_push($mainResponse['result'], $picture);
}
Finally, I will echo this out to me.
echo json_encode($mainResponse);
The problem I am facing is that my response is
{"result":[{"0":"IMAGE_URL","1":"IMAGE_URL"}],"ack":"success"}
Where I would want it to be like....
{"result":["IMAGE_URL","IMAGE_URL"],"ack":"success"}
Where did I go wrong in my PHP code?
Seems like $picture is a associative array, change the foreach loop with this:
foreach($resp->Item->PictureURL as $item) {
foreach($item as $_item){
array_push($mainResponse['result'],$_item);}
}
For some reason this API returns an object instead of an array.
You can just do:
foreach ($resp->Item as $item) {
$picture = $item->PictureURL;
array_merge($mainResponse['result'], (array)$picture);
}
You can use array_push if you want every item to have separate pictures.
I am trying to get string from JSON, that i should use later. The problem is that my code (depending which file i use) doesn't give me my string with special characters. Strange thing is that one file does everything right, while other is not, even through they contain exactly the same PHP code.
This is that i get from the first one:
[title] => 1611 Plikiškiai–Kriukai (Incorrect)
And from another:
[title] => 1611 Plikiškiai–Kriukai (Correct one)
Here is some code:
function get_info($id) {
$json = file_get_contents('http://restrictions.eismoinfo.lt/');
$array = json_decode($json, true);
//get object/element with such id, so then it would be possible take needed parts/values
$result_array = array();
foreach ($array as $key => $value) {
if ($id == $value['_id']){
$result_array = $value;
}
}
//Street name
$title = $result_array['location']['street'];
echo $title;
}
get_info($id); //$id is some string like 143crt9tDgh5
1st file is located in folder "test", while 2nd in "project".
Any ideas why is that happening?
I am learning PHP. I have to extract all the script tags of a particluar $url whose src contain a particular word say 'abc'.
I have tried this:
$domd = new DOMDocument();
#$domd->loadHTML(#file_get_contents($url));
$data = array();
$items = $domd->getElementsByTagName('script');
foreach($items as $item) {
if($item->hasAttribute('src')){
$data[] = array(
'src' => $item->getAttribute('src')
);
}
}
print_r($data);
echo "\n";
The above code gives me the list of all the script tag's src's present in the $url.
But how should i check if a src in a script tag contains a word 'abc' ?
If you need to check each value in the array, to see if it contains 'abc', try a foreach() loop with an if statement using strpos() to see if the value contains 'abc', and then do something with it.
Something like this should do the trick:
foreach( $data as $key=>$value ) {
if( strpos( $value['src'],'abc' ) !== false ) {
//do something with it here
}
}
Just edited this. Call the ['src'] element of the subArray and use that in strpos(). Alternately, you could change your line that builds the $data array to this, since you only have one element in each subArray:
if($item->hasAttribute('src')){
$data[] = $item->getAttribute('src');
}
good evening. ingore all other elements, I want get first image in each foreach. how to?
$data = json_decode($json,true);
foreach ($data['data'][0] as $image) {
echo '<img src="'.$image['images'][0]['source'][0].'" />';
}
json tree is too large, over the letters paste limit.
Oops! Your question couldn't be submitted because:
body is limited to 30000 characters; you entered 68494
so here is the url u can get the json tree https://graph.facebook.com/5550296508/photos
and u can paste in http://jsonlint.com/ look the struction well. Thanx.
With json_decode every {} (object) will become an object(stdClass) and every [] (array) will become an array. So:
$data->data[0]->images[0]->source
is what you need, to reach the first image source.
Edit: since the second parameter of json_decode is true, it will become an associative array, and it will be like:
$data['data'][0]['images'][0]['source']
To get all the images:
$images = array();
foreach ($data['data'] as $d)
{
foreach ($d['images'] as $i)
{
$images[] = $i['source'];
}
}