• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with arrays

#1
My array structure will be like this .. In this array I am getting Sub arrays Dynamically..
PHP Code:
Array
(
    [
0] => Array
        (
            [
NewFriend] => 147852
            
[MutualFriend] => 955716327
        
)

    [
1] => Array
        (
            [
NewFriend] => 12345
            
[MutualFriend] => 955716327
        
)

    [
2] => Array
        (
            [
NewFriend] => 12345
            
[MutualFriend] => 9845479445
        
)





But I want to change the array structure like This .. If NewFriend Is Same Value ... I want To out put Like This .. Is it possible..?
PHP Code:
Array
(
    [
0] => Array
        (
            [
NewFriend] => 147852
            
[MutualFriend] => 955716327
        
)

    [
1] => Array
        (
            [
NewFriend] => 12345
            
[MutualFriend] => 955716327,9845479445
        
)


Manikanta
Reply

#2
(02-16-2016, 02:14 AM)Chandini Wrote: My array structure will be like this .. In this array I am getting Sub arrays Dynamically..
PHP Code:
Array
(
 
   [0] => Array
 
       (
 
           [NewFriend] => 147852
            
[MutualFriend] => 955716327
        
)

 
   [1] => Array
 
       (
 
           [NewFriend] => 12345
            
[MutualFriend] => 955716327
        
)

 
   [2] => Array
 
       (
 
           [NewFriend] => 12345
            
[MutualFriend] => 9845479445
        
)





But I want to change the array structure like This .. If NewFriend Is Same Value ... I want To out put Like This .. Is it possible..?
PHP Code:
Array
(
 
   [0] => Array
 
       (
 
           [NewFriend] => 147852
            
[MutualFriend] => 955716327
        
)

 
   [1] => Array
 
       (
 
           [NewFriend] => 12345
            
[MutualFriend] => 955716327,9845479445
        
)




HERE you go.
PHP Code:
  $new_array = array();
   $i 0;
   foreach($array as $ind=>$val) {
      
       $found 
false;
       $j=0;
       foreach($new_array as $id=>$vl){
           if(!empty($vl['NewFriend']) && $vl['NewFriend'] == $val['NewFriend']) {
               $found true;
               $new_array[$j]['MutualFriend'] =  $vl['MutualFriend'].','.$val['MutualFriend']; 
               break;
           }
           $j $j +1;
       
       if(!$found ) {
           $new_array[$i]['NewFriend'] = $val['NewFriend'];    
           $new_array
[$i]['MutualFriend'] = $val['MutualFriend'];
        
       
       $i 
$i +1;
   
Amjad Farooq,
Skype: shahgeb
cell: +923444553686
Reply

#3
(02-16-2016, 06:57 AM)shaheb Wrote:
(02-16-2016, 02:14 AM)Chandini Wrote: My array structure will be like this .. In this array I am getting Sub arrays Dynamically..
PHP Code:
Array
(
 
   [0] => Array
 
       (
 
           [NewFriend] => 147852
            
[MutualFriend] => 955716327
        
)

 
   [1] => Array
 
       (
 
           [NewFriend] => 12345
            
[MutualFriend] => 955716327
        
)

 
   [2] => Array
 
       (
 
           [NewFriend] => 12345
            
[MutualFriend] => 9845479445
        
)





But I want to change the array structure like This .. If NewFriend Is Same Value ... I want To out put Like This .. Is it possible..?
PHP Code:
Array
(
 
   [0] => Array
 
       (
 
           [NewFriend] => 147852
            
[MutualFriend] => 955716327
        
)

 
   [1] => Array
 
       (
 
           [NewFriend] => 12345
            
[MutualFriend] => 955716327,9845479445
        
)




HERE you go.
PHP Code:
  $new_array = array();
   $i 0;
   foreach($array as $ind=>$val) {
      
       $found 
false;
       $j=0;
       foreach($new_array as $id=>$vl){
           if(!empty($vl['NewFriend']) && $vl['NewFriend'] == $val['NewFriend']) {
               $found true;
               $new_array[$j]['MutualFriend'] =  $vl['MutualFriend'].','.$val['MutualFriend']; 
               break;
           }
           $j $j +1;
       
       if(!$found ) {
           $new_array[$i]['NewFriend'] = $val['NewFriend'];    
           $new_array
[$i]['MutualFriend'] = $val['MutualFriend'];
        
       
       $i 
$i +1;
   

Thanks ..
Manikanta
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.