php


Multidimensional array update


I am trying to update each language as a multidimensional array to another multidimensional array but it seems to be only saving the last key e.g.(lang_3) but not lang_1 & lang_2. Cracking my head to figure this out. Hope someone can point out my faults. ($country_specs = list of language, $get_code = country code)
$awards = array(
'award_year' => sanitize_array_text_field($_POST['award_year']),
'award_title_user' => sanitize_array_text_field($_POST['award_title_user']),
'award_description_user' => sanitize_array_text_field($_POST['award_description_user'])
);
foreach ($country_specs as $specs => $value) {
if ($value[0] == $get_code ) {
foreach ($value['lang'] as $lang_key => $lang) {
$awards_title = 'award_title_'.$lang_key;
$awards_description = 'award_description_'.$lang_key;
$awards_lang = array(
$awards_title => sanitize_array_text_field($_POST[$awards_title]),
$awards_description => sanitize_array_text_field($_POST[$awards_description])
);
update_user_meta($user_id, 'awards', array_merge($awards,$awards_lang));
}
}
}
Current code output example:
Array (
[award_year] => Array (
[0] => 1999-01
[1] => 2010-02 )
[award_title_user] => Array (
[0] => 2
[1] => tt )
[award_description_user] => Array (
[0] => 2
[1] => ddd )
[award_title_lang3] => Array (
[0] => 2CC
[1] => zz )
[award_description_lang3] => Array (
[0] => 2CCCCCCC
[1] => dzz ) )
Working code as follows.
$awards = array(
'award_year' => sanitize_array_text_field($_POST['award_year']),
'award_title_user' => sanitize_array_text_field($_POST['award_title_user']),
'award_description_user' => sanitize_array_text_field($_POST['award_description_user'])
);
$awards_new_lang = array();
foreach ($country_specs as $specs => $value) {
if ($value[0] == $get_code ) {
foreach ($value['lang'] as $lang_key => $lang) {
$awards_title = 'award_title_'.$lang_key;
$awards_description = 'award_description_'.$lang_key;
$awards_new_lang[$awards_title] = sanitize_array_text_field($_POST[$awards_title]);
$awards_new_lang[$awards_description] = sanitize_array_text_field($_POST[$awards_description]);
}
}
}
$array_merge_new = array_merge($awards, $awards_new_lang);
update_user_meta($user_id, 'awards', $array_merge_new);
I created a new array ($awards_new_lang) and did an array merge with the old array, thus combining both of the arrays together.
Try this code, it outputs as expected, I tried the mimic the variables structures
<?php
$awards = array(
'award_year' => '1999',
'award_title_user' => '2',
'award_description_user' => '2CCCCCCC'
);
$value = array();
$value['lang'] = array(1, 2, 3);
foreach ($value['lang'] as $lang_key) {
$awards_title = 'award_title_'.$lang_key;
$awards_description = 'award_description_'.$lang_key;
$awards_lang = array(
$awards_title => "$lang_key title",
$awards_description => "$lang_key desc"
);
//array merge returns an array, we save the changes here to use them later when the loops are through
$awards = array_merge($awards,$awards_lang);
}
echo '<pre>';
//Final updated version of the array
var_dump($awards);
echo '</pre>';
?>
Outputs:
array(9) {
["award_year"]=>
string(4) "1999"
["award_title_user"]=>
string(1) "2"
["award_description_user"]=>
string(8) "2CCCCCCC"
["award_title_1"]=>
string(7) "1 title"
["award_description_1"]=>
string(6) "1 desc"
["award_title_2"]=>
string(7) "2 title"
["award_description_2"]=>
string(6) "2 desc"
["award_title_3"]=>
string(7) "3 title"
["award_description_3"]=>
string(6) "3 desc"
}

Related Links

php loop through results without using the native driver
Unable to Connect to GitHub.com For Cloning in ffmpeg installation
My Login system PHP is not working and logging in
PHP Issue with Contact Form
While loop does not finish in PHP/MySQL
change num key to string key [PHP]
Top view post is not showing with featured image in wordpress
Get output of a php file running in background
submit null values in Twitter bootstrap Datepicker
phpOCR is not working properly
How to extract data with Goutte Crawler?
Override few php.ini settings on hosting server without changing rest of the settings
Prepend an item at the beginning of each element of an Eloquent collection of Laravel
PHP MySQL Multi User Login on Single UserID e.g. Corporate account with five usernames all accessing the same data
Migrate to php55 on GAE using mysqli
PDO order by DESC with PHP

Categories

HOME
php
hpoo
ionic2
android-studio
bots
kivy
histogram
gallery
moodle-api
elk-stack
turbojpeg
ipv6
minecraft
cqrs
buffer
webstore
distributed-computing
global-variables
temperature
android-contacts
playframework-2.0
jaspersoft-studio
pymongo
acl
firemonkey-style
raima
data-collection
prompt
onchange
gravity-forms-plugin
acrobat
ccavenue
audit
quick-nimble
series
kendo-listview
payeezy
markov-chains
spring-rabbitmq
assemblies
praat
microkernel
suds
nomad
htmlspecialchars
clrs
email-forwarding
alertify
zend-server
degrees
hmmlearn
fitbit
finder
qtextedit
boost-compute
portal
s-function
wininet
android-4.2-jelly-bean
wicked-pdf
noclassdeffounderror
maximize
psd
jsr363
onepage-checkout
pack
openshift-cartridge
apple
gql
ngcordova
domino-designer-eclipse
incron
ecos
nidaqmx
android-2.3-gingerbread
forceclose
unrealscript
stty
quickgraph
insertion
django-filebrowser
multiscaleimage
stretch
acpi
kolite
drawimage
haiku
snackjs
pyjamas
mongrel
dm
3270
motif
webclient.uploaddata

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile