php function to order a nesting array -
i'm trying solve problem function handle reorder array.
this have:
this calling function:
_inline_entity_form_bulk_value_fixing($operations);
and defined function (the $ret it's helper return result)
function _inline_entity_form_bulk_value_fixing(&$operations, &$ret = array()) {
so in $operations array have this
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 ) )
and want this.
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 ) ) ) )
i trying foreach , extras functions can't.
thanks in advance.
edit:
i find solution function foreach order array. if find other solution less messy please feel free post it.
function _inline_entity_form_bulk_value_fixing($operations, array &$ret, $child = 0) { // entry weird. if (empty($operations) && !is_array($operations)) { return; } $end_level = false; foreach ($operations $value) { array_shift($operations); // controler child tell me when full top item. if (is_array($value['values']) && !$end_level) { foreach ($value['values'] $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; }
sure can
$results = array(); foreach($options $option){ $res = array(); foreach ($option $kv){ $res[$kv["field"]] = $kv["values"]; } $results[] = $res; }
Comments
Post a Comment