/    Sign up×
Community /Pin to ProfileBookmark

convert to boolean

I have a function that returs a bunch of information in an array. Sore of the values are “T” or “F”. How can I convert these to PHP booleans (true, false)?

Thanks

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@pyroDec 26.2003 — How about something like this:

[code=php]<?PHP
$foo = "F";
$bar = ($foo == "T") ? true : false;
?>[/code]
Copy linkTweet thisAlerts:
@jrthor2authorDec 26.2003 — Ok, but would I have to do it for every variable in my array? I have an array that comes back with this information (this is the sml results from a webservice I am writing, but this is the infor from that array):

<store_number xsi:type="xsd:int">04260</store_number>

<addr1 xsi:type="xsd:string">4299 Union Deposit Road </addr1>

<city xsi:type="xsd:string">Harrisburg </city>

<state xsi:type="xsd:string">PA</state>

<zip xsi:type="xsd:string">17111</zip>

<phone_fe xsi:type="xsd:string">(717) 564-6750</phone_fe>

<phone_ph xsi:type="xsd:string">(717) 564-6750</phone_ph>

<phone_phf xsi:type="xsd:string">(717) 564-4839</phone_phf>

<distance xsi:type="xsd:string">1.23</distance>

<convenience_foods xsi:type="xsd:boolean">T</convenience_foods>

<rite_express xsi:type="xsd:boolean">F</rite_express>

<one_hour_photo xsi:type="xsd:boolean">T</one_hour_photo>

<ra_24_hour xsi:type="xsd:boolean">T</ra_24_hour>

<drive_thru_rx xsi:type="xsd:boolean">T</drive_thru_rx>

<rite_rewards xsi:type="xsd:boolean">T</rite_rewards>

<gnc xsi:type="xsd:boolean">T</gnc>

<western_union xsi:type="xsd:boolean">T</western_union>

<hours_fe_mon xsi:type="xsd:string">OPEN 24 HOURS </hours_fe_mon>

<hours_fe_tue xsi:type="xsd:string">OPEN 24 HOURS </hours_fe_tue>

<hours_fe_wed xsi:type="xsd:string">OPEN 24 HOURS </hours_fe_wed>

<hours_fe_thu xsi:type="xsd:string">OPEN 24 HOURS </hours_fe_thu>

<hours_fe_fri xsi:type="xsd:string">OPEN 24 HOURS </hours_fe_fri>

<hours_fe_sat xsi:type="xsd:string">OPEN 24 HOURS </hours_fe_sat>

<hours_fe_sun xsi:type="xsd:string">OPEN 24 HOURS </hours_fe_sun>

<hours_rx_mon xsi:type="xsd:string">OPEN 24 HOURS </hours_rx_mon>

<hours_rx_tue xsi:type="xsd:string">OPEN 24 HOURS </hours_rx_tue>

<hours_rx_wed xsi:type="xsd:string">OPEN 24 HOURS </hours_rx_wed>

<hours_rx_thu xsi:type="xsd:string">OPEN 24 HOURS </hours_rx_thu>

<hours_rx_fri xsi:type="xsd:string">OPEN 24 HOURS </hours_rx_fri>

<hours_rx_sat xsi:type="xsd:string">OPEN 24 HOURS </hours_rx_sat>

<hours_rx_sun xsi:type="xsd:string">OPEN 24 HOURS </hours_rx_sun>

<store_date_open xsi:type="xsd:string">1996-08-30</store_date_open>

<store_date_relo xsi:type="xsd:string">9999-12-31</store_date_relo>

<store_date_reopen xsi:type="xsd:boolean">9999-12-31</store_date_reopen>

<picture_maker xsi:type="xsd:boolean">T</picture_maker>

<photo_ramp xsi:type="xsd:boolean">F</photo_ramp>

<store_status xsi:type="xsd:string">00</store_status>

<detail_desc xsi:type="xsd:string">Located At 4299 Union Deposit Road Across From Comfort Inn </detail_desc>

<division xsi:type="xsd:string">E</division>
Copy linkTweet thisAlerts:
@pyroDec 26.2003 — Alright, try this, then:

[code=php]<?PHP
$foo = array("F", "T", "F", "qux");
foreach ($foo as $bar => $baz) {
if ($baz == "T") {
$foo[$bar] = true;
}
else if ($baz == "F") {
$foo[$bar] = false;
}
}
echo "<pre>";
print_r($foo);
echo "</pre>";
?>[/code]
Copy linkTweet thisAlerts:
@jrthor2authorDec 26.2003 — Here is my code, but I am not getting anything echoed out?? This will replace the T and F with true/false in my array, right?

