Welcome Guest, Not a member yet? Register   Sign In
change the value of the array
#1

(This post was last modified: 05-11-2019, 03:03 PM by DELE.)

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?
Reply
#2

You need to add it inside of the foreach loop.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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?
Reply
#4

(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.
Reply
#5

Sorry missed your first foreach, do a var_dump() of your arrays to see what is being inserted.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

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;

Reply
#7

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB