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));
Related
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']).
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
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
)
)
)
I have this multidimensional array and I want to access a specific value without doing loop.. is it possible?
here's the array:
Array
(
[0] => stdClass Object
(
[akeebasubs_user_id] => 205
[user_id] => 268
[isbusiness] => 0
[businessname] => sci555
[occupation] =>
[vatnumber] =>
[viesregistered] => 0
[taxauthority] =>
[address1] => Ma. Cristina St.
[address2] => Negros Oriental
[city] => Dumaguete
[state] => IA
[zip] => 6200
[country] => BS
[params] => {"work_telephone":"232424","hospital_company":"sci5","company_introductory":"test","organization_type":"","applicant_url":"www","user_title":"","year_established":"","parent_company":"","r_01":"","r_02":"","r_03":"","r_04":""}
[notes] => <p>test</p>
)
)
what I want is to access the user_id which is 268 directly.
You will need to do the following:
var_dump($array[0]->user_id);
$arrayis a one-entry array that contains an stdClass object (you access an object property by using ->).
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;
?>