function rastore($zip){

if (!strlen($zip)) {
return new soap_fault('Client','','Please supply a valid zip code.');
} else {
//call class to call mapquest
$CC=new mystorelookup;
$CC->find_stores_by_zip($zip);
$cnt = count($CC->$stores);
if ( count($CC->$stores) > 0 ) {
//print_r($CC->$stores);
$results[$i] = $CC->$stores[$i];
} else if ( count($CC->$stores) == 0 ) {
return new soap_fault('Client','','No results were found');
}
}

//$back = "it works";

//print_r($results);

return $results[$i];

foreach ($foo as $bar => $results) {

$foo[$bar] = ($results == "T") ? true : $results;

$foo[$bar] = ($results == "F") ? false : $results;

}

echo "<pre>";

print_r($foo);

echo "</pre>";

}
Copy linkTweet thisAlerts:
@pyroDec 26.2003 — Looks like you grabbed what I had before I edited. Sorry about that.

Try this one:

[code=php]foreach ($result as $key => $value) {
if ($value == "T") {
$result[$key] = true;
}
else if ($value == "F") {
$result[$key] = false;
}
}
echo "<pre>";
print_r($result);
echo "</pre>";[/code]
Copy linkTweet thisAlerts:
@jrthor2authorDec 26.2003 — I still just get a blank screen, no values printed out?? Do I have your code in the right spot, after the return $results[$i]; code? Even if I put it before that, it doesn't give me anything?? My code is below:

function rastore($zip){

if (!strlen($zip)) {
return new soap_fault('Client','','Please supply a valid zip code.');
} else {
//call class to call mapquest
$CC=new mystorelookup;
$CC->find_stores_by_zip($zip);
$cnt = count($CC->$stores);
if ( count($CC->$stores) > 0 ) {
//print_r($CC->$stores);
$results[$i] = $CC->$stores[$i];
} else if ( count($CC->$stores) == 0 ) {
return new soap_fault('Client','','No results were found');
}
}

//$back = "it works";

//print_r($results);

return $results[$i];

foreach ($results as $key => $value) {

if ($value == "T") {

$results[$key] = true;

}

else if ($value == "F") {

$results[$key] = false;

}

}

echo "<pre>";

print_r($results);

echo "</pre>";

}

Here is what the array looks like:

Array

(

[] => Array

(

[04260] => Array

(

[name] => Rite Aid


[store_number] => 04260

[addr1] => 4299 Union Deposit Road


[city] => Harrisburg


[state] => PA

[zip] => 17111

[phone_fe] => (717) 564-6750

[phone_ph] => (717) 564-6750

[phone_phf] => (717) 564-4839

[distance] => 1.23

[convenience_foods] => T

[rite_express] => F

[one_hour_photo] => T

[ra_24_hour] => T

[drive_thru_rx] => T

[rite_rewards] => T

[gnc] => T

[western_union] => T

[hours_fe_mon] => OPEN 24 HOURS


[hours_fe_tue] => OPEN 24 HOURS


[hours_fe_wed] => OPEN 24 HOURS


[hours_fe_thu] => OPEN 24 HOURS


[hours_fe_fri] => OPEN 24 HOURS


[hours_fe_sat] => OPEN 24 HOURS


[hours_fe_sun] => OPEN 24 HOURS


[hours_rx_mon] => OPEN 24 HOURS


[hours_rx_tue] => OPEN 24 HOURS


[hours_rx_wed] => OPEN 24 HOURS


[hours_rx_thu] => OPEN 24 HOURS


[hours_rx_fri] => OPEN 24 HOURS


[hours_rx_sat] => OPEN 24 HOURS


[hours_rx_sun] => OPEN 24 HOURS


[store_date_open] => 1996-08-30

[store_date_relo] => 9999-12-31

[store_date_reopen] => 9999-12-31

[picture_maker] => T

[photo_ramp] => F

[store_status] => 00

[detail_desc] => Located At 4299 Union Deposit Road Across From Comfort Inn

[division] => E

)

}

}
Copy linkTweet thisAlerts:
@pyroDec 26.2003 — Try adding return $results; after the foreach loop. That will return the array.
Copy linkTweet thisAlerts:
@jrthor2authorDec 26.2003 — Not sure you saw my edited post, I added what the array looks like before doing this. It doesn't work if I put return $results[$i} after it either.
×

Success!

Help @jrthor2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...