How to visit an array indexed by indexed in php / Laravel?

101
May 28, 2022, at 12:20 PM

I got an API response like

["at123@gmail.com","adhd5@gmail.com","adahsad5@gmail.com"]

I got this array in a request variable $request->optional_email I am trying to access data by a loop like below:

foreach ($request->optional_email as $key => $optionalEmail) {
    $email->email = $optionalEmail[$key];
    $email->save();
}

But it doesn't work. How can I solve it?

Answer 1

As the $request->optional_email is just a list you do not need to use the $key variable in the foreach. Instead you should just use the value ($optionalEmail) of the foreach so your code would look something like this:

foreach ($request->optional_email as $optionalEmail) {
    $email->email = $optionalEmail;
    $email->save();
}
Rent Charter Buses Company
READ ALSO
Why date comparison gives wrong result?

Why date comparison gives wrong result?

I am trying to give access to users in same specific datesFor example one of them started today and the comparison worked fine

102
How we can insert header and footer in google docs with google docs api using PHP code

How we can insert header and footer in google docs with google docs api using PHP code

I want to insert header and footer in my google docs with google docs api in PHP codeI am doing it like this-

80
Merge people profiles based on email match

Merge people profiles based on email match

I have an existing directory (php with xml datasource) which contains people information such as this:

113
Writing a new and appending a file in PHP without erasing contents

Writing a new and appending a file in PHP without erasing contents

How could one write a new line to a file in php without erasing all the other contents of the file?

101