John Davidson

PHP multi array search and assign value to variable

0 comments
Message:


I have searched the forums up and down, tested as much as I can and still cannot find an answer to this problem. I need to assign a value to the following variables:


$custID           = contactId;
$custFieldCityID = [customFieldValues][x][customFieldId]
$custFieldCity = [customFieldValues][x][value][x]
$custFieldStateID = [customFieldValues][x][customFieldId]
$custFieldState = [customFieldValues][x][value][x]

The order of the customFieldValues always changes (city can be in position 2, next array is in position 0, etc) and the customFieldId value is always different.


Here is the array I am getting (cannot be changed):


Array
(
[contactId] => V
[name] => Mrs Mder
[email] => [email protected]
[customFieldValues] => Array
(
[0] => Array
(
[customFieldId] => I
[name] => city
[value] => Array
(
[0] => Fairfax
)

[values] => Array
(
[0] => Fairfax
)

[type] => text
[fieldType] => text
[valueType] => string
)

[1] => Array
(
[customFieldId] => 8
[name] => postal_code
[value] => Array
(
[0] => 22032
)

[values] => Array
(
[0] => 22032
)

[type] => text
[fieldType] => text
[valueType] => string
)

[2] => Array
(
[customFieldId] => M
[name] => state
[value] => Array
(
[0] => VA
)

[values] => Array
(
[0] => VA
)

[type] => text
[fieldType] => text
[valueType] => string
)

[3] => Array
(
[customFieldId] => V
[name] => street
[value] => Array
(
[0] => 123 Holden Street
)

[values] => Array
(
[0] => 123 Holden Street
)

[type] => text
[fieldType] => text
[valueType] => string
)

[4] => Array
(
[customFieldId] => s
[name] => phone
[value] => Array
(
[0] => +11234567890
)

[values] => Array
(
[0] => +11234567890
)

[type] => text
[fieldType] => text
[valueType] => phone
)

)
)

So far I have tried this:


foreach($returncustdata as $row) {
foreach($row as $k) {
$fieldID= $k['customFieldValues']['customFieldId'];
$fieldname = $k['customFieldValues']['name'];
$fieldvalue = $k['customFieldValues']['value'][0];
}
}


My Answer:
12345,
[customFieldValues] = Array(
[0] = Array(
[customFieldId] = 1,
[value] = Array(
[0] = "New York"
)
),
[1] = Array(
[customFieldId] = 2,
[value] = Array(
[0] = "NY"
)
)
)
)

To assign the values to the variables, you can loop through the customFieldValues array and check the customFieldId to determine which variable to assign the value to. Here's an example code snippet to achieve this:

php
$custID = $array['contactId'];

foreach ($array['customFieldValues'] as $customField) {
if ($customField['customFieldId'] == 1) {
$custFieldCityID = $customField['customFieldId'];
$custFieldCity = $customField['value'][0];
} elseif ($customField['customFieldId'] == 2) {
$custFieldStateID = $customField['customFieldId'];
$custFieldState = $customField['value'][0];
}
}

echo "custID: $custID\n";
echo "custFieldCityID: $custFieldCityID\n";
echo "custFieldCity: $custFieldCity\n";
echo "custFieldStateID: $custFieldStateID\n";
echo "custFieldState: $custFieldState\n";


This code will assign the values from the customFieldValues array to the variables based on the customFieldId. You can adjust the conditions in the loop to handle different customFieldIds as needed.

Rate this post

4 of 5 based on 8337 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us