PHPで、再帰や参照を使用せずに(二/多)次元配列をフラット化することは可能ですか?
私は値にのみ興味があるので、キーは無視できます。array_map()との行で考えていarray_values()ます。
PHPで、再帰や参照を使用せずに(二/多)次元配列をフラット化することは可能ですか?
私は値にのみ興味があるので、キーは無視できます。array_map()との行で考えていarray_values()ます。
回答:
標準PHPライブラリ(SPL)を使用して、再帰を「隠す」ことができます。
$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
  echo $v, " ";
}プリント
1 2 3 4 5 6 7 8 9 iterator_to_array($it, false)foreachの必要性を回避します。
                    function flatten($arr){     $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));     return iterator_to_array($it, true); }これが他の人を助けることを願っています。
                    とおりPHP 5.3最短ソリューションがあると思われるarray_walk_recursive()新しいクロージャ構文で:
function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}useこれを使用するための構文が必要array_walk_recursiveです$userdata
                    2次元配列のソリューション
これを試してください:
$array  = your array
$result = call_user_func_array('array_merge', $array);
echo "<pre>";
print_r($result);編集:2013年8月21日
多次元配列で機能するソリューションは次のとおりです。
function array_flatten($array) {
    $return = array();
    foreach ($array as $key => $value) {
        if (is_array($value)){
            $return = array_merge($return, array_flatten($value));
        } else {
            $return[$key] = $value;
        }
    }
    return $return;
}
$array  = Your array
$result = array_flatten($array);
echo "<pre>";
print_r($result);参照:http : //php.net/manual/en/function.call-user-func-array.php
call_user_func_array('array_merge', [])(空の配列に注意)nullを返し、php警告エラーをトリガーします。配列が空にならないという事実を知っている場合、これは洗練されたソリューションですが、それは多くの人が行うことができる一般的な仮定ではありません。
                    $result = $array ?call_user_func_array('array_merge', $array) : [];
                    PHP 5.6 array_merge以降では、外部配列を...演算子でアンパックした後、2次元配列をフラット化できます。コードはシンプルで明確です。
array_merge(...$a);これは、連想配列のコレクションでも機能します。
$a = [[10, 20], [30, 40]];
$b = [["x" => "X", "y" => "Y"], ["p" => "P", "q" => "Q"]];
print_r(array_merge(...$a));
print_r(array_merge(...$b));
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)
Array
(
    [x] => X
    [y] => Y
    [p] => P
    [q] => Q
)ただし、外部配列に非数値キーがある場合は機能しません。その場合、array_values最初に電話をする必要があります。
$c = ["a" => ["x" => "X", "y" => "Y"], "b" => ["p" => "P", "q" => "Q"]];
print_r(array_merge(...array_values($c)));
Array
(
    [x] => X
    [y] => Y
    [p] => P
    [q] => Q
)更新:@MohamedGharibのコメントに基づく
これは、外側の配列が空の場合にエラーをスローします。 array_merge引数なしで呼び出されるされます。最初の引数として空の配列を追加することで回避できます。
array_merge([], ...$a);array_merge([], ...$a);
                    (要求したとおりに)再帰なしでフラット化するには、スタックを使用できます。当然、これをのような独自の関数に入れることができますarray_flatten。以下は、キーなしで機能するバージョンです。
function array_flatten(array $array)
{
    $flat = array(); // initialize return array
    $stack = array_values($array); // initialize stack
    while($stack) // process stack until done
    {
        $value = array_shift($stack);
        if (is_array($value)) // a value to further process
        {
            $stack = array_merge(array_values($value), $stack);
        }
        else // a value to take
        {
           $flat[] = $value;
        }
    }
    return $flat;
}要素はその順序で処理されます。サブエレメントはスタックの一番上に移動されるため、次に処理されます。
キーを考慮することも可能ですが、スタックを処理するには別の戦略が必要になります。これは、サブ配列内の重複キーの可能性に対処する必要があるために必要です。関連する質問の同様の答え:PHPキーを保持しながら多次元配列をウォークスルー
具体的にはわかりませんが、IIは過去にこれをテストしました。これRecurisiveIteratorは再帰を使用するため、実際に何が必要かによって異なります。スタックに基づいて再帰的なイテレータを作成することも可能でなければなりません:
foreach(new FlatRecursiveArrayIterator($array) as $key => $value)
{
    echo "** ($key) $value\n";
}これまでのところ、RecursiveIterator私は良いアイデアだと思うスタックを実装することはしませんでした。
if(!empty($value)){$flat[] = $value}結果の配列に追加空の状態を防ぐために、else文の内側。素晴らしい機能!
                    わかりやすいとワンライナーの答え。
