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]);

 

Edited by benanamen
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.