6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

多次元配列をフラットな配列にするとき

Last updated at Posted at 2012-12-16

タイトルまんまですけど、多次元配列をフラットにして処理していくということが続いたのでメモ。

flatten_array()とarray_flatten()ともにPHPマニュアルから引っ張ってきたものを、クラスメソッドにするためにちょっといじってます。
結果はどっちも同じなので好きな方でどうぞ。(テストコードで一応簡単に調べてます)

<?php
class arr{
	
	/**
	 * array_walk()のコールバック関数として使います
	 */
	public function flatten_array($value, $key, &$array) {
		if (!is_array($value))
			array_push($array,$value);
		else
			array_walk($value, array('self','flatten_array'), &$array); 
	}

	/**
	 * こっちは普通に呼ぶだけ
	 */	
	public static function array_flatten($arr){ //flattens multi-dim arrays (distroys keys)
		$flat_arr = array();
		if(!is_array($arr)) return $flat_arr;
		foreach($arr as $value){
			if(is_array($value)){
				$flat_arr = array_merge($flat_arr,self::array_flatten($value));
			}else{
				array_push($flat_arr,$value);
			}
		}
		return $flat_arr;
	}
}

// 以下はテストコード
$test_arr = new test_arr;
$test_arr->test_flatten_array();
$test_arr->test_array_flatten();

class assertion {
	
	public function assertEqual($expected, $actuary){
		if($expected !== $actuary) {
			throw new Exception('Assertion failed.');
		} else {
			echo '.',PHP_EOL;
		}
	}
}

class test_arr extends assertion {

	public $arr;

	public function __construct(){
		$this->arr = new arr;
	}

	public function test_flatten_array(){
		$new_array = array();
		$old_array = $this->old_array();
		array_walk($old_array, array('arr','flatten_array'), &$new_array);
		$expected_array = $this->expected_array();
		$this->assertEqual($expected_array, $new_array);
	}

	public function test_array_flatten(){
		$old_array = $this->old_array();
		$new_array = arr::array_flatten($old_array);
		$expected_array = $this->expected_array();
		$this->assertEqual($expected_array, $new_array);
	}
	
	public function old_array(){
		return array(
		    1 => array(
	    		'0001_hoge.dat',
	    		'0002_hoge.dat',
	    		'0003_hoge.dat',
	    		'0004_hoge.dat',
	    		'0005_hoge.dat',
	    	),
		    2 => array(
	    		'0001_fuga.dat',
	    		'0002_fuga.dat',
	    		'0003_fuga.dat',
	    		'0004_fuga.dat',
	    		'0005_fuga.dat',
	    	),
		    3 => array(
	    		'0001_mugi.dat',
	    		'0002_mugi.dat',
	    		'0003_mugi.dat',
	    		'0004_mugi.dat',
	    		'0005_mugi.dat',
	    	)
		);
	} 

	public function expected_array(){
		return array(
			'0001_hoge.dat',
			'0002_hoge.dat',
			'0003_hoge.dat',
			'0004_hoge.dat',
			'0005_hoge.dat',
			'0001_fuga.dat',
			'0002_fuga.dat',
			'0003_fuga.dat',
			'0004_fuga.dat',
			'0005_fuga.dat',
			'0001_mugi.dat',
			'0002_mugi.dat',
			'0003_mugi.dat',
			'0004_mugi.dat',
			'0005_mugi.dat',
		);
	}

}

6
10
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?