function flatten_array(array $array)
{
    return iterator_to_array(
         new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array)));
}使用法:
$array = [
    'name' => 'Allen Linatoc',
    'profile' => [
        'age' => 21,
        'favourite_games' => [ 'Call of Duty', 'Titanfall', 'Far Cry' ]
    ]
];
print_r( flatten_array($array) );出力(PsySH):
Array
(
    [name] => Allen Linatoc
    [age] => 21
    [0] => Call of Duty
    [1] => Titanfall
    [2] => Far Cry
)これで、キーをどのように処理するかはあなた次第です。乾杯
編集(2017-03-01)
Nigel Aldertonの懸念/問題を引用:
明確にするために、これはキー(数値のキーも含む)を保持するため、同じキーを持つ値は失われます。たとえばに
$array = ['a',['b','c']]なりArray ([0] => b, [1] => c )ます。のキーも持っている'a'ため'b'、0
Svishの答えを引用する:
ただ、2番目のパラメータとして偽を追加
($use_keys)するiterator_to_arrayコール
$array = ['a',['b','c']]なりArray ([0] => b, [1] => c )ます。ものキーを持っている'a'ため'b'、失われます0。
                    再帰を使用します。それがどれほど複雑でないかを見ると、うまくいけば、それがどれほど複雑でないかを見ると、再帰の恐れがなくなるでしょう。
function flatten($array) {
    if (!is_array($array)) {
        // nothing to do if it's not an array
        return array($array);
    }
    $result = array();
    foreach ($array as $value) {
        // explode the sub-array, and add the parts
        $result = array_merge($result, flatten($value));
    }
    return $result;
}
$arr = array('foo', array('nobody', 'expects', array('another', 'level'), 'the', 'Spanish', 'Inquisition'), 'bar');
echo '<ul>';
foreach (flatten($arr) as $value) {
    echo '<li>', $value, '</li>';
}
echo '<ul>';出力:
<ul><li>foo</li><li>nobody</li><li>expects</li><li>another</li><li>level</li><li>the</li><li>Spanish</li><li>Inquisition</li><li>bar</li><ul>これはフォールドであることを指摘したので、array_reduceを使用できます。
array_reduce($my_array, 'array_merge', array());編集:これは、任意の数のレベルを平坦化するように構成できることに注意してください。これにはいくつかの方法があります。
// Reduces one level
$concat   = function($x) { return array_reduce($x, 'array_merge', array()); };
// We can compose $concat with itself $n times, then apply it to $x
// This can overflow the stack for large $n
$compose  = function($f, $g) {
    return function($x) use ($f, $g) { return $f($g($x)); };
};
$identity = function($x) { return $x; };
$flattenA = function($n) use ($compose, $identity, $concat) {
    return  function($x) use ($compose, $identity, $concat, $n) {
        return ($n === 0)? $x
                         : call_user_func(array_reduce(array_fill(0, $n, $concat),
                                                       $compose,
                                                       $identity),
                                          $x);
    };
};
// We can iteratively apply $concat to $x, $n times
$uncurriedFlip     = function($f) {
    return  function($a, $b) use ($f) {
        return $f($b, $a);
    };
};
$iterate  = function($f) use ($uncurriedFlip) {
    return  function($n) use ($uncurriedFlip, $f) {
    return  function($x) use ($uncurriedFlip, $f, $n) {
        return ($n === 0)? $x
                         : array_reduce(array_fill(0, $n, $f),
                                        $uncurriedFlip('call_user_func'),
                                        $x);
    }; };
};
$flattenB = $iterate($concat);
// Example usage:
$apply    = function($f, $x) {
    return $f($x);
};
$curriedFlip = function($f) {
    return  function($a) use ($f) {
    return  function($b) use ($f, $a) {
        return $f($b, $a);
    }; };
};
var_dump(
    array_map(
        call_user_func($curriedFlip($apply),
                       array(array(array('A', 'B', 'C'),
                                   array('D')),
                             array(array(),
                                   array('E')))),
        array($flattenA(2), $flattenB(2))));もちろん、ループを使用することもできますが、質問では、array_mapまたはarray_valuesの行に沿って結合関数を求めます。
