1
我有以下數據:遍歷所有對象,直到父插入,然後插入自
Array
(
[form_field_id] => CbIXvKxy2camTsM
[form_id] => IHwNBRTV4QcSu0Z
[field_id] => 0GW8GjlF7qufIJn
[parent_field_id] => xu7SJU9aUGu52FD
[list_type_id] =>
[is_child] => 1
[state] => 1
[active] => 1
[created_at] => 2016-02-12 22:24:28
[updated_at] => 2016-02-12 22:24:28
[field_type_id] =>
[position_in_form] => 0
[required] => 0
[multiple_select] => 0
[item_language_id] => vb8EYue2gwTLmUV
[item_id] => 0GW8GjlF7qufIJn
[language_id] => i4yZDXRuljgoUEU
[item_language_type_id] => TLajukarXC1YZhq
[item_name] =>
[item_description] =>
[item_help] =>
[item_placeholder] =>
[item_extra_info] =>
)
Array
(
[form_field_id] => l28it3KmuLT9LdI
[form_id] => IHwNBRTV4QcSu0Z
[field_id] => S8xouMqf16S6FvJ
[parent_field_id] => xu7SJU9aUGu52FD
[list_type_id] =>
[is_child] => 1
[state] => 1
[active] => 1
[created_at] => 2016-02-12 22:24:28
[updated_at] => 2016-02-12 22:24:28
[field_type_id] =>
[position_in_form] => 0
[required] => 0
[multiple_select] => 0
[item_language_id] => EHh7WCgd92GjGYx
[item_id] => S8xouMqf16S6FvJ
[language_id] => i4yZDXRuljgoUEU
[item_language_type_id] => TLajukarXC1YZhq
[item_name] =>
[item_description] =>
[item_help] =>
[item_placeholder] =>
[item_extra_info] =>
)
Array
(
[form_field_id] => maDMHwyOCBQBYDP
[form_id] => IHwNBRTV4QcSu0Z
[field_id] => xu7SJU9aUGu52FD
[parent_field_id] => s8RX7J79v0K4rKq
[list_type_id] =>
[is_child] => 1
[state] => 1
[active] => 1
[created_at] => 2016-02-12 22:24:28
[updated_at] => 2016-02-12 22:24:28
[field_type_id] =>
[position_in_form] => 0
[required] => 0
[multiple_select] => 0
[item_language_id] => l6FurGRaO0xKIe0
[item_id] => xu7SJU9aUGu52FD
[language_id] => i4yZDXRuljgoUEU
[item_language_type_id] => TLajukarXC1YZhq
[item_name] =>
[item_description] =>
[item_help] =>
[item_placeholder] =>
[item_extra_info] =>
)
Array
(
[form_field_id] => oEcBTJ0EeLs8Lqd
[form_id] => IHwNBRTV4QcSu0Z
[field_id] => DQnb5frKCbtpD6l
[parent_field_id] => -1
[list_type_id] =>
[is_child] => 0
[state] => 1
[active] => 1
[created_at] => 2016-02-12 22:24:28
[updated_at] => 2016-02-12 22:24:28
[field_type_id] =>
[position_in_form] => 0
[required] => 0
[multiple_select] => 0
[item_language_id] => 9Ggrn5w8Vyox8i6
[item_id] => DQnb5frKCbtpD6l
[language_id] => i4yZDXRuljgoUEU
[item_language_type_id] => TLajukarXC1YZhq
[item_name] =>
[item_description] =>
[item_help] =>
[item_placeholder] =>
[item_extra_info] =>
)
Array
(
[form_field_id] => Os7ltKzO4qJDQUB
[form_id] => IHwNBRTV4QcSu0Z
[field_id] => s8RX7J79v0K4rKq
[parent_field_id] => DQnb5frKCbtpD6l
[list_type_id] =>
[is_child] => 1
[state] => 1
[active] => 1
[created_at] => 2016-02-12 22:24:28
[updated_at] => 2016-02-12 22:24:28
[field_type_id] =>
[position_in_form] => 0
[required] => 0
[multiple_select] => 0
[item_language_id] => qz82stTcc4g92Ya
[item_id] => s8RX7J79v0K4rKq
[language_id] => i4yZDXRuljgoUEU
[item_language_type_id] => TLajukarXC1YZhq
[item_name] =>
[item_description] =>
[item_help] =>
[item_placeholder] =>
[item_extra_info] =>
)
我建立一個表單生成器,數據已經保存到數據庫中,但試圖提取它是現在是一個使命。每個數組表示一個字段。
正如您所見,有一個is_child
字段,如果它是0,字段是main field
/main question
。如果它是1,那麼它是一個sub-field
/sub-question
。
我需要遍歷它並構建字段的層次結構。
所以我試圖得到它的循環是這樣的:
DQnb5frKCbtpD6l : {
s8RX7J79v0K4rKq : {
xu7SJU9aUGu52FD : {
0GW8GjlF7qufIJn : {}
}
}
},
...
有可能在每個級別以上的項目,因此這將是有效的:
DQnb5frKCbtpD6l : {
s8RX7J79v0K4rKq : {
xu7SJU9aUGu52FD : {
0GW8GjlF7qufIJn : {},
a : {}
},
b : {}
},
c : {}
},
...
什麼最有效的方法來做到這一點?我目前正在:
foreach ($fields as $field) {
// $field is an object, I printed out $field->toArray() to get the data above
$this->loopThrough($field, $form_structure);
}
private function loopThrough($field, &$form_structure) {
// check if is parent
if($field->is_child == 0) {
$form_structure[$field->field_id] = [];
return true;
}
// check if is child of current level
if(array_key_exists($field->parent_field_id, $form_structure)) {
$form_structure[$field->parent_field_id][$field->field_id] = [];
return true;
}
// check if is grandchild
foreach($form_structure as $key => $value) {
if($key == $field->parent_id || array_key_exists($field->parent_id, $value)) {
$value[] = $field->field_id;
} else {
$this->loopThrough($field, $value);
}
}
}
我認爲正在發生的是什麼:
當通過對象循環,當我檢查ID,該parent_field_id
可能不存在/插入的是,這樣的話它會忽略它並繼續下一步。如果是這樣,是否有辦法讓foreach
保持循環(或者可以使用while
)繼續瀏覽項目,直到全部插入爲止?