Extracting Lower, Middle, and Higher Distancefrom Array - php

The goal is to extract minimum, middle and highest distance. the lowest distance was successfully achieved. however the middle and highest distance is more complex.
Array content is shown below:
Array name: $array
Array
(
[0] => Array
(
[city] => Array
(
[0] => Reduit
[1] => Curepipe
)
[distance] => 200
)
[1] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebe
)
[1] => Bees Village
[2] => Phoen Trunk Rd
[3] => Riv,Phoenix
[4] => St l Rd
[5] => Geoes Guibert St
[6] => Curepipe
)
[distance] => 151
)
[2] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Riv,Phoenix
)
[1] => St l Rd
[2] => Geoes Guibert St
[3] => Curepipe
)
[distance] =>50
)
[3] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebene
)
[1] => Belles Village
[2] => Phoenix Trunk Rd
[3] => Riverside,Phoenix
[4] => St Paul Rd
[5] => Georges Guibert St
[6] => Curepipe
)
[distance] => 101
)
)
Can someone tell me where am wrong . My workings are
$current = $array[0]['distance'];
for($middleDistance=1;$middleDistance<$total;$middleDistance++){
$next = $array[($middleDistance)]['distance'];
if ($next<$current){
$current = $next;
print_r($current);
if($current>50&&$current<100){
}
}
}

Why not sort the array by value and get the min/mid/max from that? If you truly want the closest to middle element you would have to compare the 2 middle-most elements in the case of an even number of distances. Also, I didn't make use of them here, but take note of PHPs min and max functions:
http://php.net/manual/en/function.min.php
http://php.net/manual/en/function.max.php
$arr = array(200, 151, 50, 101);
sort($arr, SORT_NUMERIC);
if (count($arr)) {
$min = current($arr);
$max = end($arr);
$mid = $arr[round(count($arr) / 2) - 1];
}

Related

Reading the response of Google Analytics php Api

I am trying to get the page views from each country.
I have following code for this purpose.
function getResults($analytics, $profileId,$url) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
$optParams = array(
'dimensions' => 'ga:country',
'filters' => 'ga:pagePath=#/605-2/'
);
return $analytics->data_ga->get(
'ga:' . $profileId,
'2017-11-01',
'today',
'ga:pageViews',
$optParams);
}
The code is working fine. The problem is I am not sure how to print the results as a table of form
Country | Views
USA | 52
Australia | 50
Below is the response I managed to filter out
Array ( [0] => Array ( [0] => (not set) [1] => 1 ) [1] => Array ( [0] => Australia [1] => 11 ) [2] => Array ( [0] => Bahrain [1] => 2 ) [3] => Array ( [0] => Belgium [1] => 1 ) [4] => Array ( [0] => Canada [1] => 5 ) [5] => Array ( [0] => Denmark [1] => 1 ) [6] => Array ( [0] => Finland [1] => 1 ) [7] => Array ( [0] => France [1] => 1 ) [8] => Array ( [0] => Germany [1] => 10 ) [9] => Array ( [0] => Hungary [1] => 1 ) [10] => Array ( [0] => Italy [1] => 1 ) [11] => Array ( [0] => Japan [1] => 4 ) [12] => Array ( [0] => Mali [1] => 1 ) [13] => Array ( [0] => Norway [1] => 1 ) [14] => Array ( [0] => Pakistan [1] => 589 ) [15] => Array ( [0] => Peru [1] => 1 ) [16] => Array ( [0] => Saudi Arabia [1] => 13 ) [17] => Array ( [0] => Singapore [1] => 1 ) [18] => Array ( [0] => South Africa [1] => 1 ) [19] => Array ( [0] => South Korea [1] => 2 ) [20] => Array ( [0] => Ukraine [1] => 1 ) [21] => Array ( [0] => United Arab Emirates [1] => 7 ) [22] => Array ( [0] => United Kingdom [1] => 7 ) [23] => Array ( [0] => United States [1] => 10 ) )

Need help on PHP array filtering

