Jump to content

Can’t figure out what’s wrong with this array


kentonator

Recommended Posts

Ok, to be fair, I’m brand new to PHP and just trying to piece this together for a friends website. The idea of this code is if a value of 1 is given in the data entry it would return house and so on. But right now, even if it’s a 3 it’s just returning house instead of Mobile Home.  I’ve tried everything I know and googled for ever, but if you don’t know the right language to google, it’s impossible.  I have a feeling this is a simple fix and I just don’t know it.

function property_structure_type ($data){ $data = array("1", "2", "3", "4", "5", "6", "7");
if (in_array("1", $data)) {
    return "House";
}
if (in_array("2", $data)) {
    return "Cabin";
}
if (in_array("3", $data)) {
    return "Mobile Home";
}
if (in_array("4", $data)) {
    return "Apartment";
}
if (in_array("5", $data)) {
    return "Condominium";
}
if (in_array("6", $data)) {
    return "Townhome";
}
if (in_array("7", $data)) {
    return "None";
}

 

Link to comment
Share on other sites

It' simple really. You are providing an array, it matches the 1 and returns back to the caller so the rest of the if's are not processed.

Try this...

<?php declare(strict_types=1);

function property_structure_type ($data)
{
 if (in_array("1", $data)) {
    return "House";
}
if (in_array("2", $data)) {
    return "Cabin";
}
if (in_array("3", $data)) {
    return "Mobile Home";
}
if (in_array("4", $data)) {
    return "Apartment";
}
if (in_array("5", $data)) {
    return "Condominium";
}
if (in_array("6", $data)) {
    return "Townhome";
}
if (in_array("7", $data)) {
    return "None";
}
}

echo property_structure_type ([2]);

 

Link to comment
Share on other sites

Of course - no matter what value you pass, "1" is in the array so it returns "House". Functions exit as soon as a value is returned.

Perhaps

function property_structure_type ($data)
{
    $types = [ 1 => 'House',
               2 => 'Cabin',
               3 => 'Mobile Home',
               4 => 'Apartment',
               5 => 'Condominium',
               6 => 'Townhome'
             ];
    return isset($types[$data]) ? $types[$data] : 'None';
}

echo property_structure_type(3);    //-> Mobile home

 

Link to comment
Share on other sites

3 hours ago, Barand said:

Of course - no matter what value you pass, "1" is in the array so it returns "House". Functions exit as soon as a value is returned.

Perhaps


function property_structure_type ($data)
{
    $types = [ 1 => 'House',
               2 => 'Cabin',
               3 => 'Mobile Home',
               4 => 'Apartment',
               5 => 'Condominium',
               6 => 'Townhome'
             ];
    return isset($types[$data]) ? $types[$data] : 'None';
}

echo property_structure_type(3);    //-> Mobile home

 

This worked perfectly, thanks a ton!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.