0

My code creates an array like this

$stmt = $pdo->prepare("SELECT uniqueid, firstname, lastname, email, notify FROM information WHERE userid = ? AND notify = 'yes'");
    $stmt->execute([$userid]);
    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
        //get emails
        $notifyemailsarray[$row['email']]=$row;
    }

After that query runs (or possibly even before it runs), I would like to add one specific set of data to that array, but only email, firstname and lastname. So I tried this...

$specificemail="[email protected]";
$specificfirstname="Bob";
$specificlastname="Jones";

    $notifyemailsarray[$specificemail] = array('firstname' => $specificfirstname, 'lastname' => $specificlastname); 

But when I print_r that array afterwards, I don't see the "specific" info that I added separately. I do see the data that was populated from the PDO query, just not the "specific" stuff. What am I doing wrong?

3
  • 1
    That should work, so there's most likely some code here we're not seeing. Can you show the full code concerning this, and in what order it comes? Does the email [email protected] appear in the database too, perhaps? Commented Aug 8, 2017 at 16:45
  • 2
    Nothing wrong with what you are doing. Only issues would be A) the email already exists and so you are just overwriting an existing email, not adding one to the end. B) you are not including the email in the array (not really an issue if you don't need it, but it is in the original data you are building the array with). Or C) shot in the dark, but possibly a scope or reference issue. e.g. adding that last row inside of a function, but building the original array outside the function. Inside the function would only have a copy of the original data so changes won't affect outside the function. Commented Aug 8, 2017 at 16:49
  • @Jonathan Kuhn It was scenario A :(. I didn't realize the "specific" email I was adding was already in the database and already a part of the array, my apologies. But thanks for saying that and leading me to find it. Commented Aug 8, 2017 at 17:18

1 Answer 1

1

Nothing wrong with what you are doing. Only issues would be:

  • The email already exists and so you are just overwriting an existing email, not adding one to the end.
  • You are not including the email in the array (not really an issue if you don't need it, but it is in the original data you are building the array with).
  • Shot in the dark, but possibly a scope or reference issue. e.g. adding that last row inside of a function, but building the original array outside the function. Inside the function would only have a copy of the original data so changes won't affect outside the function.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.