php函数来命令一个嵌套数组

时间:2013-07-25 17:13:30

标签: php arrays sorting bulk

我正在尝试使用函数来解决此问题,以处理重新排序数组。

这就是我所拥有的:

这是我的呼叫功能:

_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;
}

1 个答案:

答案 0 :(得分:0)

当然可以

 $results = array();
 foreach($options as $option){
      $res = array();
      foreach ($option as $kv){
           $res[$kv["field"]] = $kv["values"]; 
      }
      $results[] = $res;

 }