PDA

View Full Version : [Solved] how to determine if a value is in a json string array?



madkris
10 Aug 2009, 10:12 PM
Hi all,
I was just wondering if any one could help me or give me a tip with this problem.

I would like to determine if a value is in the json string array, so I can disable or enable a certain button.


var obj = eval($temp);

disabled: function () {
return obj.result == 'value1' ? 'false' : true;
}This is how my json string looks like.

{"results":["value1", "value2", "value3", "value4"]}In php, something like this would work,

disabled: <?php echo in_array('value1',$php_array) ? 'false' : 'true' ?> Of course my array in php looks different

array( [0] => 'value1' [1] => 'value2' [2] => 'value3' [3] => 'value4')Regards,
kris

13 Aug 2009, 9:32 AM
paste this in firebug:

var r = {"results":["value1", "value2", "value3", "value4"]};

r.results.indexOf('value2')

aconran
13 Aug 2009, 10:10 AM
Or you could decode the JSON first so that you were dealing with a real array and then use array's indexOf method.

madkris
14 Aug 2009, 4:18 PM
@jgarcia - going to try that as soon as i can get my firebug console to run... not sure why and its the first time ive come across this problem... gonna try google...
uncaught exception: _FirebugCommandLine init failed in about:blank because TypeError: window.loadFirebugConsole is not a function

@aconran - i actually had the same idea.. problem was that I was using php's json_decode and didn't get it to work... i had forgotten that idea thanks for the reminder... with a little bit of searching I came across Ext.util.JSON.decode. Gonna try this one and see if i can get it to work...

var myArray = {"results":["value1", "value2", "value3", "value4"]};
if I decode myArray, it would give me the array below right?

["value1", "value2", "value3", "value4"]

thanks for the replies :)