2

I have array like this one.

$fields = array(
            'user[facebook_gender]' => urlencode("female"),
            'user[facebook_likes]' => array(1,2,3,4,5,6)
);

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string=rtrim($fields_string, '&');

then I get

user[facebook_gender]=female&user[facebook_likes]=Array

It just show "Array", however I want to show the content of Array,

1
  • 1
    What is your question? How to turn an array into a string? How to create a url from an array? Commented Jul 17, 2014 at 14:48

3 Answers 3

1

Have a look at the serialize function

http://php.net/manual/en/function.serialize.php

For example:

foreach($fields as $key=>$value) {
  if (is_array($value)) $value = serialize($value);
  $fields_string .= $key.'='.$value.'&';
}
$fields_string=rtrim($fields_string, '&');

Note that you have to use unserialize() on your target site, to make use of the value again.

Sign up to request clarification or add additional context in comments.

Comments

1

Actually, http_build_query does exactly what you need.

Comments

0

Use implode on the array:

foreach($fields as $key=>$value) {
    if(is_array($value))
    {
         $value = implode($value,",");
    }
    $fields_string .= $key.'='.$value.'&';
}
$fields_string=rtrim($fields_string, '&');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.