'PHP arrays using keynames and 2 loops [duplicate]
I know this question is very basic to you all, and I'm truly sorry for asking, but I'm studying arrays right now and I've been asked to do the following learning from textbook, which I hate.
I need to loop through the first level and get the FIELD_NAME and FIELD_LABEL. Then to loop through again and get all CUSTOM_FIELD_OPTIONS > OPTION_VALUE for each first level array.
My problem is that it fails at the first loop. And I can't get my head around second loops.
Can you provide some guidance please? I need some help on this :(
$getAllCustomFields = $api->getAllCustomFields();
foreach ($getAllCustomFields as $item) {
echo addslashes($item->FIELD_NAME) . ": '" . addslashes($item->FIELD_LABEL) . "',";
}
In the array I need to be able to loop and get
array(46) {
["Secondary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(25) "Secondary_Volunteering__c"
["FIELD_ORDER"]=>
int(2)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(22) "Secondary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(9) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(3)
["OPTION_VALUE"]=>
string(6) "Events"
["OPTION_DEFAULT"]=>
bool(false)
}
[1]=>
array(3) {
["OPTION_ID"]=>
int(14)
["OPTION_VALUE"]=>
string(28) "Flagship Programme"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
["Primary_Volunteering__c"]=>
array(12) {
["FIELD_NAME"]=>
string(23) "Primary_Volunteering__c"
["FIELD_ORDER"]=>
int(3)
["FIELD_FOR"]=>
string(7) "CONTACT"
["FIELD_LABEL"]=>
string(20) "Primary Volunteering"
["FIELD_TYPE"]=>
string(11) "MULTISELECT"
["FIELD_HELP_TEXT"]=>
NULL
["DEFAULT_VALUE"]=>
NULL
["EDITABLE"]=>
bool(true)
["VISIBLE"]=>
bool(true)
["CUSTOM_FIELD_OPTIONS"]=>
array(27) {
[0]=>
array(3) {
["OPTION_ID"]=>
int(87)
["OPTION_VALUE"]=>
string(9) "Animal Me"
["OPTION_DEFAULT"]=>
bool(false)
}
[1]=>
array(3) {
["OPTION_ID"]=>
int(51)
["OPTION_VALUE"]=>
string(18) "Animals inc Plants"
["OPTION_DEFAULT"]=>
bool(false)
}
[2]=>
array(3) {
["OPTION_ID"]=>
int(13)
["OPTION_VALUE"]=>
string(3) "Art"
["OPTION_DEFAULT"]=>
bool(false)
}
[3]=>
array(3) {
["OPTION_ID"]=>
int(21)
["OPTION_VALUE"]=>
string(12) "Bright Stars"
["OPTION_DEFAULT"]=>
bool(false)
}
}
["DEPENDENCY"]=>
NULL
["JOIN_OBJECT"]=>
NULL
}
}
Solution 1:[1]
foreach($getAllCustomFields as $fields){
foreach($fields as $field){
echo( '<strong>'.$field["FIELD_LABEL"].'</strong><br>');
foreach ($field["CUSTOM_FIELD_OPTIONS"] as $cfield){
echo($cfield["OPTION_VALUE"].'<br>');
}
echo('<br>');
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Julie Freeman |
