Get Value from an array with foreach - php

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;

Related

Accessing second array (non object) in decoded JSON

I'm trying to access the second array (contacts) in this decoded json to grab the ID next to first_name (the two ID keys could be different), but the second array doesn't seem to be an object so I can't find a loop which can acces it:
stdClass Object (
[data] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[name] => test test
[display_name] => test test
[balance] => 0
[paid_to_date] => 0
[updated_at] => 1578494555
[archived_at] =>
[address1] => Street
[address2] =>
[city] => Town
[state] => State
[postal_code] => Code
[country_id] => 0
[work_phone] => Number
[private_notes] =>
[public_notes] =>
[last_login] =>
[website] =>
[industry_id] => 0
[size_id] => 0
[is_deleted] =>
[payment_terms] => 30
[vat_number] =>
[id_number] =>
[language_id] => 0
[currency_id] => 0
[custom_value1] =>
[custom_value2] =>
[invoice_number_counter] => 1
[quote_number_counter] => 1
[task_rate] => 0
[shipping_address1] =>
[shipping_address2] =>
[shipping_city] =>
[shipping_state] =>
[shipping_postal_code] =>
[shipping_country_id] => 0
[show_tasks_in_portal] => 1
[send_reminders] => 1
[credit_number_counter] => 1
[custom_messages] => {}
[contacts] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[first_name] => test
[last_name] => test
[email] => myemail#me.com
[contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1
[updated_at] => 1578494555
[archived_at] =>
[is_primary] => 1
[phone] => 07919446174
[last_login] =>
[send_invoice] => 1
[custom_value1] =>
[custom_value2] =>
)
)
)
)
[meta] => stdClass Object (
[pagination] => stdClass Object (
[total] => 1
[count] => 1
[per_page] => 15
[current_page] => 1
[total_pages] => 1
[links] => Array ( )
)
)
)
This is what I've tried, but it doesn't find anything:
$person = getclient($itemid);
$person_data = json_decode($person);
foreach ($person_data->contacts as $key => $item)
{
$itemid = $item->id . "<br />";
}
$person_data contains the data property, which is an array of objects.
foreach ($person_data->data as $person) {
foreach ($person->contacts as $contact) {
echo $contact->id . '<br>';
}
}

PHP5.3 json_encode is outputting bad json

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.

Array of array values

