I have a Java string array lets say something like this:
String [] distance = {"40","43","32","313","3123"};
I sent this array as a POST message to the server where a php file reads this array.
nameValuePairs.add(new BasicNameValuePair("distance",distance);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
This entire array will be read in this variable: $_POST['distance']
I am unable to access an element in this array using $_POST['distance[0]'].
How can i access only one specified element from this variable
3 Answers 3
You have plenty of options.
- Encode the data as JSON on the Java side and decode the string on the PHP side.
- Post the data as a string with Java that is detected as array in PHP:
distance[]=40&distance[]=43&distance[]=32&... - Work on the actual string you get and decode the native form created by Java, i.e. comma separated list of values.
Comments
You should read in the array element in PHP as:
$distance = explode(',', $_POST['distance']);
echo $distance[0];
Comments
I am not sure, but if you send string via post, you can try to replace data until you get something like this
40,43,32,313,3123
and after you can use php function explode.
String [] distance = {40,43,32,313,3123};in a .java source file I get the errorType mismatch: cannot convert from int to String— also, Java and PHP are both generally server-side languages. If you're sending this array as a POST could you mean javascript rather than java?String [] distance = {40,43,32,313,3123};isn't valid javascript syntax either.