How to convert 2-demensional array in 1-demednsional array?

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
Xname
New php-forum User
New php-forum User
Posts: 7
Joined: Wed May 09, 2012 5:47 am

Fri May 11, 2012 5:45 am

Code: Select all

<?php
$frnd1 = array(
0=> 'arfan',
1=> 'haider',

2=> array(
0=> 'azeem',
1=> 'jeme',
),
3=> array(
0=> 'haider',
1=> 'one'
)
);
?>
This is the code I want to convert this array in 1-demednsional array.
Please do not use foreach loop OR while loop OR for loop,my mean
do not use any loop to do it.

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 359
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Fri May 11, 2012 5:50 am

without using a loop seems sort of pointless, but it's easy. Just repetitive:

Code: Select all

$array = array (
0=> 'arfan',
1=> 'haider',
2=> 'azeem',
3=> 'jeme',

etc. etc.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Fri May 11, 2012 7:01 am

Sounds like homework.

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Fri May 11, 2012 8:29 am

Loop through first dimension elements. If element is not array - add it to result array, if it's array - do loop through this array and add all its content to result array

Code: Select all

loop through first dimension {
	if first dimension element is array {
		loop through this array {
			save elements to result array
		}
	} if it's not array {
		save elements to result array
	}
}

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 359
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Fri May 11, 2012 8:53 am

that would require a loop though :p

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Fri May 11, 2012 10:36 am

Could use recursion.

Code: Select all

$frnd1 = array(
0=> 'arfan',
1=> 'haider',

2=> array(
0=> 'azeem',
1=> 'jeme',
),
3=> array(
0=> 'haider',
1=> 'one'
)
);

print_r(collapseArray($frnd1));





function collapseArray($array){
	$resultArray = array();//initialize array

	//since this is recursive we need to have the kill condition
	if(empty($array)){
		//we have an empty array, return an empty array
		return $resultArray;
	}else{
		//here we need to deal with the array kind of like a stack.
		//first we will check if the current 0 indexed item is an array
		if(is_array($array[0])){
			$resultArray = collapseArray($array[0]);
		}else{
			$resultArray[] = $array[0];
		}
		
	    //now we pop the saved element off and call the function again
		$array = array_reverse($array);
		array_pop($array);
		$array = array_reverse($array);
		
		$returnedArray = collapseArray($array);
		
		//merge the recent element with the returned array
		$finalArray = array_merge($resultArray,$returnedArray);
		
		return $finalArray;
	}
      

}


Xname
New php-forum User
New php-forum User
Posts: 7
Joined: Wed May 09, 2012 5:47 am

Sat May 12, 2012 10:04 pm

Thanks,It is work fine and topic is solved.
But I can not find the button of tpoic sovled.
Where I tell the other that the topic is solved.
Here is any button in this site or not.
if yes where.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests