How do i get specific value from first array and second array - php

How i can get value of latitude in first array and second array using php? I hope you can teach me how to do it.. Thanks in advance
Here the array
Array
(
[0] => Array
(
[unit_id] => 2
[added_when] => 2014-01-10 09:57:05
[latitude] => 11.6994922
[longitude] => 122.36759245
[speed] => 0
[head] =>
[valid] => 1
[bearing] => 0
[accuracy] => 141
[altitude] => 0
[distance] => 18
[id] => 2014-01-10
[start_datetime] => 2014-01-10 09:53:48
[stop_time] => 18:38:05
[client_id] => 2
[imei] => 865687015810821
[name] => cm flare
[group_id] => 1
[status] => 1
)
[1] => Array
(
[unit_id] => 2
[added_when] => 2014-01-09 10:48:06
[latitude] => 11.6994922
[longitude] => 122.36759245
[speed] => 0
[head] =>
[valid] => 1
[bearing] => 0
[accuracy] => 141
[altitude] => 0
[distance] => 0
[id] => 2014-01-09
[start_datetime] => 2014-01-09 10:45:46
[stop_time] => 17:27:07
[client_id] => 2
[imei] => 865687015810821
[name] => cm flare
[group_id] => 1
[status] => 1
)
)

If you has a dynamic array
$first = $array[0]['latitude'];
$last = $array[count($array)-1]['latitude'];
For you example you can just use this code
$first = $array[0]['latitude'];
$last = $array[1]['latitude'];

if you want a general and reusable way define this function:
function array_pluck ($toPluck, $arr) {
return array_map(function ($item) use ($toPluck) {
return $item[$toPluck];
}, $arr);
}
then
$latitudes = array_pluck("latitude", $myarrays);

Use a foreach loop.
foreach($arrays as $array) {
echo $array['latitude'];
echo $array['longitude'];
}

Related

Accessing second array (non object) in decoded JSON

I'm trying to access the second array (contacts) in this decoded json to grab the ID next to first_name (the two ID keys could be different), but the second array doesn't seem to be an object so I can't find a loop which can acces it:
stdClass Object (
[data] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[name] => test test
[display_name] => test test
[balance] => 0
[paid_to_date] => 0
[updated_at] => 1578494555
[archived_at] =>
[address1] => Street
[address2] =>
[city] => Town
[state] => State
[postal_code] => Code
[country_id] => 0
[work_phone] => Number
[private_notes] =>
[public_notes] =>
[last_login] =>
[website] =>
[industry_id] => 0
[size_id] => 0
[is_deleted] =>
[payment_terms] => 30
[vat_number] =>
[id_number] =>
[language_id] => 0
[currency_id] => 0
[custom_value1] =>
[custom_value2] =>
[invoice_number_counter] => 1
[quote_number_counter] => 1
[task_rate] => 0
[shipping_address1] =>
[shipping_address2] =>
[shipping_city] =>
[shipping_state] =>
[shipping_postal_code] =>
[shipping_country_id] => 0
[show_tasks_in_portal] => 1
[send_reminders] => 1
[credit_number_counter] => 1
[custom_messages] => {}
[contacts] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[first_name] => test
[last_name] => test
[email] => myemail#me.com
[contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1
[updated_at] => 1578494555
[archived_at] =>
[is_primary] => 1
[phone] => 07919446174
[last_login] =>
[send_invoice] => 1
[custom_value1] =>
[custom_value2] =>
)
)
)
)
[meta] => stdClass Object (
[pagination] => stdClass Object (
[total] => 1
[count] => 1
[per_page] => 15
[current_page] => 1
[total_pages] => 1
[links] => Array ( )
)
)
)
This is what I've tried, but it doesn't find anything:
$person = getclient($itemid);
$person_data = json_decode($person);
foreach ($person_data->contacts as $key => $item)
{
$itemid = $item->id . "<br />";
}
$person_data contains the data property, which is an array of objects.
foreach ($person_data->data as $person) {
foreach ($person->contacts as $contact) {
echo $contact->id . '<br>';
}
}

extracting an element of an array [duplicate]