I have this array in php, i want to select all casenumber those have dob and then select all links those not have any dob. How can I do that in php
Array
(
[links] => Array
(
[0] => inquiryDetail.jis?caseId=0101SP085622015&loc=3&detailLoc=DV
[1] => inquiryDetail.jis?caseId=0101SP096462015&loc=3&detailLoc=DV
[2] => inquiryDetail.jis?caseId=050200173642014&loc=20&detailLoc=DSCIVIL
[3] => inquiryDetail.jis?caseId=CAL1432003&loc=65&detailLoc=PGV
)
[case_number] => Array
(
[0] => 0101SP085622015
[1] => 0101SP096462015
[2] => 050200173642014
[3] => CAL1432003
)
[persons] => Array
(
[0] => Walker, Rosemary
[1] => Walker, Rosemary
[2] => Walker, Rosemary
[3] => Walker, Rosemary
)
[dob] => Array
(
[0] => 11/1961
[1] => 11/1961
)
[Party_Type] => Array
(
[0] => Defendant
[1] => Defendant
[2] => Defendant
[3] => Defendant
)
[Court] => Array
(
[0] => Baltimore City District Court 1400 North Ave.
[1] => Baltimore City District Court 1400 North Ave.
[2] => Upper Marlboro District Court
[3] => Prince George\'s County Circuit Court
)
[Case_Type] => Array
(
[0] => Domestic Violence
[1] => Domestic Violence
[2] => CONT
[3] => CIVIL
)
[Case_Status] => Array
(
[0] => CLOSE
[1] => CLOSE
[2] => ACTIVE
[3] => ACTIVE
)
[Filing_Date] => Array
(
[0] => 09/04/2015
[1] => 11/25/2015
[2] => 07/24/2014
[3] => 11/18/2014
)
)
Here all are inter connected with keys to each other. please help
This solutuon works if indexes of each subarrays match. If not, you should clarify your question with associations between subarrays.
$array - your input data.
You'll get $result array which contains searched values.
$result = array(
'links_without_dob' => array(),
'case_number_with_dob' => array()
);
foreach($array['dob'] as $k => $v) {
$result['case_number_with_dob'][] = $array['case_number'][$k];
}
foreach($array['links'] as $k => $v) {
if (array_key_exists($k, $array['dob'])) continue;
$result['links_without_dob'][] = $v;
}
I'll be glad to see any suggestions for improving this solution.

How to sort an array based from another array in php?

