So I decoded my json, and I get this array out of it
Array (
[totalstreams] => 1
[activestreams] => 1
[currentlisteners] => 0
[peaklisteners] => 2
[maxlisteners] => 100
[uniquelisteners] => 0
[averagetime] => 0
[version] => 2.4.0.147 (posix(linux x64))
[streams] => Array ( [0] => Array (
[id] => 1
[currentlisteners] => 0
[peaklisteners] => 2
[maxlisteners] => 100
[uniquelisteners] => 0
[averagetime] => 0
[servergenre] => Misc
[serverurl] => http://example.com
[servertitle] => Van Den Berg Stream
[songtitle] => Katy Perry - This Is How We Do
[streamhits] => 3
[streamstatus] => 1
[backupstatus] => 0
[streampath] => /stream
[streamuptime] => 1998
[bitrate] => 128
[content] => audio/mpeg ) ) )
I got to this using:
print_r($json)
I'm interested in the songtitle part, so normally I'd go:
$json['streams']['songtitle'];
But it just throws a Undedefined index error.
Do you huys know what I do wrong?
$json['streams'][0]['songtitle'];
That is what you want. Look carefully at your print_r results again, you'll notice the following after streams:
[streams] => Array ( [0] => Array (
You need to take that 0 into account.
Related
I have an api with a list of arrays inside one array and each array inside this array it has one key [attributes] and inside this key one array with key [CITY_NAME]
this is my array coming from api
this array number is 0 but every day this number is changed and my figures come wrong, what is the good way to always have my city figures using the key [CITY_NAME]
I am fetching the data using this code
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$data = $json_data['features'];
$mycity = $data[0]['attributes'];
Array
(
[0] => Array
(
[attributes] => Array
(
[CITY_NAME] => city1
[Name] => city1
[ADMIN_NAME] => city1
[POP_CLASS] => 5,000,000 to10,000,000
[Population] => 7676654
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2018
[CityID] => 14
[Longitude] => 46.7614868685786
[Latitude] => 24.7388786516234
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city1
[Join_Count] => 61
[Confirmed_SUM] => 5152
[Deaths_SUM] => 9
[Recovered_SUM] => 1407
[Active_SUM] => 3736
[Tested_SUM] => 376607
[ObjectId] => 14
)
)
[1] => Array
(
[attributes] => Array
(
[CITY_NAME] => city2
[Name] => city2
[ADMIN_NAME] => city2
[POP_CLASS] => 1,000,000 to 5,000,000
[Population] => 1675368
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2010
[CityID] => 9
[Longitude] => 39.8148987363852
[Latitude] => 21.4273876500039
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city2
[Join_Count] => 59
[Confirmed_SUM] => 6848
[Deaths_SUM] => 85
[Recovered_SUM] => 1145
[Active_SUM] => 5618
[Tested_SUM] => 0
[ObjectId] => 9
)
)
Since I may have misunderstood and you may have many, just build a new array with CITY_NAME:
foreach($data as $values) {
$result[$values['attributes']['CITY_NAME']] = $values['attributes'];
}
Now you can access by CITY_NAME:
echo $result['city1']['Population'];
This might be easier. Extract all attributes from all arrays into an array, then create an array from that indexed by CITY_NAME:
$data = array_column(array_column($json_data['features'], 'attributes'),
null, 'CITY_NAME');
i am trying to calculate shipping charge and i am getting a response from REST API. i want to get return the value of "delivery_charges" form response array?
Array
(
[response] => Array
(
[delivery_charges] => 41
[return_charges] => 83
[all_charges] => Array
(
[DL] => 32
[FS] => 30
[DTO] => 0
[RTO] => 32
[ST] => 14.5
[COD] => 0
[TDS] => 0
[CARD] => 0
[CNC] => 0
)
[canc_charges] => 0
[pickup_charges] => 0
[params] => Array
(
[md] => E
[pt] => prepaid
[cl] => xxxxxxx
[o_pin] => 560076
[d_pin] => 560062
[token] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[gm] => 100
[d_city] => Bangalore
[o_city] => Bangalore
)
)
)
i got answer by just using
return $shipping_charge['response']['delivery_charges'];
You need to simply add the following at the end. If $shipping_charge is the array then write:
return $shipping_charge['response']['delivery_charges'];
I have an array like this
Array
(
[0] => stdClass Object
(
[cat_id] => 3
[cat_name] => sample 3
[cat_description] =>
[cat_folder] => sample_3
[cat_path] => sample_3
[cat_parent] => 0
[cat_num_files] => 0
[cat_num_files_total] => 0
[cat_user_roles] =>
[cat_owner] => 1
[cat_icon] =>
[cat_exclude_browser] => 0
[cat_order] => 0
)
[1] => stdClass Object
(
[cat_id] => 2
[cat_name] => sample 2
[cat_description] =>
[cat_folder] => sample_2
[cat_path] => sample_3/sample_2
[cat_parent] => 3
[cat_num_files] => 0
[cat_num_files_total] => 0
[cat_user_roles] =>
[cat_owner] => 1
[cat_icon] =>
[cat_exclude_browser] => 0
[cat_order] => 0
)
)
I need to list these like
-sample 3
--sample 2
---sample 4
There is no limit for the link depth, a category may have 5 deep, it may be 10. I have tried this in a foreach loop but failed to retrieve a list like a tree.
I recommend recursion, becouse you don't know how nested it will be. Bellow is example of recursion function.
function genCat($parentCat){
echo $parentCat->name;
if ($parentCat->haveChildren){
foreach($parentCat->children as $child){
genCat($child);
}
}
}
This is just example, hope it helps
I want to retrieve quantities from this array.
invoice Object
(
[data:private] => Array
(
[i_status] => pend
[i_title] => 500 HLCoins , 500 HLCoins x8
[i_member] => 1
[i_items] => Array
(
[0] => Array
(
[act] => new
[app] => nexus
[type] => product
[cost] => 0
[tax] => 0
[renew_term] => 0
[renew_units] =>
[renew_cost] => 0
[quantity] => 1
[physical] =>
[shipping] => Array
(
)
[weight] => 0
[itemName] => 500 HLCoins
[itemID] => 3
[cfields] => Array
(
)
[extra] =>
[opt_id] => 0
[associated] =>
[assocBought] =>
[groupRenewals] => 0
[methods] => Array
(
)
[k] => 0
[_tax] => 0
)
[1] => Array
(
[act] => new
[app] => nexus
[type] => product
[cost] => 0
[tax] => 0
[renew_term] => 0
[renew_units] =>
[renew_cost] => 0
[quantity] => 8
[physical] =>
[shipping] => Array
(
)
[weight] => 0
[itemName] => 500 HLCoins
[itemID] => 3
[cfields] => Array
(
)
[opt_id] => 0
[groupRenewals] => 0
[methods] => Array
(
)
[_tax] => 0
)
)
[i_total] => 0
[i_date] => 1347217384
[i_return_uri] =>
[i_paid] => 0
[i_status_extra] => a:1:{s:4:"type";s:4:"zero";}
[i_discount] => 0
[i_temp] =>
[i_ordersteps] => 0
[i_noreminder] => 1
[i_renewal_ids] => Array
(
)
[i_po] =>
[i_notes] =>
[i_shipaddress] =>
[i_id] => 229
)
[takeAction] => 1
)
I've tried a bunch of codes like $invoice->quantity, $invoice[1]->quantity, $this->$invoice->quantity, but none of them seem to display.
It still does not display at all, I tried to print_r and that is the array it gave me.
All the variables are private which means you cannot access them from outside the object. Check out the class definition for the invoice class. There should be some function to get the quantities from the object, or else you could add such a feature to the class.
The whole point of this is separation of concerns. The class may change in the future and possibly not use the same structure, therefore you should use object functions to access the properties, do not access them directly as variables.
You can read more on this subject in the manual or in a book about object-oriented programming.
Seems like all the data is in a private property. You cannot access it from outside directly.
Read the documentation for the class. It should have some method you can call, like getQuantity(), that'll get you the data. It depends on how the class was written and how it is supposed to be used.
I know it's simple task, but my mind not clicking at present for appropriate solution.
I have 2 arrays
$array1=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
)
$array2=Array
(
[302] => Array
(
[medication_id] => 302
[id] => 14
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
[change_date] =>
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
I want Output:
$outputarray=Array
(
[302] => Array
(
[id] => 302
[medical_id] => 55
[medication_name] => disprin
[medication_frequency] => 1
[medication_status] => Stoped
[users_id] => 106
[update_date] => 2012-03-02 11:03:57
)
[305] => Array
(
[medication_id] => 305
[id] => 15
[medical_id] => 57
[medication_name] => disprin
[medication_frequency] => 2
[medication_status] => Continued
[users_id] => 106
[update_date] => 2012-03-02 11:02:46
[change_date] =>
)
)
means if same key is exist in array1 then take it from array1 else from array2.
I tried with some PHP functions like array_merge,array_merge_recursive but not got desired output.
am not able to recollect memory now, Please suggest me any PHP array function to sort out this.
It's so simple that it's almost unethical:
$outputArray = $array1 + $array2;
Array union (above) does what you describe. Using array_merge could also work in general, but not directly with such input because your keys are integers.
Maybe this http://php.net/manual/pt_BR/function.array-intersect.php
Check with in_array() & store using array_push() will be a solution for you.