PHP add arrays with duplicates - php

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.

Related

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

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));

Combine Multidimentional Array in PHP with Duplicate Entries

I have an array of orders. I need to search for customers with more than 1 order and combine them.
Orders array as follows:
$orders = Array (
[0] => Array (
[address] => Array (
[name] => Random Name
[street1] => 975 Vintage Ct
[city] => Rio Vista
[state] => CA
[zip] => 94571-9780
)
[skus] => Product-1
[carrier] => USPS
[service] => First
)
[1] => Array (
[address] => Array (
[name] => Random Name
[street1] => 975 Vintage Ct
[city] => Rio Vista
[state] => CA
[zip] => 94571-9780
)
[skus] => Product-1
[carrier] => USPS
[service] => First
)
[2] => Array (
[address] => Array (
[name] => Different Name
[street1] => 6516 Northern Red Oak Dr
[city] => Mint Hill
[state] => NC
[zip] => 28227-4629
)
[skus] => Product-1
[carrier] => USPS
[service] => First
)
)
I'd like to find customers with more than 1 order and combine them like this:
$orders = Array (
[0] => Array (
[address] => Array (
[name] => Random Name
[street1] => 975 Vintage Ct
[city] => Rio Vista
[state] => CA
[zip] => 94571-9780
)
[skus] => Product-1, Product-1
[carrier] => USPS
[service] => First
)
[1] => Array (
[address] => Array (
[name] => Different Name
[street1] => 6516 Northern Red Oak Dr
[city] => Mint Hill
[state] => NC
[zip] => 28227-4629
)
[skus] => Product-1
[carrier] => USPS
[service] => First
)
)

Parsing The json decoded array in php

curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
print_r($jsonobj); //this shows me the op as:
Array (
[0] =>
stdClass Object (
[businessId] => 6
[subscriptionId] => 6
[name] => Eazy Borehole Drillers Limited
[city] => Blantyre
[pin] => 3332
[region] => Southern Region
[area] => City Centre
[address] => P.O. Box 3332 Blantyre
[email] => eazybhd#yahoo.com
[website] => eazyboreholedrillers.com
[district] => Blantyre
[phonenumber] => 265999434445
[category] => 14
)
[1] =>
stdClass Object (
[businessId] => 7
[subscriptionId] => 7
[name] => Eazy Travel Limited
[city] => Blantyre
[pin] => 3332
[region] => Southern Region
[area] => City Centre
[address] => P.O. BOX 3332 Blantyre
[email] => eazytravell#yahoo.com
[website] => eazytravell.com
[district] => Blantyre
[phonenumber] => 265999434445
[category] => 15
)
[2] =>
stdClass Object (
[businessId] => 20
[subscriptionId] => 20
[name] => Malswitch
[city] => Blantyre
[pin] => 384
[region] => Southern Region
[area] => City Centre
[address] => PO Box 384
[email] => info#malswitch.com
[website] => www.malswitch.com
[district] => Blantyre
[phonenumber] => 01 820 414
[category] => 69
)
[3] =>
stdClass Object (
[businessId] => 21
[subscriptionId] => 21
[name] => Malawi Savings Bank
[city] => Blantyre
[pin] => 521
[region] => Southern Region
[area] => Cicty Centre
[address] => PO Box 521 PO Box 521
[email] => balaka#msb.mw
[website] => www.msb.mw
[district] => Blantyre
[phonenumber] => 01 831 016 / 01
[category] => 69
)
Now my question is how to take the inside values like [businessId],[subscriptionId] out of this so that i can use them in my html page.
you use this 'array->key'. example array->businessId;
$data = Array (
[0] =>
stdClass Object (
[businessId] => 6
[subscriptionId] => 6
[name] => Eazy Borehole Drillers Limited
[city] => Blantyre
[pin] => 3332
[region] => Southern Region
[area] => City Centre
[address] => P.O. Box 3332 Blantyre
[email] => eazybhd#yahoo.com
[website] => eazyboreholedrillers.com
[district] => Blantyre
[phonenumber] => 265999434445
[category] => 14
)
)
$data[0]->businessId;
$data[0]->subscriptionId;
Your JSON object contains objects itself, which you can access with ->. Try this:
$jsonobj = ...; // Fetch your JSON object
foreach($jsonobj as $item) {
echo $item->businessId; // 6
echo $item->subscriptionId; // 6
echo $item->city; // Blantyre
echo $item->area; // City Centre
}

Merge two multidimensional associative arrays

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

Categories