find key and replace value in php array / how to
Solution 1:
A simple loop would work, but actually it is not even required:
<?php
$data = [
[ 'app_setting_key' => "dh_company_name", '[app_setting_value' => "ABCsss" ],
[ 'app_setting_key' => "dh_address_one", 'app_setting_value' => "123 A big streetsss" ],
[ 'app_setting_key' => "dh_address_two", 'app_setting_value' => "a big city" ],
[ 'app_setting_key' => "dh_address_country", 'app_setting_value' => "JP" ],
[ 'app_setting_key' => "dh_email", 'app_setting_value' => "[email protected]" ],
[ 'app_setting_key' => "dh_phone_country", 'app_setting_value' => "JP" ],
[ 'app_setting_key' => "dh_phone_number", 'app_setting_value' => "80-3245-6000" ]
];
$key = array_search("dh_phone_number", array_column($data, 'app_setting_key'));
$data[$key]['app_setting_value'] = "+81-80-5555-5555";
var_dump($data);
The output obviously is:
array(7) {
[0]=>
array(2) {
["app_setting_key"]=>
string(15) "dh_company_name"
["[app_setting_value"]=>
string(6) "ABCsss"
}
[1]=>
array(2) {
["app_setting_key"]=>
string(14) "dh_address_one"
["app_setting_value"]=>
string(19) "123 A big streetsss"
}
[2]=>
array(2) {
["app_setting_key"]=>
string(14) "dh_address_two"
["app_setting_value"]=>
string(10) "a big city"
}
[3]=>
array(2) {
["app_setting_key"]=>
string(18) "dh_address_country"
["app_setting_value"]=>
string(2) "JP"
}
[4]=>
array(2) {
["app_setting_key"]=>
string(8) "dh_email"
["app_setting_value"]=>
string(19) "[email protected]"
}
[5]=>
array(2) {
["app_setting_key"]=>
string(16) "dh_phone_country"
["app_setting_value"]=>
string(2) "JP"
}
[6]=>
array(2) {
["app_setting_key"]=>
string(15) "dh_phone_number"
["app_setting_value"]=>
string(16) "+81-80-5555-5555"
}
}