Find Shortest distance from an array of arrays using php - 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!

Related

How to insert a new values pair in an associative array in PHP?

I've an associative array. I am attempting to create each sub array within a loop and insert it into a multidimensional array. Google suggested using array_merge, but after using 'print_r' on the multidimensional array with the code below, only the last sub-array is being displayed.
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
And Another Array like this :
[rating] => Array
(
[0] => 7
[1] => 5
[2] =>
)
How to get merge this array value into previous array values like
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 7
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 5
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
I tried lot of tricks but couldn't get the desired array format. Can any one help me in this to get the desired array? Thanks in advance.
$arr = array(); //filled with data
$rating = array(); //filled with rates
foreach ( $rating as $key =>$rate ) {
$arr['data'][ $key ]['rating'] = $rate;
}
<?php
$arr=Array(
'data'=>Array(
0=>Array('candidate'=>Array()),
1=>Array('candidate'=>Array())
)
);
$rating=Array(7,5);
foreach($rating as $key=>$val){
$arr['data'][$key]['rating']=$val;
}
print_r($arr);
Returns:
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
)
[rating] => 7
)
[1] => Array
(
[candidate] => Array
(
)
[rating] => 5
)
)
)

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.

Replacing values in array from another array

I have this array structure:
stdClass Object
(
[carrierFsCode] => VX
[flightNumber] => 925
[departureAirportFsCode] => LAX
[arrivalAirportFsCode] => SFO
[stops] => 0
[departureTerminal] => 3
[arrivalTerminal] => 2
[departureTime] => 2014-04-28T07:00:00.000
[arrivalTime] => 2014-04-28T08:20:00.000
[flightEquipmentIataCode] => 32S
[isCodeshare] =>
[isWetlease] =>
[serviceType] => J
[serviceClasses] => Array
(
[0] => F
[1] => J
[2] => Y
)
[trafficRestrictions] => Array
(
)
[codeshares] => Array
(
[0] => stdClass Object
(
[carrierFsCode] => SQ
[flightNumber] => 1407
[serviceType] => J
[serviceClasses] => Array
(
[0] => R
[1] => F
[2] => J
[3] => Y
)
[trafficRestrictions] => Array
(
[0] => Q
)
[referenceCode] => 10594616
)
)
[referenceCode] => 979-1740743--
)
stdClass Object
(
[carrierFsCode] => SQ
[flightNumber] => 1407
[departureAirportFsCode] => LAX
[arrivalAirportFsCode] => SFO
[stops] => 0
[departureTerminal] => 3
[arrivalTerminal] => 2
[departureTime] => 2014-04-28T07:00:00.000
[arrivalTime] => 2014-04-28T08:20:00.000
[flightEquipmentIataCode] => 32S
[isCodeshare] => 1
[isWetlease] =>
[serviceType] => J
[serviceClasses] => Array
(
[0] => R
[1] => F
[2] => J
[3] => Y
)
[trafficRestrictions] => Array
(
[0] => Q
)
[operator] => stdClass Object
(
[carrierFsCode] => VX
[flightNumber] => 925
[serviceType] => J
[serviceClasses] => Array
(
[0] => F
[1] => J
[2] => Y
)
[trafficRestrictions] => Array
(
)
)
[codeshares] => Array
(
)
[referenceCode] => 979-1740743--10594616
)
And this array structure:
Array
(
[0] => stdClass Object
(
[fs] => SQ
[iata] => SQ
[icao] => SIA
[name] => Singapore Airlines
[active] => 1
)
[1] => stdClass Object
(
[fs] => VX
[iata] => VX
[icao] => VRD
[name] => Virgin America
[active] => 1
)
)
Basically what I want to do is take the first array and find its matching IATA/FS code in the second array and replace it with the ICAO code. So for example, with the first array, I want to replace the VX with VRD. I want to be able to apply the same concept to other airline/routes, too...not just VX.
If it helps, I'm getting this information from a JSON return: http://pastebin.com/2w0kQQ26
I looked into array_replace(), but because my PHP skills are almost nothing, I didn't know how to continue.
If someone can point me to the right direction, I'd appreciate it.
With array1 being your first array and array2 being the second array you described.
$comp_arr = array()
foreach ($array2 as $arr) {
$comp_arr[$arr[fs]] = $arr[icao];
}
foreach($array1 as $key => $arr){
if(array_key_exist($arr[carrierFsCode], $comp_arr){
$array1[$key][carrierFsCode] = $comp_arr[$arr[carrierFsCode]];
}
}

Extracting Lower, Middle, and Higher Distancefrom Array

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];
}

Categories