Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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

share|improve this question
    
If I put your sample String [] distance = {40,43,32,313,3123}; in a .java source file I get the error Type 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? – Stephen P Jul 8 '14 at 21:22
    
On the other hand, String [] distance = {40,43,32,313,3123}; isn't valid javascript syntax either. – Stephen P Jul 8 '14 at 21:25
    
@StephenP You are right. I have edited my java code. – user3760741 Jul 8 '14 at 21:26

3 Answers 3

up vote 1 down vote accepted

You should read in the array element in PHP as:

$distance = explode(',', $_POST['distance']);
echo $distance[0];
share|improve this answer

You have plenty of options.

  1. Encode the data as JSON on the Java side and decode the string on the PHP side.
  2. Post the data as a string with Java that is detected as array in PHP: distance[]=40&distance[]=43&distance[]=32&...
  3. Work on the actual string you get and decode the native form created by Java, i.e. comma separated list of values.
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.