I am unable to get stores and store_id from this array. Please help me, how to get stores and store_id from this array of arrays?
Array
(
[group_data] => Uni_Banner_Model_Bannergroup Object
(
[_eventPrefix:protected] => core_abstract
[_eventObject:protected] => object
[_resourceName:protected] => banner/bannergroup
[_resource:protected] =>
[_resourceCollectionName:protected] => banner/bannergroup_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[group_id] => 4
[group_name] => list banner1
[group_code] => list_banner1
[banner_width] => 320
[banner_height] => 460
[animation_type] => 1
[banner_effects] => Fade/Appear
[pre_banner_effects] =>
[banner_ids] => 1
[show_title] => 0
[show_content] => 0
[link_target] => 0
[status] => 1
[created_time] => 2015-03-04 14:47:40
[update_time] => 2015-03-04 14:47:40
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array
(
[group_id] => 4
[group_name] => list banner1
[group_code] => list_banner1
[banner_width] => 320
[banner_height] => 460
[animation_type] => 1
[banner_effects] => Fade/Appear
[pre_banner_effects] =>
[banner_ids] => 1
[show_title] => 0
[show_content] => 0
[link_target] => 0
[status] => 1
[created_time] => 2015-03-04 14:47:40
[update_time] => 2015-03-04 14:47:40
)
[_idFieldName:protected] => group_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
[banner_data] => Array
(
[0] => Uni_Banner_Model_Banner Object
(
[_eventPrefix:protected] => core_abstract
[_eventObject:protected] => object
[_resourceName:protected] => banner/banner
[_resource:protected] =>
[_resourceCollectionName:protected] => banner/banner_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[banner_id] => 1
[title] => List Banner
[filename] => custom/banners/File-1440417903.jpg
[link] => http://localhost/magentonew/french/
[banner_content] =>
[stores] => 4,6
[storeviews] =>
[status] => 1
[sort_order] => 0
[banner_type] => 0
[created_time] => 2015-08-25 16:44:46
[update_time] => 2015-08-25 16:44:46
[store_id] => Array
(
[0] => 4
[1] => 6
)
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array
(
[banner_id] => 1
[title] => List Banner
[filename] => custom/banners/File-1440417903.jpg
[link] => http://localhost/magentonew/french/
[banner_content] =>
[stores] => 4,6
[storeviews] =>
[status] => 1
[sort_order] => 0
[banner_type] => 0
[created_time] => 2015-08-25 16:44:46
[update_time] => 2015-08-25 16:44:46
[store_id] => Array
(
[0] => 4
[1] => 6
)
)
[_idFieldName:protected] => banner_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
)
)
This is the code get the data for this specific array and objects. You should really use some getter methods for the objects and iterate the array(s) properly.
$arr = your array
$arr['banner_data'][0]->_data['stores'] and
$arr['banner_data'][0]->_data['store_id'][0]
$arr['banner_data'][0]->_data['store_id'][1]

cakePHP multidimensional array loop back to previous push

I have created an array which I need to show a list of projects for the current user. Each project is made up of stages, each stage can have multiple KPIs assigned to it.
As you can see the stages are appended to the array fine and for the most part so are the KPIs.
The exception to this is with KPIs that are not stage specific. When I have this type of KPI is should be assigned to the project section, as it spans multiple stages.
KPIs are assigned to stages by the PSID, if the id is null then it is a project wide KPI.
The issue I'm having is with the project wide KPIs. They are repeated, I need each KPI to appear once in the array.
I've tried to check the previously pushed array element with the currently pushed one in order to check.
$i=0;
foreach($projects as $project){
$projectStages = $this->ProjectStages->find('all',
array(
'conditions' => array(
'pid' => $project['Projects']['pid']
)
)
);
array_push($projects[$i], $projectStages);
$n=0;
foreach($projectStages as $stage){
$projectKPIs = $this->ProjectKPIs->find('all',
array(
'conditions' => array(
'nPid' => $project['Projects']['pid']
)
)
);
foreach($projectKPIs as $projectKPI){
if(empty($projectKPI['ProjectKPIs']['nPsid'])){
$N = $n-1;
$curpkid = $projectKPI['ProjectKPIs']['pkid'];
if(isset($projects[$i]['Projects'][$N]['ProjectKPIs']['pkid'])){
$prevpkid = $projects[$i]['Projects'][$N]['ProjectKPIs']['pkid'];
}else{
$prevpkid = null;
}
if($curpkid != $prevpkid){
echo 'cur: '.$curpkid.' - prev: '.$prevpkid;
array_push($projects[$i]['Projects'], $projectKPI);
}
}else if($projectKPI['ProjectKPIs']['nPsid'] == $stage['ProjectStages']['psid']){
array_push($projects[$i][$i][$n]['ProjectStages'], $projectKPI);
}
}
$n++;
}
$i++;
}
Below is the code I'm using and the array I'm currently getting back:
Array
(
[0] => Array
(
[Projects] => Array
(
[pid] => 811
[name] => Kpi Project
[description] => This Project Has Kpis
[scheduledStartDate] => 2015-05-01
[scheduledEndDate] => 2015-06-01
[actualStartDate] =>
[actualEndDate] =>
[pmid] => 1137
[0] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 37
[nPsid] =>
[type] => 1
[start] => 2015-05-03
[end] => 2015-05-09
[nPid] => 811
[nKid] => 7
)
)
[1] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 37
[nPsid] =>
[type] => 1
[start] => 2015-05-03
[end] => 2015-05-09
[nPid] => 811
[nKid] => 7
)
)
[2] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 37
[nPsid] =>
[type] => 1
[start] => 2015-05-03
[end] => 2015-05-09
[nPid] => 811
[nKid] => 7
)
)
[3] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 37
[nPsid] =>
[type] => 1
[start] => 2015-05-03
[end] => 2015-05-09
[nPid] => 811
[nKid] => 7
)
)
[4] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 37
[nPsid] =>
[type] => 1
[start] => 2015-05-03
[end] => 2015-05-09
[nPid] => 811
[nKid] => 7
)
)
)
[pm] => Array
(
[first_name] => Mr
[last_name] => A
)
[0] => Array
(
[0] => Array
(
[ProjectStages] => Array
(
[psid] => 99
[pid] => 811
[stageID] => 1
[name] => 1
[description] => 1
[label] => 1
[actualStartDate] => 0000-00-00
[scheduledStartDate] => 2015-05-01
[actualEndDate] => 0000-00-00
[scheduledEndDate] => 2015-05-08
[prevStage] => n
[keyStage] => n
[0] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 32
[nPsid] => 99
[type] => 0
[start] =>
[end] =>
[nPid] => 811
[nKid] => 2
)
)
)
)
[1] => Array
(
[ProjectStages] => Array
(
[psid] => 100
[pid] => 811
[stageID] => 2
[name] => 2
[description] => 2
[label] => 2
[actualStartDate] => 0000-00-00
[scheduledStartDate] => 2015-05-08
[actualEndDate] => 0000-00-00
[scheduledEndDate] => 2015-05-15
[prevStage] => n
[keyStage] => n
[0] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 33
[nPsid] => 100
[type] => 0
[start] =>
[end] =>
[nPid] => 811
[nKid] => 4
)
)
)
)
[2] => Array
(
[ProjectStages] => Array
(
[psid] => 101
[pid] => 811
[stageID] => 3
[name] => 3
[description] => 3
[label] => 3
[actualStartDate] => 0000-00-00
[scheduledStartDate] => 2015-05-15
[actualEndDate] => 0000-00-00
[scheduledEndDate] => 2015-05-22
[prevStage] => n
[keyStage] => n
[0] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 34
[nPsid] => 101
[type] => 0
[start] =>
[end] =>
[nPid] => 811
[nKid] => 5
)
)
)
)
[3] => Array
(
[ProjectStages] => Array
(
[psid] => 102
[pid] => 811
[stageID] => 4
[name] => 4
[description] => 4
[label] => 4
[actualStartDate] => 0000-00-00
[scheduledStartDate] => 2015-05-22
[actualEndDate] => 0000-00-00
[scheduledEndDate] => 2015-05-29
[prevStage] => n
[keyStage] => n
[0] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 35
[nPsid] => 102
[type] => 0
[start] =>
[end] =>
[nPid] => 811
[nKid] => 6
)
)
)
)
[4] => Array
(
[ProjectStages] => Array
(
[psid] => 103
[pid] => 811
[stageID] => 5
[name] => 5
[description] => 5
[label] => 5
[actualStartDate] => 0000-00-00
[scheduledStartDate] => 2015-05-29
[actualEndDate] => 0000-00-00
[scheduledEndDate] => 2015-06-01
[prevStage] => n
[keyStage] => n
[0] => Array
(
[ProjectKPIs] => Array
(
[pkid] => 36
[nPsid] => 103
[type] => 0
[start] =>
[end] =>
[nPid] => 811
[nKid] => 8
)
)
)
)
)
)
[1] => Array
(
[Projects] => Array
(
[pid] => 572
[name] => Sgh
[description] => Dfgh
[scheduledStartDate] => 2015-04-01
[scheduledEndDate] => 2015-05-01
[actualStartDate] =>
[actualEndDate] =>
[pmid] => 3304
)
[pm] => Array
(
[first_name] => Mr
[last_name] => Brown
)
[0] => Array
(
)
)
)
It seems that you are retrieving the same exact database results multiple times. I'm looking at the find in this code block:
foreach($projectStages as $stage){
$projectKPIs = $this->ProjectKPIs->find('all',
array(
'conditions' => array(
'nPid' => $project['Projects']['pid']
)
)
);
// ...
}
It looks to me that you are going to get the same result set for $projectKPIs on each iteration of the foreach $projectStages because the find for ProjectKPIs is using the project id and nothing to do with whatever the current $stage is.
So effectively for every stage you then loop over all the same KPIs again, causing your logic to get convoluted trying to avoid duplicates.
Perhaps this can be simplified by restructuring your foreach loops. I would ditch the outer foreach($projectStages as $stage) and then iterate over all the KPIs only once. Then inside that loop iterate over each stage only when you need to test against the $stage['ProjectStages']['psid'], which may actually be unnecessary at that point.