How to sort an array based on another array in php? I have this list of persons, actually they are 1300 + people... I will only display 22 people here :)
persons array:
Array (
[0] => Array ( [name] => Kay [site] => ex [rate] => 10 )
[1] => Array ( [name] => Kat [site] => ih [rate] => 9.7 )
[2] => Array ( [name] => Kate [site] => tp [rate] => 9 )
[3] => Array ( [name] => rina [site] => tc [rate] => 9.8 )
[4] => Array ( [name] => Katay [site] => cfnm [rate] => 6.8 )
[5] => Array ( [name] => teriay [site] => sn [rate] => 7.6 )
[6] => Array ( [name] => Kaay [site] => tla [rate] => 9.7 )
[7] => Array ( [name] => na Kay [site] => bsc [rate] => 9.5 )
[8] => Array ( [name] => qwerty [site] => tdp [rate] => 9.5 )
[9] => Array ( [name] => Katey [site] => hd [rate] => 9.4 )
[10] => Array ( [name] => Kat Kay [site] => ss [rate] => 9.2 )
[11] => Array ( [name] => ina Kay [site] => pv [rate] => 9.43 )
[12] => Array ( [name] => ina [site] => rat [rate] => 9.32 )
[13] => Array ( [name] => atay [site] => trw [rate] => 9.32 )
[14] => Array ( [name] => erina [site] => tlm [rate] => 9.43 )
[15] => Array ( [name] => Ky [site] => ol [rate] => 8.34 )
[16] => Array ( [name] => ikay [site] => tb [rate] => 7.34 )
[17] => Array ( [name] => jay [site] => ta [rate] => 6.5 )
[18] => Array ( [name] => saday [site] => hfy [rate] => 4.6 )
[19] => Array ( [name] => tarans [site] => sd [rate] => 6.54 )
[20] => Array ( [name] => dastw [site] => si [rate] => 6.4 )
[21] => Array ( [name] => dyr say [site] => ex [rate] => 7.6 )
)
and there is an another array called site which i want to display them accordingly, only one person per site per loop and when the site array reached to the end then it will start to top until the lists of persons are done reading.
by site order array
Array ( [0] => Array ( [acronym] => exs [site_order] => 1 )
[1] => Array ( [acronym] => ts [site_order] => 1 )
[2] => Array ( [acronym] => ih [site_order] => 2 )
[3] => Array ( [acronym] => tp [site_order] => 3 )
[4] => Array ( [acronym] => tc [site_order] => 4 )
[5] => Array ( [acronym] => cfnm [site_order] => 5 )
[6] => Array ( [acronym] => sn [site_order] => 6 )
[7] => Array ( [acronym] => tla [site_order] => 7 )
[8] => Array ( [acronym] => bsc [site_order] => 8 )
[9] => Array ( [acronym] => tdp [site_order] => 9 )
[10] => Array ( [acronym] => lhd [site_order] => 10 )
[11] => Array ( [acronym] => ss [site_order] => 11 )
[12] => Array ( [acronym] => pov [site_order] => 12 )
[13] => Array ( [acronym] => rat [site_order] => 13 )
[14] => Array ( [acronym] => trw [site_order] => 14 )
[15] => Array ( [acronym] => tgs [site_order] => 15 )
[16] => Array ( [acronym] => tlm [site_order] => 16 )
[17] => Array ( [acronym] => ol [site_order] => 17 )
[18] => Array ( [acronym] => tb [site_order] => 18 )
[19] => Array ( [acronym] => ta [site_order] => 19 )
[20] => Array ( [acronym] => hfy [site_order] => 20 )
[21] => Array ( [acronym] => sd [site_order] => 21 )
[22] => Array ( [acronym] => si [site_order] => 22 )
[23] => Array ( [acronym] => tse [site_order] => 23 )
[24] => Array ( [acronym] => ih [site_order] => 24 )
)
I also want to display them by rate. but I cant even display a correct data because when the site array foreach is finish then it stop loading on people. this is what my code so far...
$x = 0;
$num = 0;
foreach ($site as $item) {
foreach ($data as $person)
{
if($site[$num]['acronym'] == $persons['site'] && $x != 1)
{
echo 'name: '.$person['site'].'<br>';
echo 'site: '.$person['name'].'<br>';
echo '<br>';
$x++;
}
}
$x = 0;
$num++;
}
what output i want is for example:
site: ex
name: kay
rate: 10
site: tp
name: kate
rate: 9
site: tc
name: rina
rate: 9.8
site: ih
name: kat
rate: 9.7
site: tla
name: Kaay
rate: 9.7
.
.
.
and so on
note that I want a condition that:
loop persons order by site orders and order by rate but only one person per site loop.
I know it sounds bit complicated but please help me. i searched so much thread here that has the same problem as me, but no luck. :'(
The following code is a possible solution:
$already_printed = Array();
$total_of_people = count($people);
// while there is someone that was not printed yet
while(count($already_printed) < $total_of_people){
// iterate thought each site
foreach($sites as $site){
// take each person and its position in the array
foreach ($people as $i => $person){
// if this i-th person was not printed yet
// and is related to this $site
if (! isset($already_printed[$i])
&& $person['site'] == $site['acronym']){
// print this person
echo 'site: '.$person['site'].'<br>';
echo 'name: '.$person['name'].'<br>';
echo '<br>';
// mark this i-th person as printed
$already_printed[$i] = true;
}
}
}
}
The array already_printed is used to know who was already printed to avoid double printing. When its size is equal to the total of people, then, we printed everybody, so, we can stop (this is what the outer loop does).
The key is to organize that large array into a format that doesn't force you to iterate through it more times than necessary. Give this a shot:
$highest = array();
foreach ($data as $person) {
if (!array_key_exists($person['site'], $highest) || ($person['rate'] > $highest[$person['site']]['rate'])) {
$highest[$person['site']] = $person;
}
}
foreach ($site as $item) {
if (array_key_exists($item['acronym'], $highest)) {
echo "site: {$highest[$item['acronym']]['site']}<br />name:{$highest[$item['acronym']]['name']}<br />rate:{$highest[$item['acronym']]['rate']}";
}
}
As a side note, good call on changing 'girls' to 'person' so this isn't so obviously an application created to rate women...
Using your code I would do something like this:
foreach ($site as $item) {
$maxRate = 0;
$savePerson = array();
foreach ($data as $person)
{
if($item['acronym'] == $girls['site'] && $person['site'] == $girls['site'] && $person['rate'] > $maxRate)
{
$maxRate = $person['rate'];
$savePerson = $person;
}
}
if(!empty($savePerson)){
echo 'name: '.$savePerson['site'].'<br>';
echo 'site: '.$savePerson['name'].'<br>';
echo '<br>';
}
}
I don't know exactly if you need this condition $person['site'] == $girls['site'] ..if not you can delete it. That was just my feeling.
btw: you didn't show the $girls array so I just assume is just computed previously. And $data I assume is the $persons array that you are talking about.