fold4レベルにしたりfold . fold、3レベルにしたりfold . fold . fold、2レベルにしたりすることができます。これにより、バグが非表示になるのを防ぐこともできます。例えば。5D配列をフラット化したいが、4D配列が与えられた場合、エラーはすぐにトリガーされます。
                    このソリューションは非再帰的です。要素の順序は多少混合されることに注意してください。
function flatten($array) {
    $return = array();
    while(count($array)) {
        $value = array_shift($array);
        if(is_array($value))
            foreach($value as $sub)
                $array[] = $sub;
        else
            $return[] = $value;
    }
    return $return;
}shifting配列の値をオフにして最後に再度追加しても、あまり意味がありません。array_merge()代わりにしたいと思いますか?
                    これは、ミューテーションや見慣れないクラスを使用しない最もクリーンなソリューションだと思います。
<?php
function flatten($array)
{
    return array_reduce($array, function($acc, $item){
        return array_merge($acc, is_array($item) ? flatten($item) : [$item]);
    }, []);
}
// usage
$array = [1, 2, [3, 4], [5, [6, 7]], 8, 9, 10];
print_r(flatten($array));次の簡単な関数を試してください:
function _flatten_array($arr) {
  while ($arr) {
    list($key, $value) = each($arr); 
    is_array($value) ? $arr = $value : $out[$key] = $value;
    unset($arr[$key]);
  }
  return (array)$out;
}だからこれから:
array (
  'und' => 
  array (
    'profiles' => 
    array (
      0 => 
      array (
        'commerce_customer_address' => 
        array (
          'und' => 
          array (
            0 => 
            array (
              'first_name' => 'First name',
              'last_name' => 'Last name',
              'thoroughfare' => 'Address 1',
              'premise' => 'Address 2',
              'locality' => 'Town/City',
              'administrative_area' => 'County',
              'postal_code' => 'Postcode',
            ),
          ),
        ),
      ),
    ),
  ),
)あなたが得る:
array (
  'first_name' => 'First name',
  'last_name' => 'Last name',
  'thoroughfare' => 'Address 1',
  'premise' => 'Address 2',
  'locality' => 'Town/City',
  'administrative_area' => 'County',
  'postal_code' => 'Postcode',
)トリックは、参照によってソース配列と宛先配列の両方を渡すことです。
function flatten_array(&$arr, &$dst) {
    if(!isset($dst) || !is_array($dst)) {
        $dst = array();
    }
    if(!is_array($arr)) {
        $dst[] = $arr;
    } else {
        foreach($arr as &$subject) {
            flatten_array($subject, $dst);
        }
    }
}
$recursive = array('1', array('2','3',array('4',array('5','6')),'7',array(array(array('8'),'9'),'10')));
echo "Recursive: \r\n";
print_r($recursive);
$flat = null;
flatten_array($recursive, $flat);
echo "Flat: \r\n";
print_r($flat);
// If you change line 3 to $dst[] = &$arr; , you won't waste memory,
// since all you're doing is copying references, and imploding the array 
// into a string will be both memory efficient and fast:)
echo "String:\r\n";
echo implode(',',$flat);/**
 * For merging values of a multidimensional array into one 
 *
 * $array = [
 *     0 => [
 *         0 => 'a1',
 *         1 => 'b1',
 *         2 => 'c1',
 *         3 => 'd1'
 *     ],
 *     1 => [
 *         0 => 'a2',
 *         1 => 'b2',
 *         2 => 'c2',
 *     ]
 * ];
 *
 * becomes : 
 *
 * $array = [
 *     0 => 'a1',
 *     1 => 'b1',
 *     2 => 'c1',
 *     3 => 'd1',
 *     4 => 'a2',
 *     5 => 'b2',
 *     6 => 'c2',
 *     
 * ]
 */
