Home  >  Q&A  >  body text

Issue with not being able to append value using Php in loop

I'm using php and phpexcel, I have the following array ($data), I want to search and append the array value in php, for example, I want to change the user's "link" value (want to append after the previous value "txt") whose email id is "cde@gmail.com" but it is not working properly, I mean after refreshing the page it removes the previous "append" value and appends the current value but I want to keep What should I do with the previous value? This is my current array

Array
(
    [0] => Array
        (
            [email] => abc@gmail.com
            [link] => abc,xyz,def
         )

    [1] => Array
        (
            [email] => cde@gmail.com
            [link] => cde,abb
        )
...

Desired result (adding "bbb" while removing "abb")

Array
(
    [0] => Array
        (
            [email] => abc@gmail.com
            [link] => abc,xyz,def
         )

    [1] => Array
        (
            [email] => cde@gmail.com
            [link] => cde,abb,bbb
        )
...

I tried the following code, but it will "delete" the previous value ("abb"), and I want to append the new value to the previous value

$searchEmail = 'cde@gmail.com';
$appendString = 'bbb';
foreach ($set_excel_query_all as $key => &$item) {
   if ($item['email'] == $searchEmail) {
      $item['link'] .= ',' . $appendString;
      break;
   }
}
unset($item);

P粉478835592P粉478835592423 days ago595

reply all(1)I'll reply

  • P粉098417223

    P粉0984172232023-09-14 16:03:47

    I ran into the same error yesterday as you did here. Try this code

    $searchEmail = 'cde@gmail.com';
    $appendString = 'bbb';
    foreach ($set_excel_query_all as $key => $item) {
    if ($item['email'] == $searchEmail) {
      $set_excel_query_all[$key]['link'] .= ',' . $appendString;
      break;
    }
    }
    unset($item);

    reply
    0
  • Cancelreply