ju1985oh ju1985oh - 1 year ago 46
PHP Question

Checking Array For Multiple Values and Change Output If Value Exists

After wrenching my brain for multiple hours, trying multiple solutions and checking Stack Overflow for PHP-based solutions to my problem -- I'm reaching out for help.

What I'm trying to accomplish (via PHP):

  1. Check array to see if certain values exist.

  2. For each value that does -> show green checkmark icon

  3. If the value doesn't exist -> show red X icon

My problem:

  1. I'm performing the above steps, checking for multiple values.

  2. I'm trying to call the API endpoint that I receive the array data from only one time, instead of once per check.

Example Setup:

amenitiesArray = ['amenityX', 'smoking', 'pets', 'amenityY', 'amenityZ', 'amenityA', 'linens', 'amenityB', 'wifi']

If amenitiesArray contains smoking, show a green check.

If amenitiesArray doesn't contain smoking, show a red X.

Perform the above 2 steps, checking for multiple other values...like...

If amenitiesArray contains pets, show a green check.

If amenitiesArray doesn't contain pets, show a red X.

All values I'm looking for: linens, wifi, pets and smoking.

Helpful Note: The amenitiesArray values are different for each product/rental property. The amenitiesArray may or may not contain the values I'm checking for.

Ultimately, for each rental property, I'm trying to display a table that looks like this: http://i.imgur.com/jEnXUb6.png

What is the best way to achieve this with PHP?

Answer Source

just use in_array() I'm always checking for the same 4 values (smoking, linens, wifi, linens). For each of the 4 values, I need to display a green check if it exists in the array, or a red X if it does not.

As per comment this what you need to do using in_array

`$amenitiesArray = ['amenityX', 'smoking', 'pets', 'amenityY', 'amenityZ', 'amenityA', 'linens', 'amenityB', 'wifi'] //from api`

 $your_data = ['smoking','pets','linens','wifi'];

echo "<table boder='1px'>";
echo "<thead>";
echo "<tr><th>column1</th><th>colum2</th></tr>";
echo "</thead>";
echo "<tbody>";
foreach($your_data as $row)

    echo "<tr><td>".$row."</td>";

           echo "<td>green icon html</td>";
          echo "<td>red icon html</td>";
      echo "</tr>";

  echo "</tbody>";
  echo "</table>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download