我正在尝试使用处理重新排序数组的函数来解决这个问题。
这就是我所拥有的:
这是我的调用函数:
_inline_entity_form_bulk_value_fixing($operations);
和我定义的函数一样( $ret 它是返回结果的助手)
function _inline_entity_form_bulk_value_fixing(&$operations, &$ret = array()) {
所以在 $operations 数组中我有这个
Array
(
[0] => Array
(
[field] => field_colors
[values] => Array
(
[90] => 90
[89] => 89
[92] => 92
)
[volume] => 3
)
[1] => Array
(
[field] => field_size
[values] => Array
(
[86] => 86
[85] => 85
)
[volume] => 2
)
)
我想得到这样的东西。
array
(
[0] => array(
[field_colors] => array( [values] => array( [90] => 90 ) )
[field_size] => array( [values] => array( [86] => 86 ) )
)
[1] => array(
[field_colors] => array( [values] => array( [89] => 89 ) )
[field_size] => array( [values] => array( [86] => 86 ) )
)
[2] => array(
[field_colors] => array( [values] => array( [92] => 92 ) )
[field_size] => array( [values] => array( [86] => 86 ) )
)
[3] => array(
[field_colors] => array( [values] => array( [90] => 90 ) )
[field_size] => array( [values] => array( [85] => 85 ) )
)
[4] => array(
[field_colors] => array( [values] => array( [89] => 89 ) )
[field_size] => array( [values] => array( [85] => 85 ) )
)
[5] => array(
[field_colors] => array( [values] => array( [92] => 92 ) )
[field_size] => array( [values] => array( [85] => 85 ) )
)
)
我正在尝试使用 foreach 和 extras 功能,但我不能。
提前致谢。
编辑:
我找到了解决方案,这是我的函数 foreach 来订购我的数组。如果有人发现其他解决方案不那么混乱,请随时发布。
function _inline_entity_form_bulk_value_fixing($operations, array &$ret, $child = 0) {
// This entry was weird.
if (empty($operations) && !is_array($operations)) {
return;
}
$end_level = FALSE;
foreach ($operations as $value) {
array_shift($operations);
// The controler child tell me when is a full top item.
if (is_array($value['values']) && !$end_level) {
foreach ($value['values'] as $kvalues) {
if (!empty($ret) && $child === 1) {
$current_ret = end($ret);
$current_ret_key = key($ret);
$ret[$current_ret_key] = $current_ret + array(
$value['field'] => array(
LANGUAGE_NONE => array('#value' => array($kvalues => $kvalues)),
),
);
$child = 2;
}
elseif (!empty($ret) && $child === 2) {
$ret[] = $current_ret + array(
$value['field'] => array(
LANGUAGE_NONE => array('#value' => array($kvalues => $kvalues)),
),
);
}
else {
$ret[] = array(
$value['field'] => array(
LANGUAGE_NONE => array('#value' => array($kvalues => $kvalues)),
),
);
}
if (!empty($operations) && is_array($operations)) {
_inline_entity_form_bulk_value_fixing($operations, $ret, 1);
}
}
}
$end_level = TRUE;
}
return $ret;
}