Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

explode true and false string

Status
Not open for further replies.

KryptoS

Programmer
Feb 7, 2001
240
BE
Hello there,

I have to following function:

$aChecked = explode(',', $checked);

if $checked contains something like 'true,false,false,true' it returns an array with the values true and false.

But if $checked only contains e.g. 'true'
It returns an array with the value 1. $aChecked[0] = 1 (in case of 'false' it returns 0).

Am I doing something wrong? Or how can I get also true if I'm only exploding true or false?

The One And Only KryptoS
 
are you looking to get the string literals 'true' and 'false' returned or have i misunderstood your question?
 
Yes I want the string 'true' or 'false' back.

'cause when I've $checked = 'true,false,false' and do an explode. I also get a string true and false back. So I want the same if I have only $check = 'true'



The One And Only KryptoS
 
hmm. that's difficult to do neatly as boolean true converts to '1' and boolean false to '' on a strval();

i'm surprised by the behaviour that you're seeing though. but anyway, this should work:

Code:
print_r(_explode($string));
function _explode($string){
 $array = explode($string);
 foreach ($array as &$val){
   $val = ($val === true) ? 'True' : 'False';
 }
 return $val;
}
 
Why does he converts it to 0 and 1 only if I have one value in my string ($checked = "true") and not when I have multiple values in my string ($checked = "true,false,false")?

my code before:
$Checked = "true,false";
$aChecked = explode(',', $Checked);
echo $aChecked[0]; (printed: "true")

$Checked = "true";
$aChecked = explode(',', $Checked);
echo $aChecked[0]; (printed: 1)


I managed to get what I wanted by doing this.

if(strpos($Checked,','))
$aChecked = explode(',', $Checked);
else
$aChecked[0] = $Checked;

this gives me always true or false values in aChecked.

The One And Only KryptoS
 
my code will work for you.

as to why there is this inconsistent behaviour, i do not know. it sounds like a bug.
 
You shouldn't need to do anything. The default behavior of explode() is exactly what you're looking for.
PHP manual said:
If delimiter contains a value that is not contained in string , then explode() will return an array containing string .
So explode(',', 'true') should return a one-element array containing the string 'true'.

Are you sure you're using exactly the code you posted? You would get "1" or "0" if you passed explode() a boolean true or false, but if you passed it a string, you should get back the string.
 
OK, I'm wrong. It gives back true and false...

What went wrong...

I'm using JSON (Zend). So in my javascript I do:

Ext.Ajax.request(
{
waitMsg: 'Wait...',
url: webroot+module+'/'+controller+'/savevalideren/',
params: {
json : "1",
checked: [check]
},
...

In my php script I had:

$Checked = Zend_Json::decode($this->_request->getParam('checked'));

Json transfer the true/false to 1/0. When it checked has 'true,false,false' json keeps it like that. So I let out the Json::decode and now everything is going fine.

thanks guys!

The One And Only KryptoS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top