Store a variable from the array of object php

I have a php variabl $purchase_data which when I did
print_r('purchase_data');
I got the output as
Array
(
[downloads] => Array
(
[0] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
)
[fees] => Array
(
)
[subtotal] => 1
[discount] => 0
[tax] => 0
[price] => 1
[purchase_key] => a8d14e34ba425f9de6afe3ad4809587e
[user_email] => esh#test.com
[date] => 2014-05-11 20:07:22
[user_info] => Array
(
[id] => 1
[email] => eshtest.com
[first_name] => esh
[last_name] =>
[discount] => none
[address] =>
)
[post_data] => Array
(
[edd_email] => esh#test.com
[edd_first] => esh
[edd_last] =>
[edd_phone] => 919995871693
[edd-user-id] => 1
[edd_action] => purchase
[edd-gateway] => sample_gateway
)
[cart_details] => Array
(
[0] => Array
(
[name] => Shirt
[id] => 28
[item_number] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
[item_price] => 1
[quantity] => 1
[discount] => 0
[subtotal] => 1
[tax] => 0
[price] => 1
)
)
[gateway] => sample_gateway
[card_info] => Array
(
[card_name] =>
[card_number] =>
[card_cvc] =>
[card_exp_month] =>
[card_exp_year] =>
[card_address] =>
[card_address_2] =>
[card_city] =>
[card_state] =>
[card_country] =>
[card_zip] =>
)
)
How can i store the value to edd_phone [edd_phone] into variable $mobileNo ?
Sorry for ths kind of a question.But badly need help
If you want to assign the value of [edd_phone] to $mobileNo , use this :
$mobileNo = $purchase_data['post_data']['edd_phone'];
As a side note : Your array is very complexly nested. If I were you, I would avoid such complex arrays.

Categories