merging a single array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

merging a single array

Post by s.dot »

Say you have this array:

Code: Select all

Array
(
    [0] => Array
    (
        [1] => value
        [2] => value
        [3] => value
    )

    [1] => Array
    (
        [1] => value
        [2] => value
        [3] => value
    )

    [2] => Array
    (
        [1] => value
        [2] => value
        [3] => value
    )
)
What's the best way to merge this into a single array, so the above array would look like:

Code: Select all

Array
(
    [0] => value
    [1] => value
    [2] => value
    [3] => value
    [4] => value
    [5] => value
    [6] => value
    [7] => value
    [8] => value
)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Depending on how you want the logic to work, it may be a simple array_filter(), array_values(), array_diff() -type mashing.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Would that be any better or more efficient than..

Code: Select all

$values = array();
foreach ($arr AS $subArr)
{
    foreach ($subArr AS $value)
    {
        $values[] = $sub;
    }
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

Not sure if it would be more efficient, however this would let you be a bit more recursive..

Code: Select all

<?php
class array_handler {
	protected $values;
	
	public function getValues($array) {
		$this->values = array();
		$this->returnValues($array);
		return $this->values;
	}
	
	protected function returnValues($tree) {
		foreach ($tree as $key=>$value) {
			if(is_array($value)) {
				$this->returnValues($value);
			} else {
				$this->values[] = $value;
			}
		}
	}
}
?>
obviously this raises the issue of associative arrays, value ordering and the fact there's probably a far easier method..

a random thought.. surely to get that array you had to build it, in which case why didn't you build it flat?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

yet another way

Post by yacahuma »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

$arr = array(array('val1','val2','val3'),
             array('val4','val5','val6'),
             array('val7','val8','val9'));

$narr = array();
foreach ($arr as $v)  $narr = array_merge ($narr,$v);
I have no clue how fast will be one compared to the other. Of course this will not give you the flexibility if you have more array levels.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

	$test = array(
		array(
			array(
				'val1',
				'val2',
				'val3',
			),
			'val4',
			'val5',
		),
		array(
			'val6',
			array(
				'val7',
				'val8',
				'val9',
			),
			'val10',
		),
		array(
			'val11',
			'val12',
			array(
				'val13',
				'val14',
				'val15',
			),
		),
	);
	
	$expected = array(
		'val1',
		'val2',
		'val3',
		'val4',
		'val5',
		'val6',
		'val7',
		'val8',
		'val9',
		'val10',
		'val11',
		'val12',
		'val13',
		'val14',
		'val15',
	);
				
	function array_flatten_recursive(array $d)
	{
		$out = array();
		foreach($d as $v)
		{
			if (is_array($v))
			{
				$out = array_merge($out, array_flatten_recursive($v));
			}
			else
			{
				$out[] = $v;
			}
		}
		
		return $out;
	}

	function array_flatten(array $d)
	{
		$s   = array(array(array_values($d),0));
		$out = array();
		
		$c = 0;
		
		do
		{
			$z = count($s);
			$j = $z - 1;
			for($i = $s[$j][1], $l = count($s[$j][0]);
				$i < $l;
				++$i)
			{
				if (is_array($s[$j][0][$i]))
				{
					$s[$j][1] = $i + 1;
					$s[] = array(array_values($s[$j][0][$i]), 0);
					break;
				}
				else
				{
					$out[] = $s[$j][0][$i];
				}
			}
			
			if ($i >= $l)
			{
				array_pop($s);
			}
			else
			{
				continue;
			}
		}
		while(count($s) > 0);
		
		return $out;
	}
	
	$result = array_flatten_recursive($test);
	
	echo 'array_flatten_recursive() ' . 
		($result == $expected ? 'PASSED' : 'FAILED' . PHP_EOL . 
			var_export($result, true)) . 
		PHP_EOL . PHP_EOL;

	$result = array_flatten($test);
	
	echo 'array_flatten() ' . 
		($result == $expected ? 'PASSED' : 'FAILED' . PHP_EOL . 
			 var_export($result, true)) . 
		PHP_EOL . PHP_EOL;

Code: Select all

feyd:~ feyd$ php -f arraycrush.php
array_flatten_recursive() PASSED

array_flatten() PASSED
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Complete with unit testing and everything!

Dude.... nice.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Kieran Huggins wrote:Complete with unit testing and everything!

Dude.... nice.
It was easier to add quick'n'dirty unit tests rather than trying to compare by eye with that many values. ;)
Post Reply