Removing duplicate data in a multi dimension array (associative array) - php

I have three keys pointing to value of type array, in those array values I have duplicate values. How can I remove them?
array(3) {
["rock"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "4"
["title"]=>
string(9) "Blink-182"
["location"]=>
string(25) "Motorpoint Arena, Cardiff"
["event_date"]=>
string(10) "2017-07-03"
["event_time"]=>
string(8) "18:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.478937400000010"
["geo_long"]=>
string(17) "-3.17172289999996"
["tags"]=>
string(14) "rock,metal"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["alternative"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "6"
["title"]=>
string(6) "Sum 41"
["location"]=>
string(23) "O2 Academy Leeds, Leeds"
["event_date"]=>
string(10) "2017-02-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.802188400000000"
["geo_long"]=>
string(17) "-1.54713770000001"
["tags"]=>
string(21) "alternative,indie"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["sax"]=>
array(1) {
[0]=>
array(9) {
["id"]=>
string(1) "3"
["title"]=>
string(9) "Take That"
["location"]=>
string(22) "The SSE Hydro, Glasgow"
["event_date"]=>
string(10) "2017-05-11"
["event_time"]=>
string(8) "18:30:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "55.860156000000000"
["geo_long"]=>
string(17) "-4.28525800000000"
["tags"]=>
string(24) "pop,boy-band,sax"
}
}
}

I'm going to assume that if two bands have the same 'id' then they are identical
$unique_bands = array();
// if the master array is called bands
foreach($bands as $genre){
foreach($genre as $band){
$unqiue_bands[$band['id']] = $band;
}
}
Maybe put $unique_bands = array_values(array_filter($unique_bands)); at the end to reindex the array

You have a solution on the official documentation... A person made a simple function to do exactly what you want.
Example array:
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew", "num"=>"784581254"),
);
?>
Function:
<?php
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
?>
You will have to fix it to work your for array dimension.

Related

PHP: Convert Google Sheets result object to simple array

I've google sheet with this data:
I'm pulling it from google drive with simple code:
$ranges = [
'Price_1'
];
$params = array(
'ranges' => $ranges
);
$response = $service->spreadsheets_values->batchGet($spreadsheetId, $params);
The response is a complicated object:
object(Google_Service_Sheets_BatchGetValuesResponse)#58 (8) { ["collection_key":protected]=> string(11) "valueRanges" ["spreadsheetId"]=> string(44) "1BQpKLuvlaVeGqIqoGUOynF9IZ5C3zK6gagZiRJO1UEU" ["valueRangesType":protected]=> string(32) "Google_Service_Sheets_ValueRange" ["valueRangesDataType":protected]=> string(5) "array" ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } ["valueRanges"]=> array(1) { [0]=> object(Google_Service_Sheets_ValueRange)#67 (7) { ["collection_key":protected]=> string(6) "values" ["majorDimension"]=> string(4) "ROWS" ["range"]=> string(14) "Price_1!E2:E74" ["values"]=> array(65) { [0]=> array(1) { [0]=> string(2) "28" } [1]=> array(1) { [0]=> string(2) "14" } [2]=> array(1) { [0]=> string(1) "0" } [3]=> array(0) { } [4]=> array(1) { [0]=> string(2) "55" } [5]=> array(1) { [0]=> string(2) "28" } [6]=> array(1) { [0]=> string(1) "0" } [7]=> array(0) { } [8]=> array(0) { } [9]=> array(0) { } [10]=> array(0) { } [11]=> array(0) { } [12]=> array(0) { } [13]=> array(0) { } [14]=> array(0) { } [15]=> array(0) { } [16]=> array(0) { } [17]=> array(0) { } [18]=> array(0) { } [19]=> array(0) { } [20]=> array(1) { [0]=> string(2) "59" } [21]=> array(1) { [0]=> string(2) "49" } [22]=> array(0) { } [23]=> array(1) { [0]=> string(2) "38" } [24]=> array(1) { [0]=> string(2) "38" } [25]=> array(0) { } [26]=> array(1) { [0]=> string(2) "37" } [27]=> array(1) { [0]=> string(2) "25" } [28]=> array(1) { [0]=> string(1) "0" } [29]=> array(0) { } [30]=> array(1) { [0]=> string(2) "79" } [31]=> array(1) { [0]=> string(2) "99" } [32]=> array(0) { } [33]=> array(1) { [0]=> string(2) "75" } [34]=> array(1) { [0]=> string(2) "99" } [35]=> array(0) { } [36]=> array(1) { [0]=> string(2) "79" } [37]=> array(1) { [0]=> string(2) "99" } [38]=> array(0) { } [39]=> array(1) { [0]=> string(3) "160" } [40]=> array(1) { [0]=> string(3) "190" } [41]=> array(0) { } [42]=> array(0) { } [43]=> array(0) { } [44]=> array(0) { } [45]=> array(0) { } [46]=> array(0) { } [47]=> array(0) { } [48]=> array(0) { } [49]=> array(0) { } [50]=> array(0) { } [51]=> array(0) { } [52]=> array(1) { [0]=> string(2) "33" } [53]=> array(0) { } [54]=> array(1) { [0]=> string(2) "59" } [55]=> array(1) { [0]=> string(2) "34" } [56]=> array(1) { [0]=> string(1) "0" } [57]=> array(0) { } [58]=> array(1) { [0]=> string(2) "29" } [59]=> array(1) { [0]=> string(2) "14" } [60]=> array(1) { [0]=> string(1) "0" } [61]=> array(0) { } [62]=> array(1) { [0]=> string(2) "37" } [63]=> array(1) { [0]=> string(2) "25" } [64]=> array(1) { [0]=> string(1) "0" } } ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } } } }
and var_dump($response->getValueRanges()[0]['values']); gives this result:
array(74) { [0]=> array(5) { [0]=> string(2) "ID" [1]=> string(10) "Ticket for" [2]=> string(9) "Excursion" [3]=> string(8) "Duration" [4]=> string(14) "Price (Brutto)" } [1]=> array(5) { [0]=> string(3) "01A" [1]=> string(13) "adult ( 12+ )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "28" } [2]=> array(5) { [0]=> string(3) "01B" [1]=> string(18) "child ( age 5-11 )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "14" } [3]=> array(5) { [0]=> string(3) "01C" [1]=> string(18) "toodler ( age 0-4)" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(1) "0" } [4]=> array(0) { } [5]=> array(5) { [0]=> string(3) "02A" [1]=> string(13) "adult, age 8+" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "55" } [6]=> array(5) { [0]=> string(3) "02B" [1]=> string(14) "child, age 3-7" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "28" } [7]=> array(5) { [0]=> string(3) "02C" [1]=> string(16) "toodler, age 0-2" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(1) "0" } [8]=> array(0) { } [9]=> array(3) { [0]=> string(3) "03A" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [10]=> array(3) { [0]=> string(3) "03B" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [11]=> array(3) { [0]=> string(3) "03C" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [12]=> array(0) { } [13]=> array(3) { [0]=> string(3) "04A" [1]=> string(0) "" [2]=> string(9) "Submarine" } [14]=> array(3) { [0]=> string(3) "04B" [1]=> string(0) "" [2]=> string(9) "Submarine" } [15]=> array(3) { [0]=> string(3) "04C" [1]=> string(0) "" [2]=> string(9) "Submarine" } [16]=> array(0) { } [17]=> array(3) { [0]=> string(3) "05A" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [18]=> array(3) { [0]=> string(3) "05B" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [19]=> array(3) { [0]=> string(3) "05C" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [20]=> array(0) { } [21]=> array(5) { [0]=> string(3) "06A" [1]=> string(9) "fisherman" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "59" } [22]=> array(5) { [0]=> string(3) "06B" [1]=> string(9) "spectator" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "49" } [23]=> array(0) { } [24]=> array(5) { [0]=> string(3) "07A" [1]=> string(5) "adult" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [25]=> array(5) { [0]=> string(3) "07B" [1]=> string(13) "child, age 6+" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [26]=> array(0) { } [27]=> array(5) { [0]=> string(3) "08A" [1]=> string(14) "adult, age 12+" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "37" } [28]=> array(5) { [0]=> string(3) "08B" [1]=> string(15) "child, age 3-11" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "25" } [29]=> array(5) { [0]=> string(3) "08C" [1]=> string(16) "toodler, age 0-1" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(1) "0" } [30]=> array(0) { } [31]=> array(5) { [0]=> string(3) "09A" [1]=> string(17) "flight from 800m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "15min" [4]=> string(2) "79" } [32]=> array(5) { [0]=> string(3) "09B" [1]=> string(18) "flight from 1000m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "25min" [4]=> string(2) "99" } [33]=> array(0) { } [34]=> array(5) { [0]=> string(3) "10A" [1]=> string(21) "Single (for 1 person)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "75" } [35]=> array(5) { [0]=> string(3) "10B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "99" } [36]=> array(0) { } [37]=> array(5) { [0]=> string(3) "11A" [1]=> string(21) "Single (for 1 person)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "79" } [38]=> array(5) { [0]=> string(3) "11B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "99" } [39]=> array(0) { } [40]=> array(5) { [0]=> string(3) "12A" [1]=> string(21) "Single (for 1 person)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "160" } [41]=> array(5) { [0]=> string(3) "12B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "190" } [42]=> array(0) { } [43]=> array(3) { [0]=> string(3) "13A" [1]=> string(0) "" [2]=> string(8) "Trekking" } [44]=> array(3) { [0]=> string(3) "13B" [1]=> string(0) "" [2]=> string(8) "Trekking" } [45]=> array(3) { [0]=> string(3) "13C" [1]=> string(0) "" [2]=> string(8) "Trekking" } [46]=> array(0) { } [47]=> array(3) { [0]=> string(3) "14A" [1]=> string(0) "" [2]=> string(7) "Karting" } [48]=> array(3) { [0]=> string(3) "14B" [1]=> string(0) "" [2]=> string(7) "Karting" } [49]=> array(3) { [0]=> string(3) "14C" [1]=> string(0) "" [2]=> string(7) "Karting" } [50]=> array(0) { } [51]=> array(3) { [0]=> string(3) "15A" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [52]=> array(3) { [0]=> string(3) "15B" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [53]=> array(5) { [0]=> string(3) "15C" [1]=> string(0) "" [2]=> string(11) "Jungle park" [3]=> string(0) "" [4]=> string(2) "33" } [54]=> array(0) { } [55]=> array(5) { [0]=> string(3) "16A" [1]=> string(14) "adult, age 12+" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "59" } [56]=> array(5) { [0]=> string(3) "16B" [1]=> string(15) "child, age 2-11" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "34" } [57]=> array(5) { [0]=> string(3) "16C" [1]=> string(16) "toodler, age 0-1" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(1) "0" } [58]=> array(0) { } [59]=> array(5) { [0]=> string(3) "17A" [1]=> string(14) "adult, age 12+" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "29" } [60]=> array(5) { [0]=> string(3) "17B" [1]=> string(15) "child, age 2-11" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "14" } [61]=> array(5) { [0]=> string(3) "17C" [1]=> string(16) "toodler, age 0-1" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(1) "0" } [62]=> array(0) { } [63]=> array(5) { [0]=> string(3) "18A" [1]=> string(14) "adult, age 12+" [2]=> string(11) "Loro parque" [3]=> string(12) "8:30 - 18:45" [4]=> string(2) "37" } [64]=> array(5) { [0]=> string(3) "18B" [1]=> string(15) "child, age 6-11" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(2) "25" } [65]=> array(5) { [0]=> string(3) "18C" [1]=> string(16) "toodler, age 0-1" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(1) "0" } [66]=> array(0) { } [67]=> array(3) { [0]=> string(3) "19A" [1]=> string(0) "" [2]=> string(9) "Antologia" } [68]=> array(3) { [0]=> string(3) "19B" [1]=> string(0) "" [2]=> string(9) "Antologia" } [69]=> array(3) { [0]=> string(3) "19C" [1]=> string(0) "" [2]=> string(9) "Antologia" } [70]=> array(0) { } [71]=> array(3) { [0]=> string(3) "20A" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [72]=> array(3) { [0]=> string(3) "20B" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [73]=> array(3) { [0]=> string(3) "20C" [1]=> string(0) "" [2]=> string(13) "Medieval show" } }
And:
for($i = 1; $i < 5; $i++) {
print_r ($response->getValueRanges()[0]['values'][$i]);
}
Gives: Array ( [0] => 01A [1] => adult ( 12+ ) [2] => Catamaran [3] => 2h [4] => 28 ) Array ( [0] => 01B [1] => child ( age 5-11 ) [2] => Catamaran [3] => 2h [4] => 14 ) Array ( [0] => 01C [1] => toodler ( age 0-4) [2] => Catamaran [3] => 2h [4] => 0 ) Array ( )
In result I need a simple array, which will hold a key[row id], the ID from from ID row and price from last row, somethig like this: $result[$i] = unique_id=1, ID=01A, price=28
How it could be achieved?
So after getting the data for google sheet with:
$arr = $response->getValueRanges()[0]['values'];
You can use this simple for loop to get the data as you need:
// notice start from 1 to avoid the headers
for($i = 1; $i < count($arr); $i++) {
$res[$i] = array("unique_id" => $i, "ID" => $arr[$i][0], "price" => $arr[$i][4]);
}
Now $res will contain you structure.

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

Working with multiple array in PHP

I have an array like this :
$optionarray:
array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [1]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [2]=> array(3) { [0]=> string(19) "Formation" [1]=> string(1) "1" [2]=> string(1) "0" } }
$dataarray:
array(2) { [0]=> array(3) { [0]=> string(23) "Physical room" [1]=> int(1) [2]=> string(7) "12,00 €" } [1]=> array(3) { [0]=> string(26) "user desk" [1]=> int(4) [2]=> string(7) "40,00 €" } }
$fullarray:
array(5) { [0]=> array(3) { [0]=> string(23) "Physical room" [1]=> int(1) [2]=> string(7) "12,00 €" } [1]=> array(3) { [0]=> string(26) "user desk" [1]=> int(4) [2]=> string(7) "40,00 €" } [2]=> array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [1]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [2]=> array(3) { [0]=> string(19) "Formation " [1]=> string(1) "1" [2]=> string(1) "0" } } [3]=> array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [1]=> array(3) { [0]=> string(19) "Formation" [1]=> string(1) "1" [2]=> string(1) "0" } [2]=> NULL } [4]=> array(3) { [0]=> array(3) { [0]=> string(19) "Formation " [1]=> string(1) "1" [2]=> string(1) "0" } [1]=> NULL [2]=> NULL } }
My fullarray has not a unique structure , I want just to add $optionarray to $dataarray and iterate on $fullarray
My code :
$data[0] = array('Physical room', $nbprice, $totalprice);
$data[1] = array('user desk', $nbuser, $totaluserprice );
$i=2;
for($j=0;$j<count($optionsdata);$j++){
$data[$i]=array($optionsdata[$j],$optionsdata[$j+1],$optionsdata[$j+2]);
$i=$i+1;
}
How can I proceed for create my $fullarray properly for iterate and display each value ?
Expected output:
array(5){
[0]=>array(3){ [0]=> string(23) "Physical room" [1]=> int(1) [2]=> string(7) "12,00 €" }
[1]=> array(3) { [0]=> string(26) "user desk" [1]=> int(4) [2]=> string(7) "40,00 €" }
[2]=> array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" }
[3]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" }
[4]=> array(3) { [0]=> string(19) "Formation " [1]=> string(1) "1" [2]=> string(1) "0" }
}

List data in alphabetical order in concerned sections

I have an array of data that contains the alphabetically sorted data, but in view I have different sections. Each alphabet represents each section.
I need to list the data in such order that section with A must contain all the names starting with A, section B must contain the names sarting with B and so on.
My work out
I have made an array that gives me the data separates the data with there starting alphabets
foreach ($data['stores_cat_data'] as $store ) {
$store_title = $store['title']."<br />";
$curr = current(str_split($store_title));
if(!preg_match("/^[a-zA-Z]$/", $curr))
{
$storeArray['0-9'][$store_title] = $store;
}
else{
$storeArray[$curr][$store_title] = $store;
}
}
EDITED
my output is
array(46) {
["0-9"]=>
array(21) {
["1 800 Lighting
"]=>
array(7) {
["title"]=>
string(14) "1 800 Lighting"
["cCommisions_percentage"]=>
string(5) "8.00%"
["url"]=>
string(49) "http://www.gopjn.com/t/S0BHSUVJQERDREpHSUBHS0xKRg"
["id"]=>
string(3) "539"
["logo"]=>
string(14) "1389213829.gif"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["1-800 CONTACTS
"]=>
array(7) {
["title"]=>
string(14) "1-800 CONTACTS"
["cCommisions_percentage"]=>
string(73) "2.4%(Repeat Customer Repeat Sale) and 7.2%(New Customer First Time Sale) "
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.274994&fot=9999&foc=1"
["id"]=>
string(3) "598"
["logo"]=>
string(14) "1389289817.jpg"
["publisher_id"]=>
string(1) "4"
["subid"]=>
string(13) "&fobs={subid}"
}
["1-800-Bakery
"]=>
array(7) {
["title"]=>
string(13) "1-800-Bakery "
["cCommisions_percentage"]=>
string(30) "10.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=266793&type=3&subid=0"
["id"]=>
string(2) "22"
["logo"]=>
string(14) "1387403703.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-BASKETS.COM
"]=>
array(7) {
["title"]=>
string(17) "1-800-BASKETS.COM"
["cCommisions_percentage"]=>
string(5) "8.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.216994&fot=9999&foc=1"
["id"]=>
string(3) "599"
["logo"]=>
string(14) "1389289896.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-FLOWERS.COM
"]=>
array(7) {
["title"]=>
string(17) "1-800-FLOWERS.COM"
["cCommisions_percentage"]=>
string(6) "10.40%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.225130&fot=9999&foc=1"
["id"]=>
string(4) "1659"
["logo"]=>
string(14) "1390516089.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-PetMeds
"]=>
array(7) {
["title"]=>
string(13) "1-800-PetMeds"
["cCommisions_percentage"]=>
string(42) "12.00%-16.00% commission on selected goods"
["url"]=>
string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=56753&type=3&subid=0"
["id"]=>
string(2) "24"
["logo"]=>
string(14) "1387403850.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["100 Percent Pure
"]=>
array(7) {
["title"]=>
string(16) "100 Percent Pure"
["cCommisions_percentage"]=>
string(29) "8.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=270135&type=3&subid=0"
["id"]=>
string(2) "25"
["logo"]=>
string(14) "1387404086.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["123Inkjets.com
"]=>
array(7) {
["title"]=>
string(15) "123Inkjets.com "
["cCommisions_percentage"]=>
string(41) "25.6% and 4%(OEM and Office Supply items)"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.217798&fot=9999&foc=1"
["id"]=>
string(3) "596"
["logo"]=>
string(14) "1389289585.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["123Print
"]=>
array(7) {
["title"]=>
string(8) "123Print"
["cCommisions_percentage"]=>
string(5) "6.40%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.276533&fot=9999&foc=1"
["id"]=>
string(3) "597"
["logo"]=>
string(14) "1389289736.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["191 Unlimited
"]=>
array(7) {
["title"]=>
string(13) "191 Unlimited"
["cCommisions_percentage"]=>
string(5) "5.00%"
["url"]=>
string(49) "http://www.pntrs.com/t/S0BLS0ZGQERDREpHSUBKS0ZGSg"
["id"]=>
string(3) "540"
["logo"]=>
string(14) "1389214452.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["2020ave.com
"]=>
array(7) {
["title"]=>
string(11) "2020ave.com"
["cCommisions_percentage"]=>
string(6) "10.00%"
["url"]=>
string(50) "http://www.pjtra.com/t/TEFNSkZIQUVERUtISkFFREZHSks"
["id"]=>
string(3) "542"
["logo"]=>
string(14) "1389214732.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["24 Hour Fitness
"]=>
array(7) {
["title"]=>
string(15) "24 Hour Fitness"
["cCommisions_percentage"]=>
string(8) "4% to 8%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.256270&fot=9999&foc=1"
["id"]=>
string(3) "601"
["logo"]=>
string(14) "1389290288.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["2bstores.com
"]=>
array(7) {
["title"]=>
string(12) "2bstores.com"
["cCommisions_percentage"]=>
string(5) "5.00%"
["url"]=>
string(51) "http://www.pntrac.com/t/TEFNSUZMQUVERUtISkFFRERKR0s"
["id"]=>
string(3) "543"
["logo"]=>
string(14) "1389214901.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["360training
"]=>
array(7) {
["title"]=>
string(12) "360training "
["cCommisions_percentage"]=>
string(6) "12.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.737181&fot=9999&foc=1"
["id"]=>
string(3) "602"
["logo"]=>
string(14) "1389290350.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 All Memory
"]=>
array(7) {
["title"]=>
string(12) "4 All Memory"
["cCommisions_percentage"]=>
string(29) "8.00% commission on all goods"
["url"]=>
string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=99169&type=3&subid=0"
["id"]=>
string(2) "28"
["logo"]=>
string(14) "1387404383.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 Wheel Drive Hardware
"]=>
array(7) {
["title"]=>
string(22) "4 Wheel Drive Hardware"
["cCommisions_percentage"]=>
string(5) "5.60%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730591&fot=9999&foc=1"
["id"]=>
string(3) "603"
["logo"]=>
string(14) "1389290418.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 Wheel Parts
"]=>
array(7) {
["title"]=>
string(14) "4 Wheel Parts "
["cCommisions_percentage"]=>
string(5) "4.80%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730526&fot=9999&foc=1"
["id"]=>
string(3) "604"
["logo"]=>
string(14) "1389290467.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4imprint
"]=>
array(7) {
["title"]=>
string(8) "4imprint"
["cCommisions_percentage"]=>
string(5) "3.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.161896&fot=9999&foc=1"
["id"]=>
string(3) "605"
["logo"]=>
string(14) "1389290519.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["525 America
"]=>
array(7) {
["title"]=>
string(11) "525 America"
["cCommisions_percentage"]=>
string(5) "6.00%"
["url"]=>
string(49) "http://www.pntra.com/t/S0BLSkZKQERDREpHSUBKQ0dFRw"
["id"]=>
string(3) "544"
["logo"]=>
string(14) "1389215113.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["60 Minute Payday
"]=>
array(7) {
["title"]=>
string(17) "60 Minute Payday "
["cCommisions_percentage"]=>
string(4) "$48 "
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.164179&fot=9999&foc=1"
["id"]=>
string(3) "606"
["logo"]=>
string(14) "1389290591.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["7 For All Mankind
"]=>
array(7) {
["title"]=>
string(17) "7 For All Mankind"
["cCommisions_percentage"]=>
string(29) "4.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256004&type=3&subid=0"
["id"]=>
string(2) "29"
["logo"]=>
string(14) "1387404461.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
}
["A"]=>
array(83) {
["A.N.S.I
"]=>
array(7) {
["title"]=>
string(7) "A.N.S.I"
["cCommisions_percentage"]=>
string(30) "10.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=208750&type=3&subid=0"
["id"]=>
string(2) "48"
["logo"]=>
string(14) "1387408300.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["ABC Distributing LLC
"]=>
array(7) {
["title"]=>
string(20) "ABC Distributing LLC"
["cCommisions_percentage"]=>
string(69) "4.00% commission on all goods; [2.00% commission on selected goods]; "
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=238767&type=3&subid=0"
["id"]=>
string(2) "30"
["logo"]=>
string(14) "1387404636.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["ABCmouse.com
"]=>
array(7) {
["title"]=>
string(12) "ABCmouse.com"
["cCommisions_percentage"]=>
string(27) "$4.00 flat fee on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256295&type=3&subid=0"
["id"]=>
string(2) "31"
["logo"]=>
string(14) "1387405955.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abe's of Maine
"]=>
array(7) {
["title"]=>
string(15) "Abe's of Maine "
["cCommisions_percentage"]=>
string(5) "4.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.290656&fot=9999&foc=1"
["id"]=>
string(3) "607"
["logo"]=>
string(14) "1389290715.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abhair
"]=>
array(7) {
["title"]=>
string(6) "Abhair"
["cCommisions_percentage"]=>
string(37) "12.00%-17.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=280480&type=3&subid=0"
["id"]=>
string(2) "33"
["logo"]=>
string(14) "1387406425.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["About Airport Parking
"]=>
array(7) {
["title"]=>
string(21) "About Airport Parking"
["cCommisions_percentage"]=>
string(6) "50.00%"
["url"]=>
string(49) "http://www.pntrs.com/t/S0BMRUVFQERDREpHSUBMRUpHSA"
["id"]=>
string(3) "560"
["logo"]=>
string(14) "1389224403.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["Absolute LoJack
"]=>
array(7) {
["title"]=>
string(16) "Absolute LoJack "
["cCommisions_percentage"]=>
string(73) "$1.00 flat fee on selected goods; [15.00% commission on selected goods]; "
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=274953&type=3&subid=0"
["id"]=>
string(2) "34"
["logo"]=>
string(14) "1387406520.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abt.com
"]=>
array(7) {
["title"]=>
string(7) "Abt.com"
["cCommisions_percentage"]=>
string(5) "3.00%"
["url"]=>
string(50) "http://www.pjatr.com/t/TEFNSUtIQUVERUtISkFFREVIREc"
["id"]=>
string(3) "547"
["logo"]=>
string(14) "1389215949.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["Accessorize
"]=>
array(7) {
["title"]=>
string(11) "Accessorize"
["cCommisions_percentage"]=>
string(29) "5.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=215113&type=3&subid=0"
["id"]=>
string(4) "1660"
["logo"]=>
string(14) "1390518973.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["AccessoryGeeks.com
"]=>
array(7) {
["title"]=>
string(19) "AccessoryGeeks.com "
["cCommisions_percentage"]=>
string(6) "13.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.227987&fot=9999&foc=1"
["id"]=>
string(3) "608"
["logo"]=>
string(14) "1389291008.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
This should do it:
$storeArray = array();
foreach ($data['stores_cat_data'] as $store) {
$title = $store['title'];
$first_char = substr($title, 0, 1);
if (preg_match("/^[0-9]$/", $first_char)) {
$key = "0-9";
} else {
$key = strtoupper($first_char);
}
if (!isset($storeArray[$key])) {
$storeArray[$key] = array();
}
$storeArray[$key][$title] = $store;
}
var_dump($storeArray);
Your code looks almost correct. The way you determine $curr is a little strange and you have to account for uppercase/lowercase. Also, to avoid array warnings make sure you initialize each letter of the alphabet, as you go.
You might also want to remove all non-alphanumeric characters, like " and - that might mess up the detection. A title of "Best" book shop will be put under 0-9 where B would be better.
A regexp like: preg_replace("/[^a-zA-Z0-9]/", "", $str) should do it.

PHP Group By Day

Given this array:
array(1) {
[0]=>
array(2) {
["Project"]=>
array(5) {
["id"]=>
string(1) "2"
["user_id"]=>
string(2) "21"
["customer_id"]=>
string(1) "4"
["name"]=>
string(15) "WordPress Theme"
["created"]=>
string(19) "2011-09-26 21:30:38"
}
["Track"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "7"
["user_id"]=>
string(2) "21"
["project"]=>
string(1) "2"
["customer"]=>
string(1) "4"
["title"]=>
string(7) "Backend"
["notes"]=>
string(0) ""
["created"]=>
string(19) "2011-09-28 22:21:22"
["Lapse"]=>
array(2) {
[0]=>
array(5) {
["id"]=>
string(1) "4"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:21"
["stop"]=>
string(19) "2011-09-28 22:22:30"
["created"]=>
string(19) "2011-09-28 22:22:21"
}
[1]=>
array(5) {
["id"]=>
string(1) "3"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:07"
["stop"]=>
string(19) "2011-09-28 22:22:12"
["created"]=>
string(19) "2011-09-28 22:22:07"
}
}
}
}
}
}
How would i group by Day in the Lapse Array with PHP? This may have been easier to do directly with MySQL but i'm using CakePHP's recursive function and i cant figure out how to use Group By with that!
$list = array();
function extractByDates($arr) {
foreach ($arr as $key => $v)
if (is_array($v))
function extractByDates($v);
else if ($key == 'created')
$list[$v] = $v;
}
extractByDates($yourGivenArray);
I not tested!

Categories