array_reduce
(
    $multiArray
    , function ($lastItem, $currentItem) {
        $lastItem = $lastItem ?: array();
        return array_merge($lastItem, array_values($currentItem));
    }
);再帰が本当に嫌いなら...代わりにシフトしてみてください:)
$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
$o = [];
for ($i=0; $i<count($a); $i++) {
    if (is_array($a[$i])) {
        array_splice($a, $i+1, 0, $a[$i]);
    } else {
        $o[] = $a[$i];
    }
}注:このシンプルなバージョンでは、配列キーはサポートされていません。
continue、やや高速になります。
                    再帰的なジェネレータを使用するのはどうですか?https://ideone.com/d0TXCg
<?php
$array = [
    'name' => 'Allen Linatoc',
    'profile' => [
        'age' => 21,
        'favourite_games' => [ 'Call of Duty', 'Titanfall', 'Far Cry' ]
    ]
];
foreach (iterate($array) as $item) {
    var_dump($item);
};
function iterate($array)
{
    foreach ($array as $item) {
        if (is_array($item)) {
            yield from iterate($item);
        } else {
            yield $item;
        }
    }
}PHP 5.2の場合
function flatten(array $array) {
    $result = array();
    if (is_array($array)) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                $result = array_merge($result, flatten($v));
            } else {
                $result[] = $v;
            }
        }
    }
    return $result;
}このバージョンは、深い、浅い、または特定の数のレベルを実行できます。
/**
 * @param  array|object $array  array of mixed values to flatten
 * @param  int|boolean  $level  0:deep, 1:shallow, 2:2 levels, 3...
 * @return array
 */
function flatten($array, $level = 0) {
    $level = (int) $level;
    $result = array();
    foreach ($array as $i => $v) {
        if (0 <= $level && is_array($v)) {
            $v = flatten($v, $level > 1 ? $level - 1 : 0 - $level);
            $result = array_merge($result, $v);
        } elseif (is_int($i)) {
            $result[] = $v;
        } else {
            $result[$i] = $v; 
        }
    }
    return $result;
}ここのコードは怖いので。多次元配列をhtml形式互換の構文に変換する関数ですが、読みやすくなっています。
/**
 * Flattens a multi demensional array into a one dimensional
 * to be compatible with hidden html fields.
 *
 * @param array $array
 *  Array in the form:
 *  array(
 *    'a' => array(
 *      'b' => '1'
 *    )
 *  )
 *
 * @return array
 *  Array in the form:
 *  array(
 *    'a[b]' => 1,
 *  )
 */
