Parsing The json decoded array in php - 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
}
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; }
PHP - Looping through indexed JSON array
I have parsed and looped through JSON plenty of times. I have seemed to hit a wall with something I have not done before and I have been unable to find an answer and hopefully I am referencing this correctly. I have been really struggling with this. With the code example below I can parse it and assign a varible to an object and get its value like so. $result = json_decode($json, true); print_r($result); echo $result['response'][0]['report']['type']; This is where the problem comes in and it starts with the array index and I am hoping I am referring to this correctly. Basically it is the [0] from above. I have never looped through something like that before and my research has come up empty. Normally I would do it like this. $json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}'; $result = json_decode($json, true); print_r($result); foreach($result as report) { $type = $report['report']['type']; echo $type; } That would usually work in most cases but since this has the [0] and increases with each new entry I am not sure how to loop through that as my entry point is different that what I have dealt with before and have been unable to find any working examples for something like this. Not to mention I am not sure how to working ['response'] into the loop ether with those index keys. Here is a decode example of the array and what I am working with. Array ( [success] => 1 [error] => [response] => Array ( [0] => Array ( [id] => 59c30487db6be86b7f8b465a [loc] => Array ( [long] => -90.45 [lat] => 43.43 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Gillingham [detail] => Array ( [text] => 1 [rainIN] => 1 [rainMM] => 25.4 ) [reporter] => co-op observer [comments] => [timestamp] => 1505952240 [cat] => rain [dateTimeISO] => 2017-09-20T19:04:00-05:00 [datetime] => 2017-09-20T19:04:00-05:00 [wfo] => arx ) [place] => Array ( [name] => gillingham [state] => wi [county] => richland [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [1] => Array ( [id] => 59c302a9db6be82a758b46e8 [loc] => Array ( [long] => -90.93 [lat] => 43.32 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Mount Sterling [detail] => Array ( [text] => 2.25 [rainIN] => 2.25 [rainMM] => 57.15 ) [reporter] => trained spotter [comments] => [timestamp] => 1505951760 [cat] => rain [dateTimeISO] => 2017-09-20T18:56:00-05:00 [datetime] => 2017-09-20T18:56:00-05:00 [wfo] => arx ) [place] => Array ( [name] => mount sterling [state] => wi [county] => crawford [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [2] => Array ( [id] => 59c30106db6be8146c8b4661 [loc] => Array ( [long] => -89.53 [lat] => 44.45 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Plover [detail] => Array ( [text] => 1.05 [rainIN] => 1.05 [rainMM] => 26.67 ) [reporter] => co-op observer [comments] => One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail. [timestamp] => 1505950800 [cat] => rain [dateTimeISO] => 2017-09-20T18:40:00-05:00 [datetime] => 2017-09-20T18:40:00-05:00 [wfo] => grb ) [place] => Array ( [name] => plover [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [3] => Array ( [id] => 59c3080adb6be8d5138b4654 [loc] => Array ( [long] => -89.33 [lat] => 44.37 ) [report] => Array ( [code] => H [type] => hail [name] => 4 mi NNW Blaine [detail] => Array ( [text] => 0.88 [hailIN] => 0.88 [hailMM] => 22.35 ) [reporter] => trained spotter [comments] => Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours. [timestamp] => 1505950680 [cat] => hail [dateTimeISO] => 2017-09-20T18:38:00-05:00 [datetime] => 2017-09-20T18:38:00-05:00 [wfo] => grb ) [place] => Array ( [name] => blaine [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [4] => Array ( [id] => 59c30106db6be8146c8b4660 [loc] => Array ( [long] => -90.93 [lat] => 43.32 ) [report] => Array ( [code] => H [type] => hail [name] => Mount Sterling [detail] => Array ( [text] => 0.75 [hailIN] => 0.75 [hailMM] => 19.05 ) [reporter] => trained spotter [comments] => Hail ranged from 1/2 to 3/4 inch. [timestamp] => 1505950200 [cat] => hail [dateTimeISO] => 2017-09-20T18:30:00-05:00 [datetime] => 2017-09-20T18:30:00-05:00 [wfo] => arx ) [place] => Array ( [name] => mount sterling [state] => wi [county] => crawford [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [5] => Array ( [id] => 59c2fd80db6be844598b466d [loc] => Array ( [long] => -89.77 [lat] => 44.21 ) [report] => Array ( [code] => R [type] => heavy rain [name] => 6 mi ESE New Rome [detail] => Array ( [text] => 4 [rainIN] => 4 [rainMM] => 101.6 ) [reporter] => trained spotter [comments] => [timestamp] => 1505949840 [cat] => rain [dateTimeISO] => 2017-09-20T18:24:00-05:00 [datetime] => 2017-09-20T18:24:00-05:00 [wfo] => arx ) [place] => Array ( [name] => rome [state] => wi [county] => adams [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [6] => Array ( [id] => 59c2f678db6be898338b4673 [loc] => Array ( [long] => -89.3 [lat] => 44.46 ) [report] => Array ( [code] => H [type] => hail [name] => Amherst Junction [detail] => Array ( [text] => 1 [hailIN] => 1 [hailMM] => 25.4 ) [reporter] => trained spotter [comments] => [timestamp] => 1505948340 [cat] => hail [dateTimeISO] => 2017-09-20T17:59:00-05:00 [datetime] => 2017-09-20T17:59:00-05:00 [wfo] => grb ) [place] => Array ( [name] => amherst junction [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [7] => Array ( [id] => 59c2f2f6db6be8a2208b4672 [loc] => Array ( [long] => -89.4 [lat] => 44.26 ) [report] => Array ( [code] => H [type] => hail [name] => Almond [detail] => Array ( [text] => 2 [hailIN] => 2 [hailMM] => 50.8 ) [reporter] => trained spotter [comments] => Report via social media [timestamp] => 1505948160 [cat] => hail [dateTimeISO] => 2017-09-20T17:56:00-05:00 [datetime] => 2017-09-20T17:56:00-05:00 [wfo] => grb ) [place] => Array ( [name] => almond [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) ) ) How would I loop through this with the Index key [#] and if I am referring to this incorrectly feel free to correct me.
You can use loop in $result["response"] as: $json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}'; $result = json_decode($json, true); foreach($result["response"] as $report) { $type = $report['report']['type']; echo $type; } Check the result here: http://sandbox.onlinephpfunctions.com/code/c7b55a67e649bdc8c35ec1ba8218747de1f8ac16
PHP- How do I reduce array based on a key's value
I have an array like this: Array ( [0] => Array ( [CommodityID] => 10 [RetailerID] => 1798 [Name] => Petrol [City] => Parow [Line1] => 49 Fifth Avenue [Line2] => [Line3] => [ContractorName] => BP Fifth Ave Motors Parow [AreaCode] => 021 [Number] => 9305025 [Latitude] => -33.896493 [Longitude] => 18.579302 [LogoUrl] => https://webservices.test.com/ContractorLogos/bp.png [dist] => 0.00 ) [1] => Array ( [CommodityID] => 10 [RetailerID] => 1798 [Name] => Petrol [City] => Parow [Line1] => 49 Fifth Avenue [Line2] => [Line3] => [ContractorName] => BP Fifth Ave Motors Parow [AreaCode] => 021 [Number] => 9305025 [Latitude] => -33.896493 [Longitude] => 18.579302 [LogoUrl] => https://webservices.test.com/ContractorLogos/bp.png [dist] => 0.00 ) [2] => Array ( [CommodityID] => 22 [RetailerID] => 1758 [Name] => Jewellers [City] => Parow [Line1] => Shop 4 [Line2] => Mc Intyre Square [Line3] => Mc Intyre Road [ContractorName] => Du Plessis Manufacturing Jewellers Parow [AreaCode] => 021 [Number] => 9300788 [Latitude] => -33.895200 [Longitude] => 18.586710 [LogoUrl] => https://webservices.test.com/ContractorLogos/DuPlessisJewellers.png [dist] => 0.01 ) ) I simply want to have only unique [RetailerID] i.e: Array ( [0] => Array ( [CommodityID] => 10 [RetailerID] => 1798 [Name] => Petrol [City] => Parow [Line1] => 49 Fifth Avenue [Line2] => [Line3] => [ContractorName] => BP Fifth Ave Motors Parow [AreaCode] => 021 [Number] => 9305025 [Latitude] => -33.896493 [Longitude] => 18.579302 [LogoUrl] => https://webservices.test.com/ContractorLogos/bp.png [dist] => 0.00 ) [1] => Array ( [CommodityID] => 22 [RetailerID] => 1758 [Name] => Jewellers [City] => Parow [Line1] => Shop 4 [Line2] => Mc Intyre Square [Line3] => Mc Intyre Road [ContractorName] => Du Plessis Manufacturing Jewellers Parow [AreaCode] => 021 [Number] => 9300788 [Latitude] => -33.895200 [Longitude] => 18.586710 [LogoUrl] => https://webservices.test.com/ContractorLogos/DuPlessisJewellers.png [dist] => 0.01 ) ) I have tried array_unique, but that seems to make it unique based on all columns. Is there a php built-in function that can do this by looking at just one array key's value?
would iterate the array once and save only unique values by that field once, for example: $newArray = array(); foreach($array as $item) { $newArray[$item['RetailerID']] = $item; } this will always save only the last item of the same kind. you can modify the logic to match your needs. another, more safe possibility would be saving the unique values and checking them: $exists = array(); $newArray = array(); foreach($array as $item) { if(!in_array($item['RetailerID'],$exists)){ $newArray[$item['RetailerID']] = $item; $exists[] = $item['RetailerID']; } }
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 ) )
php sort array by amount of items?
I have a multi-dimension array of australian states, and inside each state there are items. I was wondering if it were possible to reorder an array so the states with the most amount of items are displayed first? Here's my array, any help would be appreciated :) [ACT] => Array ( [0] => stdClass Object ( [id] => 14 [name] => Burning Log Megastore [state] => ACT [classid] => 1 ) [1] => stdClass Object ( [id] => 21 [name] => Fyshwick Home & Heating [state] => ACT [classid] => 1 ) ) [NSW] => Array ( [0] => stdClass Object ( [id] => 36 [name] => A1 Hot Wood [state] => NSW [classid] => 11 ) [1] => stdClass Object ( [id] => 40 [name] => Abbey Fireplaces [state] => NSW [classid] => 1 ) [2] => stdClass Object ( [id] => 55 [name] => Almighty Firewood [state] => NSW [classid] => 11 ) [3] => stdClass Object ( [id] => 70 [name] => Aussie Tree Services [state] => NSW [classid] => 11 ) [4] => stdClass Object ( [id] => 75 [name] => B C Sands [state] => NSW [classid] => 11 ) [5] => stdClass Object ( [id] => 76 [name] => B C Sands [state] => NSW [classid] => 11 ) [6] => stdClass Object ( [id] => 77 [name] => B C Sands Mascot [state] => NSW [classid] => 11 ) [7] => stdClass Object ( [id] => 113 [name] => Barbeques Galore (Maitland) [state] => NSW [classid] => 1 ) [8] => stdClass Object ( [id] => 114 [name] => Barbeques Galore Batemans Bay [state] => NSW [classid] => 1 ) [9] => stdClass Object ( [id] => 116 [name] => Barbeques Galore Wagga Wagga [state] => NSW [classid] => 1 ) [10] => stdClass Object ( [id] => 131 [name] => Best Burning Fire Fuels [state] => NSW [classid] => 11 ) [11] => stdClass Object ( [id] => 132 [name] => Betta Burn Firewood [state] => NSW [classid] => 11 ) [12] => stdClass Object ( [id] => 136 [name] => Black Forest Firewood [state] => NSW [classid] => 11 ) [13] => stdClass Object ( [id] => 181 [name] => Central Coast Brick Supplies Pty Ltd [state] => NSW [classid] => 1 ) [14] => stdClass Object ( [id] => 188 [name] => Cheminee Pty Ltd [state] => NSW [classid] => 1 ) [15] => stdClass Object ( [id] => 249 [name] => Embers Heating & Restoration Supplies [state] => NSW [classid] => 1 ) [16] => stdClass Object ( [id] => 250 [name] => Emmbers Firewood Supplies [state] => NSW [classid] => 11 ) [17] => stdClass Object ( [id] => 292 [name] => Goulburn Sand & Soil [state] => NSW [classid] => 11 ) [18] => stdClass Object ( [id] => 304 [name] => Gunida Gunyah Aboriginal Corporation [state] => NSW [classid] => 11 ) [19] => stdClass Object ( [id] => 334 [name] => J Perram Firewood Supplies [state] => NSW [classid] => 11 ) [20] => stdClass Object ( [id] => 346 [name] => Jetmaster (Aust) Pty Ltd [state] => NSW [classid] => 1 ) [21] => stdClass Object ( [id] => 402 [name] => Mark H Newton [state] => NSW [classid] => 4 ) [22] => stdClass Object ( [id] => 416 [name] => Might Burn Red Gum Pty Ltd [state] => NSW [classid] => 11 ) [23] => stdClass Object ( [id] => 430 [name] => Mr Stoves Pool World [state] => NSW [classid] => 1 ) [24] => stdClass Object ( [id] => 455 [name] => O'Brien's Redgum Sawmills [state] => NSW [classid] => 11 ) [25] => stdClass Object ( [id] => 477 [name] => PJ & CM Ducat [state] => NSW [classid] => 11 ) [26] => stdClass Object ( [id] => 483 [name] => Quality Firewood All Seasons [state] => NSW [classid] => 11 ) [27] => stdClass Object ( [id] => 487 [name] => R J & M E Grealy [state] => NSW [classid] => 11 ) [28] => stdClass Object ( [id] => 494 [name] => RN & CA Taylor [state] => NSW [classid] => 11 ) [29] => stdClass Object ( [id] => 500 [name] => Rouse Hill Firewood [state] => NSW [classid] => 11 ) [30] => stdClass Object ( [id] => 504 [name] => Sapphire Coast Firewood [state] => NSW [classid] => 11 ) [31] => stdClass Object ( [id] => 534 [name] => Sydney Heaters Pty Ltd [state] => NSW [classid] => 1 ) [32] => stdClass Object ( [id] => 551 [name] => The Woodyard [state] => NSW [classid] => 11 ) [33] => stdClass Object ( [id] => 576 [name] => W R Campi [state] => NSW [classid] => 11 ) [34] => stdClass Object ( [id] => 602 [name] => Wood 4 U [state] => NSW [classid] => 11 ) [35] => stdClass Object ( [id] => 603 [name] => Wood Galore [state] => NSW [classid] => 11 ) [36] => stdClass Object ( [id] => 604 [name] => Wood Galore [state] => NSW [classid] => 11 ) ) [QLD] => Array ( [0] => stdClass Object ( [id] => 665 [name] => Brightspark Firewood Supplies [state] => QLD [classid] => 11 ) [1] => stdClass Object ( [id] => 702 [name] => Gleno's Firewood [state] => QLD [classid] => 11 ) [2] => stdClass Object ( [id] => 704 [name] => Gold Coast Fireplace & Barbeque Centre [state] => QLD [classid] => 1 ) [3] => stdClass Object ( [id] => 732 [name] => Longburn Wood [state] => QLD [classid] => 11 ) [4] => stdClass Object ( [id] => 742 [name] => Out In The Styx [state] => QLD [classid] => 11 ) [5] => stdClass Object ( [id] => 746 [name] => Quickfire Firewood [state] => QLD [classid] => 11 ) ) [SA] => Array ( [0] => stdClass Object ( [id] => 786 [name] => Adelaide Wholesale Landscape Supplies [state] => SA [classid] => 11 ) [1] => stdClass Object ( [id] => 825 [name] => Clare Wood Yard [state] => SA [classid] => 11 ) [2] => stdClass Object ( [id] => 826 [name] => Clewers Electrical and Furniture [state] => SA [classid] => 1 ) [3] => stdClass Object ( [id] => 832 [name] => Cullen Transport & Firewood Supplies [state] => SA [classid] => 11 ) [4] => stdClass Object ( [id] => 852 [name] => Enviro Systems Renewable Resources Ltd. [state] => SA [classid] => 11 ) [5] => stdClass Object ( [id] => 867 [name] => Gawler Landscaping Supplies [state] => SA [classid] => 11 ) [6] => stdClass Object ( [id] => 871 [name] => Green Hornet Building & Landscape Supplies [state] => SA [classid] => 11 ) [7] => stdClass Object ( [id] => 899 [name] => Mexican Living [state] => SA [classid] => 1 ) [8] => stdClass Object ( [id] => 903 [name] => Mount Barker Landscape Centre [state] => SA [classid] => 11 ) [9] => stdClass Object ( [id] => 911 [name] => Pecan Engineering Pty Ltd [state] => SA [classid] => 1 ) [10] => stdClass Object ( [id] => 919 [name] => PW & JD Plunkett [state] => SA [classid] => 11 ) [11] => stdClass Object ( [id] => 922 [name] => Renmark Firewoods & Storage Services [state] => SA [classid] => 11 ) [12] => stdClass Object ( [id] => 930 [name] => South Coast Firewood (FWS Inc) [state] => SA [classid] => 11 ) [13] => stdClass Object ( [id] => 937 [name] => Tinpak Trading Pty Ltd [state] => SA [classid] => 11 ) [14] => stdClass Object ( [id] => 940 [name] => Traeger's Earthmoving & Transport [state] => SA [classid] => 11 ) [15] => stdClass Object ( [id] => 947 [name] => Waterways Farm [state] => SA [classid] => 11 ) ) [TAS] => Array ( [0] => stdClass Object ( [id] => 985 [name] => Eastern Tiers Firewood [state] => TAS [classid] => 11 ) [1] => stdClass Object ( [id] => 998 [name] => Goods Water & Garden Supplies [state] => TAS [classid] => 11 ) [2] => stdClass Object ( [id] => 1011 [name] => K-Mac Wood Supplies [state] => TAS [classid] => 11 ) [3] => stdClass Object ( [id] => 1019 [name] => Leslie Vale Landscape & Gravel Supplies [state] => TAS [classid] => 11 ) [4] => stdClass Object ( [id] => 1023 [name] => Newmans Heating Shop [state] => TAS [classid] => 1 ) [5] => stdClass Object ( [id] => 1029 [name] => Pellet Fires Tasmania [state] => TAS [classid] => 1 ) ) [VIC] => Array ( [0] => stdClass Object ( [id] => 561 [name] => TRT Pastoral Group [state] => VIC [classid] => 11 ) [1] => stdClass Object ( [id] => 1059 [name] => A Grade Garden Products [state] => VIC [classid] => 11 ) [2] => stdClass Object ( [id] => 1062 [name] => A.F. Gason Pty Ltd [state] => VIC [classid] => 1 ) [3] => stdClass Object ( [id] => 1088 [name] => Alpine Timber [state] => VIC [classid] => 11 ) [4] => stdClass Object ( [id] => 1093 [name] => Aranbe Heat [state] => VIC [classid] => 1 ) [5] => stdClass Object ( [id] => 1096 [name] => ASAP Firewood [state] => VIC [classid] => 11 ) [6] => stdClass Object ( [id] => 1105 [name] => Bairnsdale Stoves Heaters & BBQ's [state] => VIC [classid] => 1 ) [7] => stdClass Object ( [id] => 1176 [name] => Burra Garden Supplies [state] => VIC [classid] => 11 ) [8] => stdClass Object ( [id] => 1231 [name] => Daryl Fagan Firewood [state] => VIC [classid] => 11 ) [9] => stdClass Object ( [id] => 1273 [name] => Firefox Industries Pty Ltd [state] => VIC [classid] => 1 ) [10] => stdClass Object ( [id] => 1297 [name] => Geoffrey R Gelletly [state] => VIC [classid] => 4 ) [11] => stdClass Object ( [id] => 1301 [name] => Gippsland Wood Heater Repairs [state] => VIC [classid] => 1 ) [12] => stdClass Object ( [id] => 1303 [name] => Glen Dimplex Australia Pty Ltd [state] => VIC [classid] => 1 ) [13] => stdClass Object ( [id] => 1306 [name] => Glowin' Firewood [state] => VIC [classid] => 11 ) [14] => stdClass Object ( [id] => 1328 [name] => HRL Technology Pty Ltd [state] => VIC [classid] => 1 ) [15] => stdClass Object ( [id] => 1415 [name] => Moran Logging Co Pty Ltd [state] => VIC [classid] => 11 ) [16] => stdClass Object ( [id] => 1427 [name] => Murray Industries Kerang [state] => VIC [classid] => 11 ) [17] => stdClass Object ( [id] => 1428 [name] => Murray Industries Swan Hill [state] => VIC [classid] => 4 ) [18] => stdClass Object ( [id] => 1436 [name] => North East Fwd Strategy Implementn Comm [state] => VIC [classid] => 1 ) [19] => stdClass Object ( [id] => 1438 [name] => Northern Landscape Supplies [state] => VIC [classid] => 11 ) [20] => stdClass Object ( [id] => 1448 [name] => Park Orchards Garden & Building Supplies [state] => VIC [classid] => 11 ) [21] => stdClass Object ( [id] => 1476 [name] => Redgumdave [state] => VIC [classid] => 11 ) [22] => stdClass Object ( [id] => 1493 [name] => Sand, Soil & Rocks Pty Ltd [state] => VIC [classid] => 11 ) [23] => stdClass Object ( [id] => 1495 [name] => Shamic Sheetmetal Aust Pty Ltd [state] => VIC [classid] => 1 ) [24] => stdClass Object ( [id] => 1516 [name] => Surrey Hills Firewood Supplies [state] => VIC [classid] => 11 ) [25] => stdClass Object ( [id] => 1529 [name] => The Flue Factory Pty Ltd [state] => VIC [classid] => 1 ) [26] => stdClass Object ( [id] => 1553 [name] => Trevor King [state] => VIC [classid] => 11 ) [27] => stdClass Object ( [id] => 1585 [name] => Whitlands Engineering [state] => VIC [classid] => 1 ) [28] => stdClass Object ( [id] => 1591 [name] => Wignells of Melbourne Pty Ltd [state] => VIC [classid] => 1 ) [29] => stdClass Object ( [id] => 1604 [name] => WWF Australia [state] => VIC [classid] => 1 ) [30] => stdClass Object ( [id] => 1609 [name] => Yarra Timber Salvage [state] => VIC [classid] => 11 ) ) [WA] => Array ( [0] => stdClass Object ( [id] => 1623 [name] => All Suburbs Garden & Wood Supply [state] => WA [classid] => 11 ) [1] => stdClass Object ( [id] => 1628 [name] => Barbecue Bazaar Cannington [state] => WA [classid] => 1 ) [2] => stdClass Object ( [id] => 1672 [name] => European Wood Fired Ovens [state] => WA [classid] => 1 ) [3] => stdClass Object ( [id] => 1681 [name] => Freo Firewood Supply [state] => WA [classid] => 11 ) [4] => stdClass Object ( [id] => 1740 [name] => Roleystone Firewood [state] => WA [classid] => 11 ) [5] => stdClass Object ( [id] => 1747 [name] => Smart Burn Pty Ltd [state] => WA [classid] => 1 ) [6] => stdClass Object ( [id] => 1748 [name] => Specialised Tree Service [state] => WA [classid] => 11 ) [7] => stdClass Object ( [id] => 1855 [name] => York Firewood & Milling [state] => WA [classid] => 11 ) ) )
usort($array, function($a, $b) { return count($a) - count($b); }); Assuming >= PHP 5.3
After running this, $arr will now be sorted from highest populated elements to lower. function cmpArray($a, $b) { $countA = count($a); $countB = count($b); if ($countA == $countB) { return 0; } return ($countA < $countB) ? 1 : -1; } uasort($arr, "cmpArray");
Similar to #alex's answer, but without the anonymous function (so it works on PHP <5.3) <?php $array = array( 'three' => array( 'one', 'two', 'three' ), 'one' => array( 'one' ), 'two' => array( 'one', 'two' ), ); function csort($a, $b) { return count($a) - count($b); } uasort($array, 'csort'); print_r($array); ?> Output: Array ( [one] => Array ( [0] => one ) [two] => Array ( [0] => one [1] => two ) [three] => Array ( [0] => one [1] => two [2] => three ) )