Below is My Array. I want to make all possible combinations of array whose sum is equal to some number(e.g 20). The super parent array is continent and it's child array is it's countries and countries child array is its respective cities. i want to get all combinations of countries whose cities sum of days_range equal to some number(e.g 20). This is just a single continent array. it may be possible that the array is for other continents are also available.
Array
(
[1] => Array
(
[10] => Array
(
[0] => Array
(
[id] => 18
[city_name] => Baku
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
[2] => Array
(
[id] => 43
[city_name] => Lahıc
[country_id] => 10
[country_name] => Azerbaijan
[days_range] => 5
[continent_id] => 1
)
)
[23] => Array
(
[0] => Array
(
[id] => 42
[city_name] => Vientiane
[country_id] => 23
[country_name] => Laos
[days_range] => 7
[continent_id] => 1
)
[1] => Array
(
[id] => 47
[city_name] => Vang Vieng
[country_id] => 23
[country_name] => Laos
[days_range] => 3
[continent_id] => 1
)
)
[20] => Array
(
[0] => Array
(
[id] => 37
[city_name] => Tamsui District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 1
[continent_id] => 1
)
[1] => Array
(
[id] => 35
[city_name] => Taipei
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
[22] => Array
(
[2] => Array
(
[id] => 46
[city_name] => Maolin District
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
[3] => Array
(
[id] => 36
[city_name] => Tainan
[country_id] => 20
[country_name] => Taiwan
[days_range] => 3
[continent_id] => 1
)
)
)
)
Below is my expected Output.
Array(
[0]=Array(
[0]=>[10],
[1]=>[23],
)
[1]=Array(
[0]=>[10],
[1]=>[20],
[2]=>[22],
)
[2]=Array(
[0]=>[23],
[1]=>[20],
[2]=>[22],
)
)
I am attempting to json_encode an array in php5.3.
json_encode($paperwork_info[0])
The result is bad json. The array is as follows:
[paperwork_guid] => c5bfe512-908d-c513-5a5e-e3a2fbb5548b
[name] => recycle
[sections] => Array
(
[0] => header
[1] => customer
[2] => scope_bullets
[3] => product
[4] => signature
[5] => installer
[6] => order_data
)
[data] => Array
(
[header] => Array
(
[image] => Array
(
[src] => recycle.png
[format] => float:left
)
)
[customer] => Array
(
[f_name] => TEST ONLY
[l_name] => NEIMEIER
[middle_name] => none
[title] => none
[address1] => 28 OEHMAN BLVD
[address2] =>
[address3] =>
[zip] => 14225
[city] => BUFFALO
[state_abbrev] => NY
[email_address] => DALE.NEIMEIER#INSTALLS.COM
[phones] => Array
(
[alt] =>
[cell] =>
[work] =>
[home] => 7165551212
)
)
[scope_bullets] => Array
(
[sku_3001] => Array
(
[bullet_0] => Array
(
[paperwork_bullet_obj_guid] => 2ebefa96-6f6b-069e-e194-245d138b9845
[ordering] => 1
[bullet_text] => This is bullet point #1
[child_level] => 1
[formatting] => Array
(
[0] => Array
(
[start_word] => 2
[num_words] => 0
[tag] => b
[href] =>
)
[1] => Array
(
[start_word] => 2
[num_words] => 1
[tag] => a
[href] => http://www.cnn.com
)
)
)
[bullet_1] => Array
(
[paperwork_bullet_obj_guid] => 734db3f4-01a0-b025-9624-cc52a1845dff
[ordering] => 1
[bullet_text] => Sub-point #1.1
[child_level] => 2
)
[bullet_2] => Array
(
[paperwork_bullet_obj_guid] => ebf5ef02-906e-2005-e499-27eae2edefe9
[ordering] => 1
[bullet_text] => Sub point #1.1.1
[child_level] => 3
)
[bullet_3] => Array
(
[paperwork_bullet_obj_guid] => 447997c1-fd9c-25be-b9bf-39257009fb8b
[ordering] => 1
[bullet_text] => This is bullet point #2
[child_level] => 1
)
[bullet_4] => Array
(
[paperwork_bullet_obj_guid] => 5def2d9c-d322-788c-0afe-d13707004b97
[ordering] => 1
[bullet_text] => Sub point #2.1
[child_level] => 2
)
[bullet_5] => Array
(
[paperwork_bullet_obj_guid] => 84936151-65a3-bcca-951f-b69ff16d34ec
[ordering] => 2
[bullet_text] => Sub point #2.2
[child_level] => 2
)
)
[sku_4405] =>
)
[product] => Array
(
[0] => Array
(
[model] => Paperwork Test abc123
[serial] => 777333
[weight] => 26
[size] =>
[width] =>
[height] => 32
[num_boxes] => 1
[product_name] =>
[sku] =>
[model_number] =>
[part_number] =>
[serial_number] =>
[cat_value] => Best Buy Equipment
[product] =>
[product_type] =>
[product_size] =>
[category] =>
)
)
[signature] => Array
(
[date] => 2016-06-15
[need_customer] => 1
[need_tech] => 1
[need_store] =>
[need_warehouse] =>
)
[installer] => Array
(
[anum] => 45455
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[phone] => (317) 519-0481
[tech_f_name] =>
[tech_l_name] =>
[company_name] => ZICO LLC
[email_address] => ZICOLLC#GMAIL.COM
)
[order_data] => Array
(
[assigned_date] => 2016-06-07 22:24:47-04
[purchased_date] => 2016-06-06
[creation_date] => 2016-06-06
[install_date] => 2016-06-10
[install_time] => 19:00:00
[prescheduled_date] => 1969-12-31
[prom_start_time] => 2016-06-10 08:06:00
[prom_end_time] => 2016-06-10 11:06:00
[client_order_id] => 12365478996325412
[job_num] => 4043269
[campaign_wkord_obj_guid] => a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0
[pos_obj_guid] => 096e38c2-55c3-80cf-0778-23f81f1cf2f6
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[client_obj_guid] => 247e893a-3ea4-c544-47b2-f23ff16017c6
[ord_obj_guid] => ae55e034-d7d0-5d13-3dd3-22f99df8ead4
[skus] => Array
(
[0] => Array
(
[job_type_obj_guid] => 8234ca2c-e40e-c48c-befc-7ceac5e9de32
[job_sku] => 3001
[client_sku] =>
[sku_name] => Site Survey
)
[1] => Array
(
[job_type_obj_guid] => a3f60c3b-ad3c-d828-9898-fa200edcd3cd
[job_sku] => 4405
[client_sku] =>
[sku_name] => Home Delivery
)
)
[client_specific_data] =>
)
)
)
The resultant json string is as follows:
{"paperwork_guid":"c5bfe512-908d-c513-5a5e-e3a2fbb5548b","name":"recycle","sections":{"0":"header","1"
:"customer","2":"scope_bullets","3":"product","4":"signature","5":"installer","6":"order_data"},"data"
:{"header":{"image":{"src":"recycle.png","format":"float:left"}},"customer":{"f_name":"TEST ONLY","l_name"
:"NEIMEIER","middle_name":"none","title":"none","address1":"28 OEHMAN BLVD","address2":"","address3"
:null,"zip":"14225","city":"BUFFALO","state_abbrev":"NY","email_address":"DALE.NEIMEIER#INSTALLS.COM"
,"phones":{"alt":"","cell":"","work":"","home":"7165551212"}},"scope_bullets":{"sku_3001":{"bullet_0"
:{"paperwork_bullet_obj_guid":"2ebefa96-6f6b-069e-e194-245d138b9845","ordering":"1","bullet_text":"This
is bullet point #1","child_level":"1","formatting":{"0":{"start_word":"2","num_words":"0","tag":"b"
,"href":""},"1":{"start_word":"2","num_words":"1","tag":"a","href":"http:\/\/www.cnn.com"}}},"bullet_1"
:{"paperwork_bullet_obj_guid":"734db3f4-01a0-b025-9624-cc52a1845dff","ordering":"1","bullet_text":"Sub-point
#1.1","child_level":"2"},"bullet_2":{"paperwork_bullet_obj_guid":"ebf5ef02-906e-2005-e499-27eae2edefe9"
,"ordering":"1","bullet_text":"Sub point #1.1.1","child_level":"3"},"bullet_3":{"paperwork_bullet_obj_guid"
:"447997c1-fd9c-25be-b9bf-39257009fb8b","ordering":"1","bullet_text":"This is bullet point #2","child_level"
:"1"},"bullet_4":{"paperwork_bullet_obj_guid":"5def2d9c-d322-788c-0afe-d13707004b97","ordering":"1","bullet_text"
:"Sub point #2.1","child_level":"2"},"bullet_5":{"paperwork_bullet_obj_guid":"84936151-65a3-bcca-951f-b69ff16d34ec"
,"ordering":"2","bullet_text":"Sub point #2.2","child_level":"2"}},"sku_4405":null},"product":{"0":{"model"
:"Paperwork Test abc123","serial":"777333","weight":"26","size":null,"width":null,"height":"32","num_boxes"
:"1","product_name":null,"sku":null,"model_number":null,"part_number":null,"serial_number":null,"cat_value"
:"Best Buy Equipment","product":null,"product_type":null,"product_size":null,"category":null}},"signature"
:{"date":"2016-06-15","need_customer":true,"need_tech":true,"need_store":false,"need_warehouse":false
},"installer":{"anum":"45455","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","phone":"(317) 519-0481"
,"tech_f_name":null,"tech_l_name":null,"company_name":"ZICO LLC","email_address":"ZICOLLC#GMAIL.COM"
},"order_data":{"assigned_date":"2016-06-07 22:24:47-04","purchased_date":"2016-06-06","creation_date"
:"2016-06-06","install_date":"2016-06-10","install_time":"19:00:00","prescheduled_date":"1969-12-31"
,"prom_start_time":"2016-06-10 08:06:00","prom_end_time":"2016-06-10 11:06:00","client_order_id":"12365478996325412"
,"job_num":"4043269","campaign_wkord_obj_guid":"a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0","pos_obj_guid"
:"096e38c2-55c3-80cf-0778-23f81f1cf2f6","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","client_obj_guid"
:"247e893a-3ea4-c544-47b2-f23ff16017c6","ord_obj_guid":"ae55e034-d7d0-5d13-3dd3-22f99df8ead4","skus"
:{"0":{"job_type_obj_guid":"8234ca2c-e40e-c48c-befc-7ceac5e9de32","job_sku":"3001","client_sku":null
,"sku_name":"Site Survey"},"1":{"job_type_obj_guid":"a3f60c3b-ad3c-d828-9898-fa200edcd3cd","job_sku"
:"4405","client_sku":null,"sku_name":"Home Delivery"}},"client_specific_data":""}}}
It appears as though new line feeds are being inserted randomly throughout the josn string. I am not sure if it is just how firebug is outputting the string, but copying-and-pasting the string into jsonLINT produces errors.
Any help would be appreciated.
I have an players array that already sorted on the basis of points descending order.like this:
$positionArray =
Array
(
[PG] => Array
(
[0] => Array
(
[id] => 1
[player_id] => 31471
[salary] => 10800
[points] => 51.53
[position] => PG
)
[1] => Array
(
[id] => 3
[player_id] => 3223
[salary] => 8000
[points] => 49.53
[position] => PG
)
[2] => Array
(
[id] => 5
[player_id] => 4356
[salary] => 11000
[points] => 45.53
[position] => PG
)
)
[3] => Array
(
[id] => 4
[player_id] => 5654
[salary] => 6000
[points] => 42.53
[position] => PG
)
)
[SF] => Array
(
[0] => Array
(
[id] => 2
[player_id] => 4433
[salary] => 11000
[points] => 45.1
[position] => SF
)
[1] => Array
(
[id] => 6
[player_id] => 7667
[salary] => 10800
[points] => 44
[position] => SF
)
[2] => Array
(
[id] => 9
[player_id] => 4533
[salary] => 8000
[points] => 39.53
[position] => SF
)
)
[SG] => Array
(
[0] => Array
(
[id] => 8
[player_id] => 3245
[salary] => 9000
[points] => 56.53
[position] => SG
)
[1] => Array
(
[id] => 7
[player_id] => 7867
[salary] => 6000
[points] => 54.53
[position] => SG
)
[2] => Array
(
[id] => 15
[player_id] => 4543
[salary] => 7000
[points] => 53.53
[position] => SG
)
)
[2] => Array
(
[id] => 15
[player_id] => 2322
[salary] => 5000
[points] => 51.53
[position] => SG
)
)
[PF] => Array
(
[0] => Array
(
[id] => 11
[player_id] => 7777
[salary] => 4000
[points] => 60.53
[position] => PF
)
[1] => Array
(
[id] => 54
[player_id] => 1232
[salary] => 8000
[points] => 59.3
[position] => PF
)
[2] => Array
(
[id] => 18
[player_id] => 5678
[salary] => 3000
[points] => 54.45
[position] => PF
)
)
[C] => Array
(
[0] => Array
(
[id] => 23
[player_id] => 8906
[salary] => 5000
[points] => 47.53
[position] => C
)
[1] => Array
(
[id] => 22
[player_id] => 9007
[salary] => 4000
[points] => 45.53
[position] => C
)
[2] => Array
(
[id] => 43
[player_id] => 1008
[salary] => 11000
[points] => 43.53
[position] => C
)
)
)
PG,SG,SF,PF,C are the positions of the player.I want an array that contain 2 player of PG position ,2 player of SF position,2 player of SG position,2 player of SF position and only one player of C position.
like this:
Array
(
[PG_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PG
)
[PG_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PG
)
[SG_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SG
)
[SG_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SG
)
[SF_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SF
)
[SF_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => SF
)
[PF_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PF
)
[PF_2] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => PF
)
[C_1] => Array
(
[id] =>
[player_id] =>
[salary] =>
[points] =>
[position] => c
)
)
That's mean I just want 9 player on the basis of maximum points and sum of the player salary should not exceed the limit of 60,000.
Note:Player should not be duplicate and If all the above positions are not in the array that just throw an error.It mean we need all these positions
This is what I did so far:
$lineupTemp = array(
"PG",
"PG",
"SG",
"SG",
"SF",
"SF",
"PF",
"PF",
"C"
);
$lineupCount=array_count_values($lineupTemp);
foreach ($lineupCount as $posss=>$countloop){
if(!isset($positionArray[$posss])){
break;
}
foreach ($this->positionArray[$posss] as $reco_){
for($llll=0;$llll<$countloop;$llll++){
array_push($combineLinupArray,array(
'array_id'=>$posss.'_'.($llll + 1),
'point'=>$this->positionArray[$posss][$llll]['points'],
'salary'=>$this->positionArray[$posss][$llll]['salary'],
'fantasy_id'=>$this->positionArray[$posss][$llll]['id'],
'position'=>$posss,
'key_used'=>$llll
)
);
}
break;
}
}
if(count($combineLinupArray)<9){
//return message that all the positions are not found..
}
After that I am adjusting the salary to iterate the array.
The problem is,It's taking too much time,So how can I make this array?
i've an format array like this
Array (
[0] => stdClass Object ( [subscriptionContractId] => 20878 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-02 03:27:39.000 [endDate] => 2015-07-04 03:27:39.000 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 2 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => 2015-07-03 03:27:39.000 [lastPaymentDate] => 2015-07-02 03:27:39.000 [subscriptionContractHistory] =>Array ( [0] => stdClass Object ( [subscriptionContractId] => 20878 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-07-01 03:27:53.290 ) ) [subscriptionContractLogs] => )
[1] => stdClass Object ( [subscriptionContractId] => 20861 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:31:47.000 [endDate] => 2015-06-30 11:32:05.150 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:31:47.000 [subscriptionContractHistory] => Array ( [0] =>
stdClass Object ( [subscriptionContractId] => 20861 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:31:55.530 ) [1] => stdClass Object ( [subscriptionContractId] => 20861 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:32:05.977 ) ) [subscriptionContractLogs] => )
[2] => stdClass Object ( [subscriptionContractId] => 20860 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:29:37.000 [endDate] => 2015-06-30 11:30:19.887 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:29:37.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:10.267 ) [1] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:20.687 ) ) [subscriptionContractLogs] => )
[3] => stdClass Object ( [subscriptionContractId] => 20859 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:27:33.000 [endDate] => 2015-06-30 11:27:57.683 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:27:33.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:42.173 ) [1] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:58.467 ) ) [subscriptionContractLogs] => )
i want to take this value [subscriptionContractId] => 20859
PFB my code
foreach($getContract as $key =>$value) {
$cancel = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
with that above code only showing single value of record, meanwhile record is around 27 records.
and another code, i try to rectify with initialize array, PFB.
$cancel = array()
foreach($getContract as $key =>$value) {
$cancel[] = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
i need only the value, of the array such as 20879, etc. eventhough i use double loop to extract particular array it only show the last record on 10663.
Please help
You can modify your code as follows (see php documenation about foreach for more background):
$cancel = array();
foreach($getContract as $value) {
$cancel[] = $value->subscriptionContractId;
...
}
This way, in every iteration $value is assigned one value from your getContract array, with it's key just being omitted. Then you can store the value's subscriptionContractId in your $cancel array to do whatever you are planning to do.
Note: briosheje is right about the line:
$this->data['res'] = $cancel;
$this->data['res'] is overwritten in every iteration. As you are overwriting it with the your $cancel array, your code should still work, but your doing unnecessary work here, as your array might be subjected to further change in the next iteration.
My advice would be to move this line after your loop brackets ended, so that your result is saved just once:
...
foreach($getContract as $value) {
...
}
$this->data['res'] = $cancel;
I am using PHP 5.5.2 with MySQL back-end. I have a self-referencing database table from that I am getting an array as below :
Array
(
[0] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => Aaa
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 48
[s_id] => 37
[m_id] =>
[title] => bbbbbbbbbbbbb
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 49
[s_id] => 38
[m_id] =>
[title] => cccccccccc
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 50
[s_id] => 39
[m_id] =>
[title] => ddddddddd dddd
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[2] => Array
(
[id] => 51
[s_id] => 40
[m_id] =>
[title] => eeeeeee eeeeeee.
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 54
[s_id] => 18
[m_id] =>
[title] => fffffffff ffff
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 55
[s_id] => 19
[m_id] =>
[title] => gggggg gggggg
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 56
[s_id] => 20
[m_id] =>
[title] => hhhhhhhhhh hhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
[2] => Array
(
[id] => 57
[s_id] => 21
[m_id] =>
[title] => iiiiiiii iiii
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
[0] => Array
(
[id] => 58
[s_id] => 22
[m_id] =>
[title] => jjjjjjj jjj
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 59
[s_id] => 23
[m_id] =>
[title] => kkkkkkkk kkkk
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 47
[s_id] =>
[m_id] =>
[title] => xxxx xxx xxx
[type] => G
[created_at] => 2014-08-29 06:05:18
[frequency] => d
[start] => 2014-08-29
[time] => 09:00
[children] => Array
(
[0] => Array
(
[id] => 112
[s_id] => 37
[m_id] =>
[title] => lllllllllll llllll
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 113
[s_id] => 38
[m_id] =>
[title] => mmmmmmm mmmm
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 123
[s_id] => 39
[m_id] =>
[title] => nnnnnnnnn hhhhh
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 456
[s_id] => 18
[m_id] =>
[title] => ppppppp pppppp
[type] => Q
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
[0] => Array
(
[id] => 545
[s_id] => 19
[m_id] =>
[title] => qqqqqqqqq qqq
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[time] =>
[children] => Array
(
)
)
[1] => Array
(
[id] => 435
[s_id] => 20
[m_id] =>
[title] => ssssssssss sssssssssss
[type] => A
[created_at] => 2014-08-29 06:05:18
[frequency] =>
[start] =>
[children] => Array
(
)
)
)
)
)
)
)
There might be N nesting levels in this array.
Now for each array element, if ['type'] is G then I want to remove keys [s_id] and [m_id] and for any other type I want to remove keys [start], [end] and [time].
Edit : I have tried using a recursive function but it is not giving the desired result.
private function getarray(array &$arr){
foreach ($arr as $item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
foreach ($item["children"] as $c)
{
$child = array();
getarray($child);
}
}
}
How can I achieve this?
Thanks.
function filterMyArray($arr){
if(isset($arr['type'] && $arr['type'] == 'G'){
unset($arr['s_id']);
unset($arr['m_id']);
}
else if(isset($arr['type']){
unset($arr['start']);
unset($arr['end']);
unset($arr['time']);
}
if(isset($arr['children'])
$arr['children'] = filterMyArray($arr['children']);
return $arr;
}
This function is not tested, I wrote it from memory.
But seriously, Googling something like that doesn't hurt. It would take you 5 minutes to get all the required knowledge to do this...
Resolved it myself :
private function getarray(array &$arr){
foreach ($arr as &$item)
{
switch ($item['type'])
{
case 'G' :
unset($item['s_id'], $item['m_id']);
break;
default :
unset($item['start'], $item['end'], $item['time']);
break;
}
if (is_array($item['children']) && sizeof($item['children']) > 0){
getarray($item['children']);
}
}
}
And this is working fine. I have posted this answer to help other people who are novice to php and trying to overcome their problem.
And this is a slap to the jealous people who did not helped me but asked "What did you tried so long?" with a superiority complex attitude. I did it myself.