0

I am generating multidimensional array like this from database

$sender_array = [];

$qq = "SELECT * FROM tbl_senders WHERE s_status = 0 LIMIT 5";
$res = mysqli_query($conn,$qq);
$totals = mysqli_num_rows($res);
if($totals==0){
    //exit();
}else{
    
   while($row=mysqli_fetch_array($res)){
        $s_email = $row['s_email'];
        $s_user = $row['s_user'];
        $s_id = $row['s_id'];
        $sender_array[] = [
            's_id' => $s_id,
            's_user' => $s_user,
            's_email' => $s_email,
            's_inbox'      => 0,
            's_spam'      => 0
        ];
        
        $mysenders[]=$s_email;
        
    }

}

Which is providing array like this

Array
(
    [0] => Array
        (
            [s_id] => 2
            [s_user] => 4
            [s_email] => claire@example.com
            [s_inbox] => 0
            [s_spam] => 0
        )

    [1] => Array
        (
            [s_id] => 3
            [s_user] => 4
            [s_email] => ella@example.com
            [s_inbox] => 0
            [s_spam] => 0
        )

    [2] => Array
        (
            [s_id] => 4
            [s_user] => 4
            [s_email] => amelia@example.com
            [s_inbox] => 0
            [s_spam] => 0
        )

    [3] => Array
        (
            [s_id] => 6
            [s_user] => 4
            [s_email] => sarah@example.com
            [s_inbox] => 0
            [s_spam] => 0
        )

    [4] => Array
        (
            [s_id] => 7
            [s_user] => 4
            [s_email] => victoria@example.com
            [s_inbox] => 0
            [s_spam] => 0
        )

)

Now What I am looking is update

s_inbox value of ella@example.com or s_spam value of amelia@example.com

like

s_inbox  = s_inbox + 1 or
s_spam  = s_spam + 1

I am not getting idea which approach is best for do it. I have tried something like this

function findandReplace(&$array,$s_email,$type) {
        foreach($array as $key => &$value)
        { 
            if(is_array($value))
            { 
                findandReplace($value,$s_email,$type); 
            }
            else{
                
                if ($key == 's_email' && $value == $s_email) {
                  
                    if($type=="s_inbox"){
                        //echo $array['s_spam'];echo "<br>";
                        $array['s_inbox'] = intval($array['s_inbox'])+1;
                    }else{
                         //echo $array['s_spam'];echo "<br>";
                        $array['s_spam'] = intval($array['s_spam'])+1;
                    }
                    
                    break;
                }
            } 
        }

        return $array;
    }

and calling this function like this for update values

$sender_arrays = findandReplace($sender_array,$fromAddress,"s_inbox");

but its not properly working and providing wrong data.

Let me know if someone here can help me for achieve my goal with simple function.

Thanks!

Manisha
  • 195
  • 1
  • 10

1 Answers1

0

You can use a simple foreach loop:

function findandReplace(&$sender_array, $s_email, $type){
    foreach($sender_array as &$v)
        if($v['s_email'] == $s_email && isset($v[$type]))
            $v[$type]++;
}

or use the array_walk function:

function findandReplace(&$sender_array, $s_email, $type){
    array_walk($sender_array, function(&$v) use($s_email, $type){
        if($v['s_email'] == $s_email && isset($v[$type]))
            $v[$type]++;
    });
}
shingo
  • 18,436
  • 5
  • 23
  • 42