Replace numeric array keys with associative keys from array

I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
Existing Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[0] => 141627
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 18
[4] => 11
[5] => Final
)
[1] => Array
(
[0] => 140895
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 16
[4] => 10
[5] => Semi-Final
)
)
)
Desired Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[MATCHID] => 141627
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 18
[ROUNDSORT] => 11
[ROUNDNAME] => Final
)
[1] => Array
(
[MATCHID] => 140895
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 16
[ROUNDSORT] => 10
[ROUNDNAME] => Semi-Final
)
)
)
foreach ($data->DATA as $key => $array) {
$data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
$data is the object you showed.
Loop trough the data and combine the keys with the data, see array_combine
$data->DATA = array_map(function (array $entry) use ($data) {
return array_combine($data->COLUMNS, $entry);
}, $data->DATA);

Find Shortest distance from an array of arrays using php

I want to compare the distance from array[0] to end of array and display the shortest distance with its key.
Array
(
[0] => Array
(
[city] => Array
(
[0] => Reduit
[1] => Curepipe
)
[distance] => 14.4
)
[1] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebe
)
[1] => Bees Village
[2] => Phoen Trunk Rd
[3] => Riv,Phoenix
[4] => St l Rd
[5] => Geoes Guibert St
[6] => Curepipe
)
[distance] => 1.4
)
[2] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Riv,Phoenix
)
[1] => St l Rd
[2] => Geoes Guibert St
[3] => Curepipe
)
[distance] => 3.4
)
[3] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebene
)
[1] => Belles Village
[2] => Phoenix Trunk Rd
[3] => Riverside,Phoenix
[4] => St Paul Rd
[5] => Georges Guibert St
[6] => Curepipe
)
[distance] => 22.4
)
)
i use,
$total = count($array)-1;
$current = $array[0]['distance'];
for($loop=1;$loop<$total;$loop++){
$next = $array[$loop]['distance'];
$current = $next;
$current = $current;
if ($next>$current){
print_r($current);
}
}
}
to get the index of all key, take the first index and compare it to other value using swaping.. but it doesn't work. someone has a fix please. thanks you
If I am understanding this correctly, you should not set $current = $next unless it is in the if statement, and it also appears you have an extra } in there. I also believe you want to change your > to a < in your if statement. This way it will tell if $next is the lesser of the two.
Try this:
for($loop=1;$loop<$total;$loop++){
$next = $array[$loop]['distance'];
if ($next<$current){
$current = $next;
print_r($current);
}
}
Hopefully it helps you out!

Categories