2

I want to echo a different image depending on if a key exists or not.

Here is an example of the array I'm using

["Person"] => array(11) {
    ["id"] => int(38482818123)
    ["weight"] => int(140)
    ["height"] => int(65)
}
["Name"] => array(2) {
    ["firstname"] => string(4) "John"
    ["lastname"] => string(5) "Smith"
}

So the name field isn't always there. I need to showimage a if the name is there and image b if there is no name.

What I've tried:

foreach($personArray as $person)
{
    if ($person['Name'] != '')
    {
        echo "<img src='image-a.png'>";
    }
    else
    {
        echo "<img src='image-b.png'>";
    }
}

Now the problem I have is that even though the person has a name, I'm seeing both images on the page instead of just image a

I have also tried using array_key_exists("Name", $personArray); but for some reason I am getting bool(false) as a result.

Array

4
  • You're iterating an array. So if some people have names and some don't, you'll probably see multiple copies of each image. Commented May 1, 2016 at 0:09
  • empty() might be the function you're looking for. !empty($person['Name']) Commented May 1, 2016 at 0:10
  • 1
    From what I can see, the Person array does not contain a name value to begin with. Name is a separate array altogether? Commented May 1, 2016 at 0:13
  • That's a typo on my part I believe, I updated the question with an image of the array as I see it. Commented May 1, 2016 at 0:43

2 Answers 2

3

First, it seems that you have a different array for the Name, I wonder why you don't have something like:

 ["Person"] => array(11) {
    ["id"] => int(38482818123)
    ["weight"] => int(140)
    ["height"] => int(65)
    ["firstname"] => string(4) "John"
    ["lastname"] => string(5) "Smith"
    }

As per that case, the array index with the key "firstname" , "lastname" might not be set for some person. So you can check if that index is set using

isset(); function

Try:

foreach($personArray as $person)
{
    if (isset($person['firstname']) && isset($person['lastname']) ) // You may use or as well ||
    {
        echo "<img src='image-a.png'>";
    }
    else
    {
        echo "<img src='image-b.png'>";
    }


   //  Or as a short hand if statement:

  echo (isset($person['firstname'])&& isset($person['lastname'])) ? "<img src='image-a.png'>" : "<img src='image-b.png'>";
}

Edit: 1 You still seem to be using two different arrays: Person and Name. Doing this, you cannot say which particular person has the name values or not: For eg: if you have 10 persons and have the Name array with firstname, lastname for just 7 person then you will have person[0], person[1].....person[9] and Name[0]...Name[6]. As per your construct, there is no any reference / link between the Person and Name array. Suppose 1st person has Name, then Person[0] and Name[0] would represent the same person. But if the first 3 Person do not have Name, then Person[3] will have Name[0]...and so on, hence, it is not possible to identify to which Person does a particular Name array belong to. And Note: you cannot use Name["firstname"] inside the foreach() of Person. Because, your name array would be of the form Name[0]["firstname"], Name[0]["lastname"] and so on.

Bottom Line:

If possible, try to use / include the firstname, lastname in the Person array itself. That way, when iterating / looping through the Person array using foreach() you can check if each of these person have firstname , lastname or not: Hope this is clear.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried your answer but I still got both images. I might have portrayed the array poorly in my question but I took a picture of it if you would like to take another look.
Your code also works with my array outside of the loop, thank you!
Well great then :)
1

You need to use empty() like below:-

 if(!empty($personArray['person']['Name'])){ 
    //image a code
 }else{
    //image b code
 }

4 Comments

It's still showing both, I don't understand why it is doing that for an array with 1 person.
Can I use this outside of the loop?
my array is json_decoded inside a variable named $result. I just tried your solution outside of my loop and it gives image 2 if the name field is filled in.
Let me take a picture of my exact array and post it in my question, I'm still getting the second image when the field is filled in. I might be portraying it wrong in my question.

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.