Mapping three columns of sql to php arrays - php

I have this MySQL table where I have four columns: location_id, location_name, address, city
I need to make two dropdowns, where the first drop down is the city name. So, if there are 10 cities with all same name, the dropdown should only have 1 city. Upon selecting that city, corresponding location name should come to the next dropdown - location name. Upon selecting the location name dropdown, its corresponding address to be displayed.
One of my logic to do was:
Select all the distinct city from the database. With another query, select all the information.
Query:
SELECT distinct(city) from locations;
This would give me all the different city names.
SELECT * from locations;
All the required data
Make an array, where key will be the city name, and location details as an array. Something like this
Array
(
[0] => stdClass Object
(
[location_id] => 27
[location_name] => ANSND
[address] => some street 1
[city] => NYC
)
[1] => stdClass Object
(
[location_id] => 809
[location_name] => ANBC
[address] => some street 1
[city] => NYC
)
[2] => stdClass Object
(
[location_id] => 810
[location_name] => dasddsdss
[address] => some street 1
[city] => Calif
)
[3] => stdClass Object
(
[location_id] => 811
[location_name] => testing 6 feb
[address] => some street 1
[city] => Calif
)
)
How can I do this in PHP?
Array
(
[NYC] => Array
(
[0] => stdClass Object
(
[location_id] => 27
[location_name] => ANSND
[city] => NYC
[address] => some street 1
)
[1] => stdClass Object
(
[location_id] => 809
[location_name] => ANBC
[city] => NYC
[address] => fsff
)
)
[Calif] => Array
(
[0] => stdClass Object
(
[location_id] => 810
[location_name] => dasddsdss
[city] => Calif
[address] => some street 1
)
[1] => stdClass Object
(
[location_id] => 811
[location_name] => testing 6 feb
[city] => Calif
[address] => some street 1
)
)
)

<?php
$results = array(); // it will be your resulted array
$data_locations = array(); // lets say it is your data list of location table
foreach( $data_locations as $location )
{
$results[$location->city][] = array(
'location_name' =>$location->location_name;
'address' =>$location->address;
)
}
echo "<pre>"; print_r( $results ); die;
?>

Related

Extract Data from Multidem array

