I have posted parts of this.. but this a different question for it
i have the below
foreach ($results['comments'] as $item) {
echo 'Date: '. $item['created_at'] .'<br/>';
echo 'Description : '. $item['html_body'] .'<br/>';
echo 'Attachments : '. $item['attacments->url'] .'<br/>';
echo 'Filename : '. $item['file_name'] .'<br/>';
echo "<br>";
}
So basically, my Date and Description work, BUT the attachments wont work, b/c i dont think thats the correct way to get an object thats within an array of an array? hope i explained it correctly.
the comments array has all the date as a single object and so is description, then it has this trailing.
[public] => 1 [trusted] => 1 [attachments] => Array ( [0] => Array ( [url] => https://url/api/v2/attachments/IDHERE.json [id] => ID#[file_name] => name of file here
Take a look at your array dump
[public] => 1
[trusted] => 1
[attachments] => Array (
[0] => Array (
[url] => https://url/api/v2/attachments/IDHERE.json
[id] => ID#
[file_name] => name of file here
Get the values like this:
$Attachments = $item['attachments'];
$AttachmentsUrl = $Attachments[0]['url'];
$Attachmentsid = $Attachments[0]['id'];
$AttachmentsFileName = $Attachments[0]['file_name'];
Related
I'm returning array data from another service.
The data I got after JSON decode is like this
Array
(
[result] => Array
(
[0] => Array
(
[number] => INC00001234
[short_description] => LLKS portal outage
)
[1] => Array
(
[number] => INC00001235
[short_description] => Server degradation
)
[2] => Array
(
[number] => INC00001236
[short_description] => Printer not printing
)
)
)
Now, all I wanted is to loop through the JSON and list just the numbers like this
Number : INC00001234
Description : LLKS portal outage
Number : INC00001235
Description : Server degradation
Number : INC00001236
Description : Printer not printing
How do I do that?
I assume that your JSON's datas are in a variable. I used $data below, but you need to use yours.
foreach($data['result'] as $line){
echo 'Number : ' . $line['number'] . '<br/>\nDescription : ' . $line['short_description'] . '<br/>\n';
}
foreach($yourArray['result'] as $item){
echo "\nNumber:{$item['number']}\nDescription:{$item['short_description']}\n";
}
after the alert(JSON.stringify(ort_list));
I'm getting output like this.
[{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}]
now I want to get the alert value of BUNDESLAND_NAME how could I get that.
Use this:
var data = [{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}];
alert(data[0].result.BUNDESLAND_NAME);
or directly, based on your code
alert(ort_list[0].result.BUNDESLAND_NAME);
assume
ort_list = [{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}];
<?php
$data='[{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}]';
$newData=json_decode($data,true);
echo '<pre>';
print_r($newData);
echo '</br>';
echo 'the field you are looking for is : '.$newData[0]['result']['BUNDESLAND_NAME'];
And the output is :
Array
(
[0] => Array
(
[status] => success
[result] => Array
(
[BUNDESLAND_NAME] => Rheinland-Pfalz
[KREIS_TYP] => Landkreis
[GEMEINDE_NAME] => Aach
[GEMEINDE_LAT] => 4978960
[GEMEINDE_LON] => 659052
[ID_0] => 2
[ORT_NAME] => Aach
[ORT_LAT] => 4978972
[ORT_LON] => 659055
[PLZ] => 54298
)
)
)
the field you are looking for is : Rheinland-Pfalz
So here i provide you how your array looks like and how i access the specific element you wanted. I hope it's clear.
I have this array:
Array
(
[result] => Array
(
[lastModified] => 1465097340000
[name] => Ulminia
[realm] => Zangarmarsh
[battlegroup] => Rampage
[class] => 3
[race] => 4
[gender] => 1
[level] => 100
[achievementPoints] => 14915
[thumbnail] => hellscream/74/113337162-avatar.jpg
[calcClass] => Y
[faction] => 0
[items] => Array
(
[averageItemLevel] => 710
[averageItemLevelEquipped] => 709
[head] => Array
(
[id] => 125899
[name] => Warmongering Gladiator's Helm
[icon] => inv_helm_mail_raidhunter_p_01
[quality] => 4
[itemLevel] => 710
[tooltipParams] => Array
(
[transmogItem] => 71356
[timewalkerLevel] => 100
)
I want to echo out from the [head] array the [id] and the [quality]. If i just echo out the [id] everything works, but if i want to echo out the [quality] too, it doesn´t work.
My code:
$items = $r['result']['items'];
echo 'Head: '.$items['head']['id']['quality']."\n";
foreach($items['head']['tooltipParams'] as $key => $value){
echo 'head_'.$key.': '.$value.'\n';
}
echo $items['head']['id']['quality'];
The above statement means you are printing out the subkey "quality" of key "id", which doesn't exist.
You need to concatenate both key values as follows:
echo $items['head']['id'] . ' ' . $items['head']['quality'];
... or
echo $items['head']['id'], ' ', $items['head']['quality'];
$items['head']['id']['quality']."\n"; is trying to read the element with the key quality inside the array stored inside id. However, id is not an array, which is why this fails.
In order to read two fields, you need to read them seperately:
echo 'Head: ID=' . $items['head']['id'] . ', quality = ' . $items['head']['quality'] . "\n";
Notice that the id and the quality are in the same array.
//ID
echo $items['head']['id'];
//Quality
echo $items['head']['quality'];
I'm pulling in a list of my vimeo albums using the Vimeo API and the looping three times through the array to get to the albums. It works fine.
My question is, I'm isolating the date, so how can I create a new array and sort it by the date?
While where at it, is there a way to jump to the third level of a multi-dimensional array?
$albums=$vimeo->call('vimeo.albums.getAll', array('user_id' => $myUserId));
$albums_array=object_2_array($albums);
foreach($albums_array as $album_array_two){
foreach($album_array_two as $album_array_three){
foreach($album_array_threeas $album){
if(stristr($album['title'],'conference')){
$title=$album['title'];
$description=$album['description'];
$date=stristr($album['description'],'.',true);
$year_comma=stristr($date,',');
$year=ereg_replace("[^0-9]", "", $year_comma);
$url_title='http://www.psfk.com/events/'.str_replace( " ", "-", strtolower($title));
$url=''.$title.'';
$thumb=$album['thumbnail_video']['thumbnails']['thumbnail'][1]['_content'];
echo '<li class="album">';
echo '<img src="'.$thumb.'" alt="'.$title.'" />';
echo '<div class="info">';
echo '<h2>'.$url.'</h2>';
echo $description.'<br />';
echo 'View...';
echo '</div></li>';
}
}
}
}
Sample of the array returning one item:
Array (
[generated_in] => 0.0828
[stat] => ok
[albums] => Array (
[on_this_page] => 7
[page] => 1
[perpage] => 50
[total] => 7
[album] => Array (
[0] => Array (
[id] => 1690236
[title] => Interviews
[description] =>
[created_on] => 2011-09-10 21:43:49
[total_videos] => 1
[url] => Array (
[0] => http://vimeo.com/album/1690236
)
[video_sort_method] =>
[thumbnail_video] => Array (
[id] => 28825158
[owner] => 718882
[title] => Where Inspiration Comes From [thumbnails] => Array (
[thumbnail] => Array (
[0] => Array (
[height] => 75
[width] => 100
[_content] => http://b.vimeocdn.com/ts/192/593/192593029_100.jpg
)
)
)
)
)
)
)
)
In order to sort by date, you can use the php_function array_multisort(). There is a good example on that page that I think shows what you need. I'll try to provide a better example using your data. Suppose after looping through your albums you end up with an array $myAlbums that looks like this:
Array (
[0] => Array(
[title] => My Title
[description] => some description
[date] 01-05-2011
)
[1] => Array(
.......
)
In order to sort this by date, you could do the following (taken from the example on the php page)
<?php
// Obtain a list of columns
foreach ($myAlbums as $key => $row) {
$date[$key] = $row['date'];
}
// Sort the data with volume descending, edition ascending
// Add $myAlbums as the last parameter, to sort by the common key
array_multisort($date, SORT_DESC, $myAlbums);
?>
Then you can print_r($myAlbums); and you should see that it is sorted. You might have to change the SORT_DESC flag depending on what formate your dates are in. I can't really explain HOW this works, because I'm still trying to figure it out myself... but I think it is what you need.
So I have some data that's coming back from a database query, and the resulting array (gotten with print_r) looks like this (it's assigned to a var called $locationData):
Array
(
[0] => Array
(
[id] => 1
[location_name] => Cook Minnesota
[location_lat] => 47.72037
[location_long] => -90.32667
)
[1] => Array
(
[id] => 2
[location_name] => Lake Minnesota
[location_lat] => 47.18238
[location_long] => -91.35301
)
[2] => Array
(
[id] => 3
[location_name] => St. Louis Minnesota
[location_lat] => 46.83572
[location_long] => -91.96299
)
)
I have a foreach loop that needs to grab the location_name from each. It looks like this:
foreach ($locationData as $location => $value ) {
echo '<p>name ' . $location['location_name']. '</p>';
}
I'm 99% certain this should work; it's basically the same code I've used a dozen times before. But the echo is not returning anything - not even the static text (<p>name). It's not throwing any errors, and if I try to do a print_r($location), I get nothing.
Any ideas? I'm sure it's something really simple.
You want $value['location_name'] because $value represents the arrays while $location represents the indexes of the arrays:
echo '<p>name ' . $value['location_name']. '</p>';
It is:
foreach ($locationData as $location => $value ) {
echo '<p>name ' . $value['location_name']. '</p>';
}