我有一个深度未知的动态多维数组。key=>value我想根据另一个条件替换 a key=>value。
例如,当is$settings[$cient_id]时,替换 的值。$form_id1f44537
$data = array (
0 =>
array (
'id' => '2cd5985',
'elType' => 'section',
'settings' => array(),
'elements' => array (
0 => array (
'id' => '88063e6',
'elType' => 'column',
'settings' => array(),
'elements' => array (
0 =>
array (
'id' => '1f44537',
'elType' => 'widget',
'settings' => array (
'ap_google_sheet_client_id' => 'test_id',
),
'elements' => array (
),
'widgetType' => 'form',
),
)
),
1 => array (
'id' => '7878c73',
'elType' => 'column',
'settings' => array(),
'elements' => array (
0 =>
array (
'id' => '1f44537',
'elType' => 'widget',
'settings' => array (
'ap_google_sheet_client_id' => 'test_id',
),
'elements' => array (
),
'widgetType' => 'form',
),
)
),
),
),
);
这是我不成功的尝试
尝试1
public function replace_recursive( $elements, $form_id ) {
foreach ( $elements as &$element) {
if ( $element['id'] === $form_id ) {
$element['settings']['ap_google_sheet_client_id'] = 'replaced';
return $elements;
}
if ( ! empty( $element['elements'] ) ) {
$elements = self::replace_recursive( $element['elements'], $form_id );
if ( $elements ) {
return $elements;
}
}
}
}
慕仙森