How to create an array under an alreay present key in PHP? - php

I've a following array titled $grid_data as follows:
Array
(
[0] => Array
(
[question_id] => 50147
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 219
[question_directions] =>
[question_text] => A wooden scale of length L and mass M is lying on a frictionless table. A particle of mass m moving the velocity v, at a distanced from the centre as shown in Fig. 93 strikes the scale and adherses to it. Which of the follwoing quantity (quantities) is (are) conserved for the system.
[question_solution] =>
[question_file] =>
[question_description] =>
[question_difficulty_type] => 2
[question_has_sub_ques] => 0
[question_picked_individually] => no
[question_appeared_count] => 0
[question_manual] => 0
[question_has_solution] => 0
[question_site_id] =>
[question_created_staff_id] => 4e4078987c90dbf4ee477f480ad1b874
[question_added_date] => 1329728799
[question_updated_staff_id] => 1096ab29ecde5cec198bb2ebe730d229
[question_updated_date] => 1340253386
[que_issue_id] => 1
[reported_site_id] => ENTPRM
[reported_user_type] => student
[reported_user_id] => 395599c5891c1418357e2efa89bc3e27
[que_issue] => Question is wrong,Other
[que_issue_comment] => Happy question
[que_issue_status] => 0
[que_issue_date] => 1394517725
[que_issue_fixed_date] => 0
[question_lock] => 1
[ques_ans] => Array
(
[0] => Array
(
[answer_id] => 198739
[answer_question_id] => 50147
[answer_text] => linear momentum only
[answer_file] =>
[answer_description] =>
[answer_is_right] => 0
)
[1] => Array
(
[answer_id] => 198740
[answer_question_id] => 50147
[answer_text] => angular momentum only
[answer_file] =>
[answer_description] =>
[answer_is_right] => 0
)
[2] => Array
(
[answer_id] => 198741
[answer_question_id] => 50147
[answer_text] => kinetic energy only
[answer_file] =>
[answer_description] =>
[answer_is_right] => 0
)
[3] => Array
(
[answer_id] => 198742
[answer_question_id] => 50147
[answer_text] => linear momentum and angular momentum both
[answer_file] =>
[answer_description] =>
[answer_is_right] => 1
)
)
)
[1] => Array
(
[question_id] => 21679
[question_parent_id] => 0
[question_subject_id] => 5
[question_topic_id] => 285
[question_directions] =>
[question_text] => In an electrical cable there is a single wire of radius 9 mm of copper. Its resistance is �math xmlns=�http://www.w3.org/1998/Math/MathML���mn�5�/mn��mo��nbsp;�/mo��mi��#937;�/mi��/math�. The cable is replaced by 6 different insulated copper wires, the radius of each wire is 3mm. Now the total resistance of the cable will be
[question_solution] =>
[question_file] =>
[question_description] =>
[question_difficulty_type] => 2
[question_has_sub_ques] => 0
[question_picked_individually] => no
[question_appeared_count] => 0
[question_manual] => 0
[question_has_solution] => 0
[question_site_id] =>
[question_created_staff_id] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1326440661
[question_updated_staff_id] => 1096ab29ecde5cec198bb2ebe730d229
[question_updated_date] => 1338368619
[que_issue_id] => 2
[reported_site_id] => ENTPRM
[reported_user_type] => staff
[reported_user_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
[que_issue] => Question is Incomplete/Wrong,Directions Missing
[que_issue_comment] =>
[que_issue_status] => 0
[que_issue_date] => 1395655234
[que_issue_fixed_date] => 0
[question_lock] => 1
[ques_ans] => Array
(
[0] => Array
(
[answer_id] => 573122
[answer_question_id] => 21679
[answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»7«/mn»«mo».«/mo»«mn»5«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
[answer_file] =>
[answer_description] =>
[answer_is_right] => 1
)
[1] => Array
(
[answer_id] => 573123
[answer_question_id] => 21679
[answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»45«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
[answer_file] =>
[answer_description] =>
[answer_is_right] => 0
)
[2] => Array
(
[answer_id] => 573124
[answer_question_id] => 21679
[answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»90«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
[answer_file] =>
[answer_description] =>
[answer_is_right] => 0
)
[3] => Array
(
[answer_id] => 573125
[answer_question_id] => 21679
[answer_text] => «math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mn»270«/mn»«mo»§nbsp;«/mo»«mi»§#937;«/mi»«/math»
[answer_file] =>
[answer_description] =>
[answer_is_right] => 0
)
)
)
)
Now ignore rest of the array and concentrate only on two key elements from the above array which are as follows:
[que_issue] => Question is wrong,Other //From first array
[que_issue] => Question is Incomplete/Wrong,Directions Missing //From second array
Actually I want to convert this comma separated strin into an array under the key [que_issue] for every array element present in an array. I want the output in following manner in above array($grid_data):
/*Oputput for first array element from array `$grid_data`*/
[que_issue] => Array(
[0] => Question is wrong
[1] =>Other
)
/*Oputput for second array element from array `$grid_data`*/
[que_issue] => Array(
[0] => Question is Incomplete/Wrong
[1] => Directions Missing
)
Now can anyone help me in manipulating the array $grid_data in order to have the above output for each array element present in it? Thanks in advance.

Easy enough to foreach and explode:
foreach($grid_data as &$array) {
$array['que_issue'] = explode(',', $array['que_issue']);
}
Or:
foreach($grid_data as $key => $array) {
$grid_data[$key]['que_issue'] = explode(',', $array['que_issue']);
}

Without using reference:
for($i = 0; $i < count($grid_data); $i++) {
$grid_data[$i]['que_issue'] = explode(',', $grid_data[$i]['que_issue']);
}
or foreach() using $key

Related

Removing first array of multidimensional array

I want to remove the first.. how to say that outside of the array and left inside array of a multidimension array. I couldn't found out the solution.. someone knows how to do it??
This is my code. I assign a array and i try to put into another array like this.
$final_sku = array();
foreach($skus as $sku){
foreach($sku as $key => $s){
$final_sku[] = array('Sku' => $s);
}
}
$newArray = array(
"Product" => array(
"PrimaryCategory" => "1",
"AssociatedSku" => "12",
"Attributes" => array(
),
"Skus" => $final_sku
)
);
And this is the $final_sku output be like.
[0] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
)
[1] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
)
I want the output be like this.
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
UPDATE: I want to pass the array and convert to xml. So the array key would be duplicated.
It does not make sense to have the same array of 2 members under the same key.
If you want to access one of them, what will be its uniqueness over the other?
The logical solution is
[sku] => [
0 => [first sku data....],
1 => [sku data....],
]

Multiply data in array

I'm keeping data in Array, I want to multiply these data with each other, but I was not successful. I found the Array_product function but I could not run it successfully. What do you think the problem is?
$kuponlar = $_SESSION['kuponlar'];
$maclar = $kuponlar['maclar'];
The data I want to multiply:
echo array_product($maclar['oran']));
array_product does not work when I want to do. Is there a recommendation that the problem could not find?
Array
(
[maclar] => Array
(
[5ca4fb869b043] => stdClass Object
(
[mac_id] => 881
[mac_kod] => 657
[mac_lig] => HIR
[mac_zaman] => 04.04.2019
[mac_saat] => 19:00
[mac_slug] => rudes/istra/657
[mac_handikap] => 1
[ev_logo] => 12644
[deplasman_logo] => 6776
[iddaa_id] => 1308487
[evsahibi] => Rudes
[deplasman] => Istra
[ulke] => Hırvatistan 1. Ligi
[mac_tarihi] => 04.04.2019 19:00:00
[sonuc] =>
[live] => 0
[mac_uniq] => 5ca4fb869b043
[count] => 1
[tahmin] => Ev Kazanır
[oran] => 2.85
[durum] => 0
)
[5ca4fb869bf78] => stdClass Object
(
[mac_id] => 882
[mac_kod] => 658
[mac_lig] => HOL
[mac_zaman] => 04.04.2019
[mac_saat] => 19:30
[mac_slug] => psv/zwolle/658
[mac_handikap] => -1
[ev_logo] => 2836
[deplasman_logo] => 2869
[iddaa_id] => 1306687
[evsahibi] => PSV
[deplasman] => Zwolle
[ulke] => Hollanda Eredivisie Ligi
[mac_tarihi] => 04.04.2019 19:30:00
[sonuc] =>
[live] => 0
[mac_uniq] => 5ca4fb869bf78
[count] => 1
[tahmin] => Ev Kazanır
[oran] => 1.10
[durum] => 0
)
[5ca4fb869cde3] => stdClass Object
(
[mac_id] => 883
[mac_kod] => 660
[mac_lig] => İTA
[mac_zaman] => 04.04.2019
[mac_saat] => 20:00
[mac_slug] => sassuolo/chievo/660
[mac_handikap] =>
[ev_logo] => 665
[deplasman_logo] => 578
[iddaa_id] => 1333704
[evsahibi] => Sassuolo
[deplasman] => Chievo
[ulke] => İtalya Serie A Ligi
[mac_tarihi] => 04.04.2019 20:00:00
[sonuc] =>
[live] => 0
[mac_uniq] => 5ca4fb869cde3
[count] => 1
[tahmin] => Beraberlik
[oran] => 3.20
[durum] => 0
)
)
[summary] => Array
(
[total] => 1
[count] => 0
)
)
You need to multiply the property oran of each object in your array:
$maclar = $kuponlar['maclar'];
$mul = 1;
foreach($maclar as $key => $value){
$mul *= $value->oran;
}
You don't have 'oran' index in $maclar array, at first you should make an array of 'oran' then use array_product on that array.
for example:
foreach($maclar as $key=>$object){
$oran[] = $object->oran;
}
echo array_product($oran);

how to convert data type from result_object codeigniter

I have an array of objects from the query as follows :
Array
(
[0] => stdClass Object
(
[row_number] => 1
[cash_id] => 30938
[closing_id] =>
[is_closed] => 0
[cash_date] => 2018-04-03
[store_id] => 13
[store_code] => 504
[store_account] => ST0013
[store_vendor] => ES0013
[store_dwds] => 01R-0101A006S0013
[store_name] => KARTIKA CHANDRA
[area_id] => 11
[area_code] => A11
[area_name] => Area 11
[region_id] => 1
[region_code] => R01
[region_name] => Region 1
[closing_date] =>
[closing_type] =>
[closing_type_desc] =>
[cash_amount] => 6000000.0000
[closing_amount] =>
[update_time] =>
[update_by_username] =>
[update_by_first_name] =>
[update_by_last_name] =>
[update_by_email] =>
)
[1] => stdClass Object
(
[row_number] => 1
[cash_id] => 30938
[closing_id] =>
[is_closed] => 0
[cash_date] => 2018-04-03
[store_id] => 13
[store_code] => 504
[store_account] => ST0013
[store_vendor] => ES0013
[store_dwds] => 01R-0101A006S0013
[store_name] => KARTIKA CHANDRA
[area_id] => 11
[area_code] => A11
[area_name] => Area 11
[region_id] => 1
[region_code] => R01
[region_name] => Region 1
[closing_date] =>
[closing_type] =>
[closing_type_desc] =>
[cash_amount] => 6000000.0000
[closing_amount] =>
[update_time] =>
[update_by_username] =>
[update_by_first_name] =>
[update_by_last_name] =>
[update_by_email] =>
)
)
how do i change the is_closed object into a boolean data type ?
thanks,
You'll have to cast it to a boolean in your loop when you use it.
array_walk($Result, function ($item) {
$item->is_closed = (bool) $item->is_closed;
});

want to merge arrays like array1[0]+array2[0] using php [duplicate]

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
array1:
Array (
[0] => Array (
[name] => January
[scale_of_pay] => 7800-39000
[pay] => 25000
[grade_pay] => 5000
[d_a] => 5000
[h_r_a] => 5000
[t_a] => 1000
[other] => 5000
[net_claim] => 46000
)
array2:
Array (
[0] => Array (
[national_two_bank] => 700
[bhgylaxshmi_banks] => 1000
[maheshwari_bank] => 1000
[maha_bank] => 1000
[mahesh_cooperative_bank] => 1000
[co_operative] => 1000
[lic] => 1000
[total_deduction] => 6700
)
want answer like :
Array (
[0] => Array (
[name] => January
[scale_of_pay] => 7800-39000
[pay] => 25000
[grade_pay] => 5000
[d_a] => 5000
[h_r_a] => 5000
[t_a] => 1000
[other] => 5000
[net_claim] => 46000
[national_two_bank] => 700
[bhgylaxshmi_banks] => 1000
[maheshwari_bank] => 1000
[maha_bank] => 1000
[mahesh_cooperative_bank] => 1000
[co_operative] => 1000
[lic] => 1000
[total_deduction] => 6700
)
when i merger array using array_merge answer is like,
Array(
[0] => Array(
[name] => January[scale_of_pay] => 7800 - 39000[pay] => 25000[grade_pay] => 5000[d_a] => 5000[h_r_a] => 5000[t_a] => 1000[other] => 5000[net_claim] => 46000
) [1] => Array(
[name] => February[scale_of_pay] => 7800 - 39000[pay] => 25000[grade_pay] => 5000[d_a] => 5000[h_r_a] => 5000[t_a] => 1000[other] => 5000[net_claim] => 46000
) [2] => Array(
[name] => March[scale_of_pay] => 7800 - 39000[pay] => 25000[grade_pay] => 5000[d_a] => 5000[h_r_a] => 5000[t_a] => 5000[other] => 5000[net_claim] => 50000
) [3] => Array(
[national_two_bank] => 700[bhgylaxshmi_banks] => 1000[maheshwari_bank] => 1000[maha_bank] => 1000[mahesh_cooperative_bank] => 1000[co_operative] => 1000[lic] => 1000[total_deduction] => 6700
) [4] => Array(
[national_two_bank] => 1000[bhgylaxshmi_banks] => 1000[maheshwari_bank] => 1000[maha_bank] => 1000[mahesh_cooperative_bank] => 1000[co_operative] => 1000[lic] => 1000[total_deduction] => 7000
) [5] => Array(
[national_two_bank] => 1000[bhgylaxshmi_banks] => 1000[maheshwari_bank] => 1000[maha_bank] => 1000[mahesh_cooperative_bank] => 1000[co_operative] => 1000[lic] => 1000[total_deduction] => 7000
)
)
i dont wants like this...
For a solution that works with arrays with more than one inner value, try :
array_map('array_merge', $a1, $a2);
Here's an example https://repl.it/BRdQ
You want to use the array_merge function. In your specific example it would be as easy as array_merge($array1[0], $array2[0])
Here is an example
<?php
$array1 = array( array( 'test' => 'testing1', 'test2' => 'testing2' ) );
$array2 = array( array( 'test3' => 'testing3', 'test4' => 'testing4' ) );
print_r( array_merge( $array1[0], $array2[0] ) );
?>
This outputs
Array ( [test] => testing1 [test2] => testing2 [test3] => testing3
[test4] => testing4 )

CakePHP - validation returning false

validates() getting failed, but the validationErrors is empty in CakePHP 2.x, even I have user unlockedActions for this method but still getting this error, while inserting its working fine. Only edit is not working.
This my controller code:
if (!empty($this->data['UserProfileName']['new']['new_profile_id'])){
// $this->data['UserProfileName']['old_profile_id'] = $this->data['UserProfileName']['profile_id'];
$new_data['UserProfileName']['profile_id'] = $this->data['UserProfileName']['new']['new_profile_id'];
$new_data['UserProfileName']['user_profile_id']=$userProfileId;
$this->UserProfileName->create();
$this->UserProfileName->set($new_data);
}
$this->UserProfileContact->set($this->data);
if ($this->UserProfileName->validates() && $this->UserProfileContact->validates()) {
} else {
$errors = $this->UserProfileName->validationErrors;
$errors = $this->UserProfileContact->validationErrors;
pr($this->UserProfile->validationErrors);
pr($errors);
}
The form input $this->data is:
Array
(
[UserProfile] => Array
(
[id] => 1
[expiry_date] => 2017-06-30
[is_remarriage] => 0
[is_featured] => 1
[featured_date] => 2015-06-30
[featured_expity_date] => 2015-07-15
[language_id] => 1
[name] => Profile1
[gender_id] => 1
[date_of_birth] => 1988-06-30
[time_of_birth] => Array
(
[hour] => 05
[min] => 11
[meridian] => pm
)
[place_of_birth] => Trichy
[religion_id] => 1
[caste_id] => 3
[sub_caste_id] => 2
[community_id] => 3
[gothram_id] => 3
[star_id] => 5
[rasi_id] => 5
[food_habbit_id] => 2
[height] => 145
[height_type_id] => 1
[weight] => 50
[complexion_id] => 2
[blood_group_id] => 7
[disability_id] =>
[other_disability] =>
[talents] => Singing
[hobbies] => Handicrafts
[languages_konwn] => Tamil, English,Hindi
[qualification_id] => 1
[other_qualification] =>
[employment_type_id] => 5
[job_details] =>
[income] =>
[income_type_id] => 1
[parents_alive_type_id] => 2
[father_name] => Fname
[father_job] => Business
[mother_name] => Mname
[mother_job] => house wife
[mother_caste_id] => 3
[brothers_elder] => 2
[brothers_younger] =>
[brothers_married] => 1
[sisters_elder] =>
[sisters_younger] =>
[sisters_married] =>
[family_status_id] => 2
[marital_status_id] =>
[date_of_marriage] =>
[date_of_divorce] =>
[lived_together_duration] =>
[cilidrean_details] =>
[other_info] =>
[is_show_photo] => 1
)
[UserProfileName] => Array
(
[0] => Array
(
[id] => 1
[profile_id] => Profile1001
)
[new] => Array
(
[new_profile_id] =>
)
)
[PropertyType] => Array
(
[PropertyType] => Array
(
[0] => 1
[1] => 4
)
)
[UserProfileExpectation] => Array
(
[id] => 1
[qualification_id] => 2
[job_requirement_type_id] => 1
[preferred_place_of_job] => With in Tamil nadu
[min_income] => 900000/ann
[caste_id] => 3
[sub_caste_id] => 1
[community_id] => 1
[food_habbit_id] => 2
[marital_status_id] => 1
[is_horoscope_matching_required] => 1
[is_accept_disablity] => 0
[other_expectation] =>
)
[UserProfileHoroscopeDetail] => Array
(
[id] => 1
[balance_dasa_name] => sun
[balance_dasa_years] => 12
[balance_dasa_months] => 9
[balance_dasa_days] => 25
[rasi_1] => sss
[rasi_2] => hggf
[rasi_3] => ffgd
[rasi_4] =>
[rasi_12] =>
[rasi_5] =>
[rasi_11] =>
[rasi_6] => dfgd
[rasi_10] =>
[rasi_9] => fgdfd
ghg
fgf
[rasi_8] =>
[rasi_7] =>
[amsam_1] =>
[amsam_2] => fg
[amsam_3] =>
[amsam_4] =>
[amsam_12] => fghfg
[amsam_5] => fghfg
[amsam_11] => fgf
[amsam_6] =>
[amsam_10] =>
[amsam_9] =>
[amsam_8] => fghf
[amsam_7] =>
)
[UserProfileContact] => Array
(
[email] => jayashree+profile25#jbcs.co.in
[permanent_address] => fhgfhg
[permanent_country_id] => 1
[permanent_zone_id] => 1
[permanent_city_id] => 1
[local_address] =>
[local_country_id] => 1
[local_zone_id] => 1
[local_city_id] => 1
[phone_1] => 2134657898
[phone_2] =>
[phone_3] =>
[phone_4] =>
)
[images] => Array
(
[1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[3] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
)`
It always goes to else part, but when I print the error array it's empty.
Try this and modify according your requirement.
if(!empty($this->data))
{
$this->User->set( $this->data['User'] );
$this->Contractor->set( $this->data['Contractor'] );
if ($this->User->validates() && $this->Contractor->validates())
{
$this->data['User']['passwd'] = $this->Auth->password($this->data['User']['new_passwd']);
$this->data['User']['created'] = date("Y-m-d H:i:s");
$user = $this->data['User'];
$contractor = $this->data['Contractor'];
//pr($this->data);die;
if( $this->User->save( $user ) )
{
$newId = $this->User->getLastInsertId();
$contractor['user_id'] = $newId;
if($this->Contractor->save($contractor))
{
$this->redirect('/contractors/index') ;
}
}
}else{
$this->validateErrors($this->User);
$this->validateErrors($this->Contractor);
$this->render();
}
}
I have solved the issue by change the validation rule
in validation rule i've used
'required' => 'create',
and changed to
'on' => 'create',
now its working.

Categories