This question already has answers here:
Get index of row with qualifying value from a 2d array
(5 answers)
Closed 7 years ago.
Assume this array:
Array ( [0] => Array ( [id] => 1171 [product_id] => 140 [fileid] => 479717 [purchid] => 847 [cartid] => 833 [uniqueid] => f100c3b3a853202fb6559fbacf025a6aa07f52c7 [downloads] => 99998 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[1] => Array ( [id] => 1172 [product_id] => 140 [fileid] => 313624 [purchid] => 847 [cartid] => 833 [uniqueid] => f00a3c91378ad469f333abeec64753b275f10670 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[2] => Array ( [id] => 1173 [product_id] => 140 [fileid] => 313618 [purchid] => 847 [cartid] => 833 [uniqueid] => ac125595e2dbca6a086261434582f6e7dfc5638e [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[3] => Array ( [id] => 1174 [product_id] => 140 [fileid] => 313526 [purchid] => 847 [cartid] => 833 [uniqueid] => 3e6123e0a4453de71dec91a177f5b34217625680 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 ))
I want to "extract" the [0] array and use it for something else BUT it has to be conditioned.
I said the 0 element because it has the fileid = 479717 the one that i want.
So i`m looking at extracting the array (in my case [0]) that has filed = $myvalue. Where i can set $myvalue to whatever i want.
Does this fits your needs ?
function findById($id, $arrayOfArrays){
foreach ( $arrayOfArrays as $contents )
if ( $contents['id'] == $id ) return $contents;
}
function filter($fileid, array $array) {
foreach ($array as $key => $value) {
if ($value['fileid'] === $fileid) {
return $value;
}
}
}

PHP check array object key value without using loop/foreach

Just to learn more techniques in PHP , I'm so used in loop foreach and I want to find some other ways on how to check an array object. Let's say if we have a result something like this:
Array
(
[0] =; stdClass Object
(
[gallery_id] => 38
[artist_id] => 58
[title] => Appearances
[description] => Appearances
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 03:27:23
[update_timestamp] => 2014-08-07 15:26:24
[url] => talents/58/photos/38/Appearances
[position] =>
)
[1] => stdClass Object
(
[gallery_id] => 36
[artist_id] => 58
[title] => Endorsements
[description] => Endorsements
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 03:17:28
[update_timestamp] => 2015-01-13 15:50:09
[url] => talents/58/photos/36/Endorsements
[position] => 1
)
[2] => stdClass Object
(
[gallery_id] => 34
[artist_id] => 58
[title] => Magazine Covers
[description] => Magazine Covers
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 02:54:27
[update_timestamp] => 2015-01-13 15:50:10
[url] => talents/58/photos/34/Magazine-Covers
[position] => 2
)
)
I know we can do something like this to check a value in the object:
$myFlag = 2;
foreach($objArr as $obj){
if($obj->position == $myFlag){
return true;
}
}
But is there a way to check it without using a loop? A more efficient way, coz I feel whenever I use this(most of the time) , It's not that efficient and somehow takes time to process.
How about using array_filter with closure.
$myFlag = 2;
$target = array_filter($objArr, function($elem) use($myFlag){
return $elem->position === $myFlag;
});
print_r($target);
result is ...
Array
(
[2] => stdClass Object
(
[gallery_id] => 34
[artist_id] => 58
[title] => Magazine Covers
[description] => Magazine Covers
[photo_file] =>
[status] => 1
[insert_timestamp] => 2014-08-07 02:54:27
[update_timestamp] => 2015-01-13 15:50:10
[url] => talents/58/photos/34/magazine-covers
[position] => 2
)
)

PHP Accessing an item in an array

I have the following array:
Cart Object
(
[event_list] => Array
(
[15] => stdClass Object
(
[event_id] => 15
[event_name] => North Pole Express 2014
[event_date] => 2014-12-06
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[14] => stdClass Object
(
[event_id] => 14
[event_name] => North Pole Express 2014
[event_date] => 2014-11-30
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[13] => stdClass Object
(
[event_id] => 13
[event_name] => North Pole Express 2014
[event_date] => 2014-11-29
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
)
[cat_list] => Array
(
[138] => stdClass Object
(
[cat_id] => 138
[category_event_id] => 15
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[120] => stdClass Object
(
[cat_id] => 120
[category_event_id] => 14
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[102] => stdClass Object
(
[cat_id] => 102
[category_event_id] => 13
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
)
[disc_list] => Array
(
)
[items] => Array
(
[4] => PlaceItem Object
(
[id] => 4
[cart] => Cart Object
*RECURSION*
[event_id] => 14
[category_id] => 120
[seats] => Array
(
[26151] => stdClass Object
(
[seat_id] => 26151
[seat_row_nr] => 0
[seat_nr] => 0
[seat_ts] => 1388769219
[discount_id] => 0
)
)
[ts] =>
[created] => 2014-01-03T16:43:39+00:00
[expired] =>
)
)
[ts] => 1388769219
)
I need to be able to access the 'Items' Key.
However everything I have tried so far has failed. I suspect I am missing something really obvious.
What would be the best way to access these items?
Cart is not an array, it's an Object.
$cart = new Cart();
$cart->items; // <-- this is an array (of PlaceItems)
Since $cart is an object you need to use object notation to access it, the arrays are underneath that:
forach ($cart->items as $key=>$value) {
//...
}
Update from your comment:
$items=$_SESSION['_SMART_cart']->items;
foreach ($items as $key=>$value) {
//...
}

Filter large PHP array

This data is return from Flickr API. It contains 1000 arrays with photo details. The array is given below...
Array
(
[0] => Array
(
[id] => 8437769421
[owner] => 10817753#N03
[secret] => 9eccf2d244
[server] => 8056
[farm] => 9
[title] => [Group 1]-XR6A1835_XR6A1840-6 images-E.jpg
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
[description] => Array
(
[_content] => Stitched Panorama
)
[dateupload] => 1359826577
[datetaken] => 2012-12-24 10:32:04
[datetakengranularity] => 0
[ownername] => Ulf Bodin
[tags] => mist fog landscape spain ray rays andalusia andalusien spanien landskap salobreña dimma canoneos5dmarkiii canonef70200mmf28lisiiusm
[latitude] => 36.743055
[longitude] => -3.588651
[accuracy] => 16
[context] => 0
[place_id] => qvlfaYpWVbiHFTA
[woeid] => 772523
[geo_is_family] => 0
[geo_is_friend] => 0
[geo_is_contact] => 0
[geo_is_public] => 1
)
[1] => Array
(
[id] => 8437770275
[owner] => 69309429#N02
[secret] => 1805477eb0
[server] => 8078
[farm] => 9
[title] => #Newfoundlanddogs #squirellhatersclub #landseer #newfie #dogs #newfiesofinstagram #instadogs #drool #newfiesarethebestdogsever
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
[description] => Array
(
[_content] =>
)
[dateupload] => 1359826595
[datetaken] => 2013-02-02 09:36:35
[datetakengranularity] => 0
[ownername] => chefwaiterhater
[tags] => square squareformat normal iphoneography instagramapp uploaded:by=instagram
[latitude] => 0
[longitude] => 0
[accuracy] => 0
[context] => 0
)
.....
.....
.....
[999] => Array
(
[id] => 8438855962
[owner] => 23123736#N00
[secret] => ab5b4dbf4d
[server] => 8092
[farm] => 9
[title] => Dusky drama - drive home
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
[description] => Array
(
[_content] =>
)
[dateupload] => 1359826602
[datetaken] => 2013-02-02 17:36:42
[datetakengranularity] => 0
[ownername] => angee09
[tags] => square squareformat iphoneography instagramapp xproii uploaded:by=instagram
[latitude] => 0
[longitude] => 0
[accuracy] => 0
[context] => 0
)
);
I want to filter array only containing latitude for that I write this program...
function onlyLatLng($items)
{
$filters = array();
foreach ($items as $item) {
if (!in_array($item['latitude'], array('0', '', 'NULL'))) {
$filters[] = $item;
}
}
return $filters;
}
$latlngs = onlyLatLng($above_big_array);
Is this right way to do? or there is better way to filter the array? Thanks in advance.
You could use the array_filter function:
$latlngs = array_filter($items, function ($item) {
return !in_array($item['latitude'], array('0', '', 'NULL')));
});

Categories