0

First of all: Yes i did check other answers but they sadly didn't do the trick.

So i'm currently working on a script that checks if an email exists in the database. So the database data is obtained through a webservice and with an input filter function the following JSON object is returned:

{"customers":{"customer":{"lastname":"test","firstname":"login","email":"[email protected]"}}} 

Now i would like to check if the email is filled in correctly. I'm using a foreach() statement to compare the values but i'm always getting a not found returned. Maybe someone here is able to find the mistake i've made. So the full code is shown down below.

$resultEmail = ($webService->get( $optUser ));

$emailResult = json_encode($resultEmail);
$emailArray = json_decode($resultEmail);

echo ($emailResult);
echo ($chopEmail);
foreach($emailArray->customers->customer as $item)
{
    if($item->email == $email)
    {
        echo "found it!";
    }
}

// The $optUser is the JSON object
5
  • $emailResult is a json object. You can not use it in array. Commented Nov 30, 2017 at 12:57
  • @jeroen php will execute a foreach if an StdClass is passed to it Commented Nov 30, 2017 at 13:02
  • @WilliamPerron You're right, and not only StdClass it seems... Commented Nov 30, 2017 at 13:05
  • Why do you use json_encode on the json before you use json_decode? Wouldn't that give the original result? Also the returned string looks a bit strange, you have an object customers with an object customer, not with an array of customer objects Commented Nov 30, 2017 at 13:06
  • @rypskar Yeah i'm aware of that but that's how the database of prestashop is build. Can't do much about that. Commented Nov 30, 2017 at 13:07

3 Answers 3

1

Probably the quickest way would be strpos function, so you can use it this way

function hasEmail($string, $email)
{
    return strpos($string, $email) !== false;
}

//example
echo hasEmail($resultEmail, $email) ? 'Has email' : 'Email not found';
Sign up to request clarification or add additional context in comments.

1 Comment

Great! I already took a look at strpos() but at first it didn't work. Now it does, thank you for your explanation and answer!
0

The easiest way to do this would probably be to decode the string as an associative array instead of a json object, and then check if the key email exists using the array_key_exists function.

// passing true to json_decode will make it return an array
$emailArray = json_decode($resultEmail, true);

foreach($emailArray['customers'] as $customer) {
    if(array_key_exists('email', $customer)) {
        echo 'Found it!';
    }
}

Comments

0

It seems your mistake come from the foreach loop. You should write it this way :

foreach($emailArray->customers as $customer) {
    if($customer->email == $email) {
        echo "found it!";
    }
}

Please note that $emailArray is not an array but an object when you don't set the second parameter of the json_decode function :

$obj = json_decode($data);
$array = json_decode($data,true);

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.