I have been trying to extract the following data for each student from a large multidimensional array (Shown further down)
Data -> gender,
Data -> grade,
Data -> name -> first,
Data -> name -> last,
Data -> name -> middle,
Data -> student_number,
Data -> ID
I have tried various options through searching including Slice, Splice, and a for loop.
Every attempt has been met with failure on all or some of the data. I have never been able to get to the 3rd nested data of First, Middle, and Last name.
How can I take a large multidem array like this, and extract the data listed above in a foreach loop so I can import it into a database? I feel like it is alot simpler than I am making it. I have not included any code as I have yet to have anything that seems remotely useful.
Here is a sample array below. Thanks!
Array
(
[data] => Array
(
[0] => Array
(
[data] => Array
(
[gender] => M
[dob] => 7/17/2008
[email] =>
[grade] => 2
[schools] => Array
(
[0] =>12345
)
[school] => 12345
[created] => 2018-04-16T14:01:00.437Z
[name] => Array
(
[first] => Jacob
[last] => Smith
[middle] => Rabbitboom
)
[location] => Array
(
[zip] =>
[address] =>
[city] =>
[lat] =>
[lon] =>
[state] =>
)
[district] => 123456
[last_modified] => 2018-04-16T14:01:00.437Z
[race] =>
[hispanic_ethnicity] =>
[graduation_year] =>
[student_number] => 1234567
[credentials] => Array
(
[district_username] =>
)
[id] => 123456
)
[uri] =>
)
[1] => Array
(
[data] => Array
(
[gender] => F
[dob] => 7/17/2008
[email] =>
[grade] => 2
[schools] => Array
(
[0] =>12346
)
[school] => 12345
[created] => 2018-04-16T14:01:00.437Z
[name] => Array
(
[first] => Jason
[last] => Smith
[middle] => RobesPerrie
)
[location] => Array
(
[zip] =>
[address] =>
[city] =>
[lat] =>
[lon] =>
[state] =>
)
[district] => 123456
[last_modified] => 2018-04-16T14:01:00.437Z
[race] =>
[hispanic_ethnicity] =>
[graduation_year] =>
[student_number] => 1234568
[credentials] => Array
(
[district_username] =>
)
[id] => 123459
)
[uri] =>
)
The easiest approach might be to extract the nested data arrays and loop that:
foreach(array_column($array['data'], 'data') as $data) {
echo $data['gender'];
echo $data['name']['first'];
}
If schools is variable length then you'll need to loop that or implode(', ', $data['schools']).

Assign value from array as key and merge records of same key

I have array like below in PHP (Codeigniter). I want to group them by using country names. So value of country key will become key of outer array
Array
(
[0] => Array
(
[city] => Buenos Aires
[country] => Argentina
)
[1] => Array
(
[city] => Adelaide
[country] => Australia
)
[2] => Array
(
[city] => Brisbane
[country] => Australia
)
[3] => Array
(
[city] => Fremantle
[country] => Australia
)
[4] => Array
(
[city] => Melbourne
[country] => Australia
)
[5] => Array
(
[city] => Sydney
[country] => Australia
)
)
I want to convert it into array like below so country will become key with city list as per country name
Array
(
[Argentina] => Array
(
[0] => Buenos Aires
)
[Australia] => Array
(
[0] => Adelaide
[1] => Brisbane
[2] => Melbourne
[3] => Sydney
)
)
Here is the code,
$arr = [//your arr];
$result = []; // will be your output
foreach($arr as $k => $v){
$result[$v['country']][] = $v['city'];
}
I hope this will work

How to get multi level array value in PHP decoded from JSON

I am facing a problem while printing an array in PHP. I am getting data from facebook API for user events and getting the result in JSON.
I just want to get each location (City,Country). Is there any method to get them easily?
I have tried:
$myjson=json_decode($_POST['Data'],true);
echo "<pre>";
print_r($myjson["data"]);
echo "<pre>";
echo count($myjson["data"]);
echo "<pre>";
for($i =0; $i<count($myjson["data"]); $i++){
print($myjson["data"][$i]["name"]);
echo "<br />";
}
When I decode the data and use print_r:
echo "<pre>";
print_r($myjson["data"]);
I get the following result:
Array (
[0] => Array
(
[description] => this is test
[name] => test event Web Developer
[place] => Array
(
[name] => Lahore
[location] => Array
(
[city] => Lahore
[country] => Pakistan
[latitude] => 31.5497
[longitude] => 74.3436
)
[id] => 108104849224069
)
[start_time] => 2015-10-21T19:00:00+0500
[id] => 1503962379902466
[rsvp_status] => attending
)
[1] => Array
(
[description] => (Full description omitted for brevity)
[name] => Peaceful Pakistan Online Competition!
[place] => Array
(
[name] => Punjab Information Technology Board (PITB)
[location] => Array
(
[city] => Lahore
[country] => Pakistan
[latitude] => 31.5497
[longitude] => 74.3436
[zip] => 54600
)
[id] => 340860042630070
)
[start_time] => 2015-08-10T00:00:00+0500
[id] => 907352572664461
[rsvp_status] => attending
)
[2] => Array
(
[description] => (Full description omitted for brevity)
[end_time] => 2015-06-17T19:30:00+0500
[name] => Open House 2015
[place] => Array
(
[name] => 6th Floor, Arfa Software Technology Park, 346-B, Ferozepur Road, Lahore, Pakistan-54000
)
[start_time] => 2015-06-17T16:30:00+0500
[id] => 1619010958352625
[rsvp_status] => attending
)
[3] => Array
(
[description] => A Two Day Workshop on Game Design and Development on Unity 3d
[end_time] => 2015-04-26T16:00:00+0500
[name] => Game Design And Development Workshop
[place] => Array
(
[name] => House #75, Sarfaraz Rafique Road, Opposite P.A.F Sports Stadium, Lahore Cantt
)
[start_time] => 2015-04-26T10:00:00+0500
[id] => 1651929135026796
[rsvp_status] => attending
)
[4] => Array
(
[description] => (Full description omitted for brevity)
[name] => Live Chat with Experts: How Does Pakistan's Load Shedding Affect You?
[place] => Array
(
[name] => Islamabad, Pakistan
)
[start_time] => 2015-03-26T11:00:00-0400
[id] => 347677685418059
[rsvp_status] => attending
)
[5] => Array
(
[description] => (Full description omitted for brevity)
[name] => 6th September – Defense Day of Pakistan
[place] => Array
(
[name] => Virtual Event
)
[start_time] => 2013-09-06
[id] => 434438063339987
[rsvp_status] => attending
)
)
You have an array of objects, essentially. Since you're force decoding the JSON into an array you have an array of arrays.
Getting the city/country from each object in your example above is pretty simple.
foreach($myjson['data'] as $arr) {
echo $arr['place']['location']['city'], ', ', $arr['place']['location']['country'], "\n";
}
The keys mapping to place => location provide an array with the city and country keys you're looking for. So $myjson['data'][$i][place']['location']['city'] would give you the city and $myjson['data'][$i][place']['location']['country'] would give you the country, but you can iterate over $myjson['data'] with foreach more conveniently than using a for loop such as in your example above.
The first element would give you Lahore, Pakistan, for example.
foreach($myjson["data"] as $data){
echo $data['place']['location']['city'];
echo $data['place']['location']['country'];
}

Find interception of two arrays and then merge the two

I have a large array of hostnames and their corresponding location.
Array ( [ABC01] => Array ( [hostid] => 12345
[lat] => 123
[lon] => 123
[adr] => 126 Foo Street
[city] => Rocky Hill
[state] => Connecticut
[country] => USA )
[ABC02] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => Boston
[state] => Massachusetts
[country] => USA )
[ABC03] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => New York City
[state] => New York
[country] => USA )
.....)
I am comparing it to a much smaller array - same host name but it links to the IP address:
Array ( [ABC01] => Array ( [ip] => 192.168.2.1 )
[ABC02] => Array ( [ip] => 192.168.2.2 )
[ABC03] => Array ( [ip] => 192.168.2.3 )
)
I want to create the following array:
[ABC02] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => Boston
[state] => Massachusetts
[country] => USA
[ip] => 192.168.2.1)
I'm trying to find the intersection of the arrays and then merge the two (find the same key and then merge). I've tried various functions but the end result is never what I want.
I managed to find a function here to merge the common keys, but it returned the IP in an array in the array containing the location fields in another array, as seen below:
Array ( [ABC02] => Array ( [0] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => Boston
[state] => Massachusetts
[country] => USA
[1] => Array ( [ip] => 192.168.2.1) ) )
Is there an easier way to do this?
Pretty easy with a built-in PHP function:
$result = array_merge_recursive($large, $small);
Based on your comment to get only values that are common and then merge. This assumes all keys in $small are in $large:
$result = array_merge_recursive(array_intersect_key($large, $small), $small);
To not make the assumption, if there may be keys in either one that aren't in the other then:
$result = array_merge_recursive(array_intersect_key($large, $small),
array_intersect_key($small, $large));

