Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

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

Links for php scripts

Moderators: macek, egami, gesf

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

Postby Xname » 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.
Xname
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed May 09, 2012 5:47 am

Re: How to convert 2-demensional array in 1-demednsional arr

Postby freshnet » 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
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: How to convert 2-demensional array in 1-demednsional arr

Postby Nullsig » Fri May 11, 2012 7:01 am

Sounds like homework.
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: How to convert 2-demensional array in 1-demednsional arr

Postby minimihi » 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
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: How to convert 2-demensional array in 1-demednsional arr

Postby freshnet » Fri May 11, 2012 8:53 am

that would require a loop though :p
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: How to convert 2-demensional array in 1-demednsional arr

Postby Nullsig » 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;
   }
     

}

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

Re: How to convert 2-demensional array in 1-demednsional arr

Postby Xname » 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.
Xname
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed May 09, 2012 5:47 am


Return to PHP Scripts

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron