Merge two multidimensional associative arrays - php

I have these two arrays that I would like to merge together into 1 array but been having a hard time figuring out. See below for the two snippets of code:
First array:
$propSelectMaxRow = max(array_keys($property_select_email));
$i = 0;
foreach($property_queue as $r) {
if ($select_type == 3 || $select_type == 4 || $select_type == 5) {
$i = $i + 1;
$property_select_email[$propSelectMaxRow + $i] = $r;
}
}
Second array:
$propSelectMaxRow = max(array_keys($property_select_email));
$i = 0;
foreach($property_select as $r) {
if ($select_type == 1 || $select_type == 2 || $select_type == 4 || $select_type == 5) {
$i = $i + 1;
$property_select_email[$propSelectMaxRow + $i] = $r;
}
}
edit:
I would like the final array to look like this:
first array format:
Array (
[1] => Array ( [code] => PPJ3 [street] => 34412 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
[2] => Array ( [code] => PLK3 [street] => 73517 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
[3] => Array ( [code] => HYK2 [street] => 55224 Fake Street [city] => Detroit [state] => MI [zip] => 48208 )
)
Second array format:
Array (
[1] => Array ( [code] => JAK932 [street] => 353242 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
[2] => Array ( [code] => JA232 [street] => 7432 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
)
Final array format should be:
Array (
[1] => Array ( [code] => PPJ3 [street] => 34412 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
[2] => Array ( [code] => PLK3 [street] => 73517 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
[3] => Array ( [code] => HYK2 [street] => 55224 Fake Street [city] => Detroit [state] => MI [zip] => 48208 )
[4] => Array ( [code] => JAK932 [street] => 353242 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
[5] => Array ( [code] => JA232 [street] => 7432 Fake Street [city] => Detroit [state] => MI [zip] => 48223 )
)

You may need to improve your code like this:
//$propSelectMaxRow = max(array_keys($property_select_email)); // no need
//$i = 0; //no need
foreach($property_queue as $r) {
if ($select_type == 3 || $select_type == 4 || $select_type == 5) {
//$i = $i + 1;
$property_select_email[] = $r;
}
}
Similarly, build your second array. The second loop will be append array at the end of first (hence they will be auto merged).

Looks like you just need to use array_merge

Related

how can I remove 0 index key from my result in php?

This is my response:
Array
(
[0] => Array
(
[1] => Array
(
[aid] => 1
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
)
)
I want response like:
Array
(
[1] => Array
(
[aid] => 1
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
)
I want to remove 0 index from my response. I want my response like what I define below. so how can I do this with functions and etc..
It sounds like you are trying to remove an extra layer from a multi-dimentional array.
An easy way would be something like:
$new_array = $old_array[0];
Edit:
Per #Gert B's suggestion:
To return the first element regardless of the key:
$new_array = reset($old_array);
The reset function returns the pointer to the beginning of the array, and more importantly for this question, returns the first element of the array.
$result = $old_array[0];
This was the easiest way ...
Try this,
$data = 'YOUR_DATA_ARRAY';
$result = [];
foreach ($data as $category => $attributes) {
foreach ($attributes as $attribute => $options) {
foreach ($options as $option) {
$result[] = array('category' => $category, 'attribute' => $attribute, 'option' => $option);
}
}
}
I use array_replace_recursive() function and get response I want.
now my response is :
Array
(
[1] => Array
(
[aid] => 1
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
[2] => Array
(
[aid] => 2
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
)

Using array_chunk on multi array

I have a multi dimensional array and i am now sure how to use array chunk on my array key request while preserving the information into the new array. I would like to split the array every 2 arrays. I tried using array_chunk inside of a loop but no luck.
Here is my array.
[0] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
(
[0] => stdClass Object
(
[id] => 46
[client_id] => 9873
[city] => COOLIDGE
[state] => AZ
[zip] => 85228
)
[1] => stdClass Object
(
[id] => 49
[client_id] => 14965
[city] => CHANDLER
[state] => AZ
[zip] => 85226
)
[2] => stdClass Object
(
[id] => 55
[client_id] => 10120
[city] => PHX
[state] => AZ
[zip] => 85008
)
[3] => stdClass Object
(
[id] => 59
[client_id] => 11229
[city] => BUCKEYE
[state] => AZ
[zip] => 85326
)
[4] => stdClass Object
(
[id] => 69
[client_id] => 13769
[city] => PHOENIX
[state] => AZ
[zip] => 85035
)
[5] => stdClass Object
(
[id] => 175
[client_id] => 16437
[city] => Phx
[state] => Az
[zip] => 85029
)
[6] => stdClass Object
(
[id] => 195
[client_id] => 16457
[city] => Apache Junction
[state] => Az
[zip] => 85120
)
[7] => stdClass Object
(
[id] => 197
[client_id] => 16459
[city] => Mesa
[state] => Az
[zip] => 85204
)
)
)
This is the array I would like.
[0] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
(
[0] => stdClass Object
(
[id] => 46
[client_id] => 9873
[city] => COOLIDGE
[state] => AZ
[zip] => 85228
)
[1] => stdClass Object
(
[id] => 49
[client_id] => 14965
[city] => CHANDLER
[state] => AZ
[zip] => 85226
)
)
[1] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
[0] => stdClass Object
(
[id] => 55
[client_id] => 10120
[city] => PHX
[state] => AZ
[zip] => 85008
)
[1] => stdClass Object
(
[id] => 59
[client_id] => 11229
[city] => BUCKEYE
[state] => AZ
[zip] => 85326
)
)
[2] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
[0] => stdClass Object
(
[id] => 69
[client_id] => 13769
[city] => PHOENIX
[state] => AZ
[zip] => 85035
)
[1] => stdClass Object
(
[id] => 175
[client_id] => 16437
[city] => Phx
[state] => Az
[zip] => 85029
)
)
)
This is my code.
$drivers = [];
foreach($recs as $val => $rec) {
$drivers[$rec->driver_id]['first_name'] = $rec->first_name;
$drivers[$rec->driver_id]['patient_first_name'] = $rec->patient_first_name;
$drivers[$rec->driver_id]['trip_date'] = $rec->trip_date;
$drivers[$rec->driver_id]['request'][] = $rec;
}
foreach($drivers as $val => $driver) {
$drivers = array_chunk($driver['request'], 2);
}
Any suggestions?
Using array-chunk if what you need. Check the following example (I remove some of the data to simplify):
$request = array(["id" => 46], ["id" => 49], ["id" => 55], ["id" => 59], ["id" => 69], ["id" => 175], ["id" => 195], ["id" => 197]);
$arr[] = array("first_name" => "Richard", "request" => $request);
foreach($arr as $driver) {
$requests = array_chunk($driver['request'], 2);
foreach($requests as $chunck) {
$ans[] = array("id" => $driver["first_name"], "request" => $chunck); // here you can add all the other data you need from the "driver" object
}
}
Now , $ans will have your desire output
Get 'request' from the source array, chunk it and add rest items to each element of the result array
$res = array_chunk($recs['request'], 2);
unset($recs['request']);
foreach($res as &$x) {
$x += $recs;
}

PHP add arrays with duplicates

Right now, I have a way of merging two associative array of associative arrays with the duplicate values removed. Here is the code:
//$mergedProp will output the final merged arrays
$mergedProp = array(array());
$prop1 = Array (
[1] => Array ( [property_code] => CODE-01 [street] => 100 Fake Street
[city] => Las Vegas [state] => NV [zip] => (no results)
[county] => (no results) [id] => 6 )
[2] => Array ( [property_code] => CODE-02 [street] => 200 Fake St
[city] => Fake City [state] => AR [zip] => 45532
[county] => Union [id] => 3 ) ) ;
$prop2 = Array (
[1] => Array ( [property_code] => CODE-03 [street] => 1140 Fake Street
[city] => Detroit [state] => MI [zip] => 45224
[county] => Hamilton [id] => 3 )
[2] => Array ( [property_code] => CODE-02 [street] => 200 Fake St
[city] => Fake City [state] => AR [zip] => 45532
[county] => Union [id] => 12 ) )
$arrayMerged = array_merge($prop1, $prop2);
$arrayUnique = array();
foreach ($arrayMerged as $row) {
$code = $row['property_code'];
if (!isset($arrayUnique[$code])) {
$arrayUnique[$code] = array();
}
$arrayUnique[$code] = array_merge($arrayUnique[$code], $row);
}
//Sorts the array
ksort($arrayUnique);
$i = 1;
$newGrid = array();
foreach($arrayUnique as $r)
$newGrid[$i++] = $r;
$countDuplicates = count($arrayMerged) - count($newGrid);
$mergedProp = $newGrid;
Output for above:
Array (
[1] => Array ([property_code] => CODE-01 [street] => 100 Fake Street
[city] => Las Vegas [state] => NV [zip] => (no results)
[county] => (no results) [id] => 6 )
[2] => Array ([property_code] => CODE-03 [street] => 1140 Fake Street
[city] => Detroit [state] => MI [zip] => 45224
[county] => Hamilton [id] => 3 )
[3] => Array ([property_code] => CODE-02 [street] => 200 Fake St
[city] => Fake City [state] => AR [zip] => 45532
[county] => Union [id] => 12 ) )
I want the expected output to be below instead:
Array (
[1] => Array ([property_code] => CODE-01 [street] => 100 Fake Street
[city] => Las Vegas [state] => NV [zip] => (no results)
[county] => (no results) [id] => 6 )
[2] => Array ([[property_code] => CODE-02 [street] => 200 Fake St
[city] => Fake City [state] => AR [zip] => 45532
[county] => Union [id] => 3 )
[3] => Array ([property_code] => CODE-03 [street] => 1140 Fake Street
[city] => Detroit [state] => MI [zip] => 45224
[county] => Hamilton [id] => 3 )
[4] => Array ([property_code] => CODE-02 [street] => 200 Fake St
[city] => Fake City [state] => AR [zip] => 45532
[county] => Union [id] => 12 ) )
How can I get this to keep the duplicate values instead? I basically want the output to include both $prop1 and $prop2 in 1 array.
Maybe the PHP function array_intersect_assoc() is of help - it will give you the difference of the arrays.
http://php.net/manual/en/function.array-intersect-assoc.php
Fo what you've shown you just merge them as you have:
$arrayMerged = array_merge($prop1, $prop2);
And that's it.

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

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