Mr.Smithyyy
Mr.Smithyyy

Reputation: 1329

Echo different images depending on array value

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

Upvotes: 2

Views: 97

Answers (2)

Captain Red
Captain Red

Reputation: 1171

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.

Upvotes: 3

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You need to use empty() like below:-

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

Upvotes: 1

Related Questions