Sorting an array by multiple values in PHP and inserting into HTML table

I'm trying to sort the contens of an array and then enter it into a HTML table. The array needs to be sorted based on the number of goals, and if two teams have the one with the most players needs to be placed higher. This is a sample of the array:
Array
(
[0] => Array
(
[id] => 1
[team] => Manchester United
[goals] => 210
[country] => england
[players] => 10
)
[1] => Array
(
[id] => 2
[team] => Manchester City
[goals] => 108
[country] => england
[players] => 12
)
[2] => Array
(
[id] => 3
[team] => Liverpool
[goals] => 108
[country] => england
[players] => 15
)
)
I've never done sorting in PHP and I only have some experience with arrays, they weren't nested like this.
All you need is usort
usort($data, function ($a, $b) {
if ($a['goals'] == $b['goals'])
return $a['players'] > $b['players'] ? - 1 : 1;
return $a['goals'] > $b['goals'] ? -1 : 1;
});
print_r($data);
Output
Array
(
[0] => Array
(
[id] => 1
[team] => Manchester United
[goals] => 210 <--- Highest goal top
[country] => england
[players] => 10
)
[1] => Array
(
[id] => 3
[team] => Liverpool
[goals] => 108 <--- Same Score found
[country] => england
[players] => 15 Use This ---|
)
[2] => Array
(
[id] => 2
[team] => Manchester City
[goals] => 108 <--- Same Score found
[country] => england
[players] => 12 Use This ---|
)
)
See Live Demo

Categories