function flatten_array($array) {
  // Continue until $array is a one-dimensional array.
  $continue = TRUE;
  while ($continue) {
    $continue = FALSE;
    // Walk through top and second level of $array and move 
    // all values in the second level up one level.
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        // Second level found, therefore continue.
        $continue = TRUE;
        // Move each value a level up.
        foreach ($value as $child_key => $child_value) {
          $array[$key . '[' . $child_key . ']'] = $child_value;
        }
        // Remove second level array from top level.
        unset($array[$key]);
      }
    }
  }
  return $array;
}これは、 array_walk_recursive
$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
array_walk_recursive($a, function($v) use (&$r){$r[]=$v;});
print_r($r);作業例:-https : //3v4l.org/FpIrG
これは参照を使用した私の解決策です:
function arrayFlatten($array_in, &$array_out){
    if(is_array($array_in)){
        foreach ($array_in as $element){
               arrayFlatten($element, $array_out);
        }
    }
    else{
        $array_out[] = $array_in; 
    }
}
$arr1 = array('1', '2', array(array(array('3'), '4', '5')), array(array('6')));
arrayFlatten($arr1, $arr2);
echo "<pre>";
print_r($arr2);
echo "</pre>";<?php
//recursive solution
//test array
$nested_array = [[1,2,[3]],4,[5],[[[6,[7=>[7,8,9,10]]]]]];
/*-----------------------------------------
function call and return result to an array
------------------------------------------*/
$index_count = 1;
$flatered_array = array();
$flatered_array = flat_array($nested_array, $index_count);
/*-----------------------------------------
Print Result
-----------------------------------------*/
echo "<pre>";
print_r($flatered_array);
/*-----------------------------------------
function to flaten an array 
-----------------------------------------*/
function flat_array($nested_array, & $index_count, & $flatered_array) {
  foreach($nested_array AS $key=>$val) {
      if(is_array($val)) {
        flat_array($val, $index_count, $flatered_array);
      }
      else {
        $flatered_array[$index_count] = $val;
        ++$index_count;
      }      
  }
return $flatered_array;
}
?>これに対する本当にクリーンなソリューションを探している人は誰でも。ここにオプションがあります:
$test_array = array(
    array('test' => 0, 0, 0, 0),
    array(0, 0, 'merp' => array('herp' => 'derp'), 0),
    array(0, 0, 0, 0),
    array(0, 0, 0, 0)
);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($test_array));
var_dump( iterator_to_array($it, false) ) ; プリント
 0 0 0 0 0 0 derp 0 0 0 0 0 0 0 0 0あなたが鍵も保持したい場合は、それが解決策です。
function reduce(array $array) {
    $return = array();
    array_walk_recursive($array, function($value, $key) use (&$return) { $return[$key] = $value; });
    return $return;
}残念ながら、最終的なネストされた配列のみが出力され、中間キーは出力されません。したがって、次の例の場合:
$array = array(
    'sweet' => array(
        'a' => 'apple',
        'b' => 'banana'),
    'sour' => 'lemon'); 
print_r(flatten($fruits));出力は次のとおりです。
Array
(
    [a] => apple
    [b] => banana
    [sour] => lemon
)PHPの多次元配列をHTML入力形式で表す必要がありました。
$test = [
    'a' => [
        'b' => [
            'c' => ['a', 'b']
        ]
    ],
    'b' => 'c',
    'c' => [
        'd' => 'e'
    ]
];
$flatten = function ($input, $parent = []) use (&$flatten) {
    $return = [];
    foreach ($input as $k => $v) {
        if (is_array($v)) {
            $return = array_merge($return, $flatten($v, array_merge($parent, [$k])));
        } else {
            if ($parent) {
                $key = implode('][', $parent) . '][' . $k . ']';
                if (substr_count($key, ']') != substr_count($key, '[')) {
                    $key = preg_replace('/\]/', '', $key, 1);
                }
            } else {
                $key = $k;
            }           
            $return[$key] = $v;
        }
    }
    return $return;
};
die(var_dump( $flatten($test) ));
array(4) {
  ["a[b][c][0]"]=>
  string(1) "a"
  ["a[b][c][1]"]=>
  string(1) "b"
  ["b"]=>
  string(1) "c"
  ["c[d]"]=>
  string(1) "e"
}$var['a']['b']['c'][0] = 'a'; ...です。
                    オブジェクトの配列があり、ノードでそれをフラット化する場合は、次の関数を使用します。
function objectArray_flatten($array,$childField) {
    $result = array();
    foreach ($array as $node)
    {
        $result[] = $node;
        if(isset($node->$childField))
        {
            $result = array_merge(
                $result, 
                objectArray_flatten($node->$childField,$childField)
            );
            unset($node->$childField);
        }
    }
    return $result;
}