Есть массив типа
{
"id": 2,
"firstName": "Тест",
"lastName": "Тест",
"patronymic": "Тест",
"sexId": 101252,
"mobilePhone": "71111111111",
"registrationNumber": {
"id": 6,
"seria": "",
"no": "",
"extraNumber": "",
"issueDate": null,
"closeDate": null,
"manager": "",
"subdivisionCode": "",
"complementaryDocTypeId": null
},
"personRelatives": [
{
"id": 4,
"lastName": "dsv",
"firstName": "sd",
"patronymic": "",
"resident": false,
"passport": {
"id": 43,
"seria": "",
"no": "",
"extraNumber": null,
"issueDate": null,
"closeDate": null,
"manager": "",
"subdivisionCode": "",
"complementaryDocTypeId": null
},
"rnn": "",
"telephone": "",
"email": "",
"workPlace": "",
"address": "",
"birthDate": null,
"collateralRelId": 102147,
"verified": null,
"comments": ""
}
],
"clientRiskStatus": [
{
"id": 5,
"insertDate": 1539244552517,
"userId": 1027,
"comments": "3211",
"valueId": 102184
},
{
"id": 8,
"insertDate": 1540396776913,
"userId": 1027,
"comments": "321",
"valueId": 102184
},
{
"id": 9,
"insertDate": 1540397890848,
"userId": 1027,
"comments": "123",
"valueId": 102184
},
{
"id": 10,
"insertDate": 1540398177463,
"userId": 1027,
"comments": "вшозавц",
"valueId": 102184
},
{
"id": 11,
"insertDate": 1540398590878,
"userId": 1027,
"comments": "авлоавыолз",
"valueId": 102184
},
{
"id": 2,
"insertDate": 1539067291379,
"userId": 1027,
"comments": "Новый клиент",
"valueId": 102185
}
],
"extraFields": [
{
"id": 178,
"key": "placeOfEducation",
"value": "kzt1"
},
{
"id": 179,
"key": "work1",
"value": "workk1"
},
{
"id": 180,
"key": "work2",
"value": null
},
{
"id": 181,
"key": "work3",
"value": null
},
{
"id": 182,
"key": "placeOfEducation1",
"value": "kzt1"
},
{
"id": 183,
"key": "work6",
"value": ""
}]}
Количество extraFields может быть любым. Как найти значение по ключу и изменить его?
Пробовал через
$NEW = null;
$needle1 = "NEW";
foreach($response1['data']['extraFields'] as $t){
if($t['key'] === $needle1){
$t['value'] = "333";
$NEW = $t['value'];
break;
}
}
или
foreach($response1['data']['extraFields'] as $key => $val){
$response1[$key]['work1'] = "010101";
}
Но так и не выходит изменить значение. Подскажите, что делаю не так?
$json = '{
"id": 2,
"firstName": "Тест",
"lastName": "Тест",
"patronymic": "Тест",
"sexId": 101252,
"mobilePhone": "71111111111",
"registrationNumber": {
"id": 6,
"seria": "",
"no": "",
"extraNumber": "",
"issueDate": null,
"closeDate": null,
"manager": "",
"subdivisionCode": "",
"complementaryDocTypeId": null
},
"personRelatives": [
{
"id": 4,
"lastName": "dsv",
"firstName": "sd",
"patronymic": "",
"resident": false,
"passport": {
"id": 43,
"seria": "",
"no": "",
"extraNumber": null,
"issueDate": null,
"closeDate": null,
"manager": "",
"subdivisionCode": "",
"complementaryDocTypeId": null
},
"rnn": "",
"telephone": "",
"email": "",
"workPlace": "",
"address": "",
"birthDate": null,
"collateralRelId": 102147,
"verified": null,
"comments": ""
}
],
"clientRiskStatus": [
{
"id": 5,
"insertDate": 1539244552517,
"userId": 1027,
"comments": "3211",
"valueId": 102184
},
{
"id": 8,
"insertDate": 1540396776913,
"userId": 1027,
"comments": "321",
"valueId": 102184
},
{
"id": 9,
"insertDate": 1540397890848,
"userId": 1027,
"comments": "123",
"valueId": 102184
},
{
"id": 10,
"insertDate": 1540398177463,
"userId": 1027,
"comments": "вшозавц",
"valueId": 102184
},
{
"id": 11,
"insertDate": 1540398590878,
"userId": 1027,
"comments": "авлоавыолз",
"valueId": 102184
},
{
"id": 2,
"insertDate": 1539067291379,
"userId": 1027,
"comments": "Новый клиент",
"valueId": 102185
}
],
"extraFields": [
{
"id": 178,
"key": "placeOfEducation",
"value": "kzt1"
},
{
"id": 179,
"key": "work1",
"value": "workk1"
},
{
"id": 180,
"key": "work2",
"value": null
},
{
"id": 181,
"key": "work3",
"value": null
},
{
"id": 182,
"key": "placeOfEducation1",
"value": "kzt1"
},
{
"id": 183,
"key": "work6",
"value": ""
}]}';
$json = json_decode($json, true);
var_dump($json['extraFields']);
foreach($json['extraFields'] as $key => $val){
if($val['key'] == "work1"){ // находим нужный ключ
$json['extraFields'][$key]['value'] = 'new your value'; // меняем значение на нужное в исходном массиве
}
}
var_dump($json['extraFields']);
Результат
//было
[1]=>
array(3) {
["id"]=>
int(179)
["key"]=>
string(5) "work1"
["value"]=>
string(6) "workk1"
}
// стало
[1]=>
array(3) {
["id"]=>
int(179)
["key"]=>
string(5) "work1"
["value"]=>
string(14) "new your value"
}
Виртуальный выделенный сервер (VDS) становится отличным выбором
Ситуация такая, есть сайт, она находится на обычном хостинге (сервер 1) Я купил впс (сервер 2), чтобы разобраться с поиском через sphinx, настроил,...
У меня есть сайт, который я написал на Yii2Сайт использует очень большую базу данных