CodeIgniter Forums
change the value of the array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: change the value of the array (/thread-73579.html)



change the value of the array - DELE - 05-11-2019

My Array :
PHP Code:
Array
(
 
   [0] => Array
 
       (
 
           [income_user] => 00002
            
[income_country] => 1CN,1US,1US,1ID
        
)

 
   [1] => Array
 
       (
 
           [income_user] => 00003
            
[income_country] => 2US
        
)

 
   [2] => Array
 
       (
 
           [income_user] => 00004
            
[income_country] => 1US
        
)


I want to change the value of each income_country:
PHP Code:
foreach ($data as $key => $value)
{
 
   foreach (explode(','$value["income_country"]) as $val)
 
   {
 
       preg_match('#([0-9]+)([A-Z]+)#',$val,$extrack);
 
       $a                $extrack[1];
 
       $b                $extrack[2];
 
       $c                isset($z[$b])?$z[$b]:0;
 
       $d                $c $a;
 
       $z[$b           $d;
 
       arsort($z);
 
   }
 
   $data[$key]["income_country"] = $z;


Results :
PHP Code:
Array
(
 
   [0] => Array
 
       (
 
           [income_user] => 00002
            
[income_country] => Array
 
               (
 
                   [US] => 2
                    
[CN] => 1
                    
[ID] => 1
                
)
 
       )

 
   [1] => Array
 
       (
 
           [income_user] => 00003
            
[income_country] => Array
 
               (
                    
// should [US] => 2

 
                   [US] => //?
 
                   [CN] => //?
 
                   [ID] => //?
 
               )
 
       )

 
   [2] => Array
 
       (
 
           [income_user] => 00004
            
[income_country] => Array
 
               (
 
                   // should [US] => 1

 
                   [US] => //?
 
                   [CN] => //?
 
                   [ID] => //?
 
               )
 
       )


why is the result always added to each array?


RE: change the value of the array - InsiteFX - 05-12-2019

You need to add it inside of the foreach loop.


RE: change the value of the array - DELE - 05-12-2019

(05-12-2019, 03:43 AM)InsiteFX Wrote: You need to add it inside of the foreach loop.

what do I mean I don't understand, I add it in the foreach loop right?


RE: change the value of the array - DELE - 05-12-2019

(05-12-2019, 03:43 AM)InsiteFX Wrote: You need to add it inside of the foreach loop.

I tried to turn back but still failed. please enlighten.


RE: change the value of the array - InsiteFX - 05-13-2019

Sorry missed your first foreach, do a var_dump() of your arrays to see what is being inserted.


RE: change the value of the array - Wouter60 - 05-13-2019

The $z array is being preserved in the foreach loop. Change it to this:
PHP Code:
foreach ($data as $key => $value)
{
 
   $z = array();
 
   
    foreach 
(explode(','$value["income_country"]) as $val)
 
   {
 
       preg_match('#([0-9]+)([A-Z]+)#',$val,$extrack);
 
       $a $extrack[1];
 
       $b $extrack[2];
 
       if (isset($z[$b])) {
 
        $z[$b] += $a;
 
       }
 
       else {
 
        $z[$b] = $a;
 
       }
 
       arsort($z);
 
   }
 
   $data[$key]["income_country"] = $z;




RE: change the value of the array - DELE - 05-14-2019

(05-13-2019, 11:20 AM)Wouter60 Wrote: The $z array is being preserved in the foreach loop. Change it to this:
PHP Code:
foreach ($data as $key => $value)
{
 
   $z = array();
 
   
    foreach 
(explode(','$value["income_country"]) as $val)
 
   {
 
       preg_match('#([0-9]+)([A-Z]+)#',$val,$extrack);
 
       $a $extrack[1];
 
       $b $extrack[2];
 
       if (isset($z[$b])) {
 
        $z[$b] += $a;
 
       }
 
       else {
 
        $z[$b] = $a;
 
       }
 
       arsort($z);
 
   }
 
   $data[$key]["income_country"] = $z;


this works thank you very much