1

I have an API that I need to write to that expects

$foo = array(
  'tags[]' => array('one','two','three','four')
);

My array looks like this

Array
(
    [0] => one
    [1] =>  two
    [2] =>  three
    [3] =>  four
    [4] =>  five
    [5] =>  six
)

I've tried adding the array

$foo = array(
  'tags[]' => array($arr)
);

But this prints 'Array' once in the database. How do I add the values from $arr to the tags[]?

9
  • 3
    why is there ' after tags[]? Commented May 21, 2014 at 15:46
  • Correct your code before get an answer. Commented May 21, 2014 at 15:47
  • If you see Array, then you are converting your array to a string. You can't store an array in a database, only strings. Maybe you want to json_encode($arr)? Commented May 21, 2014 at 15:47
  • Looks like you've got an extra ' and you unintentionally stringified the array($arr) that you pass, which then gets inserted. Commented May 21, 2014 at 15:48
  • 1
    Use json_encode on array to save on database, and json_decode to revert from db. Commented May 21, 2014 at 15:48

1 Answer 1

1

Your tags[] array is one level too deep. It should be:

$foo = array(
  'tags[]' => $arr
);

Note how $arr is not wrapped in array().

If you don't remove the array() then it looks like:

$foo = array(
  'tags[]' => array(
      array('one', 'two', 'three')
  )
);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply, unfortunately this give me {"messages":{"tags":["This value is not valid.","This value is not valid.","This value is not valid.","This value is not valid.","This value is not valid."]},"error":"invalid"}
You will have to look at the API source code or documentation to find out the problem. Either it wants a different format or the content of the tags you're passing is invalid according to the API.
I know array('one','two','three','four') work. just that I doesn't like the key value of the array, it just expects value.
array('one','two','three','four') is same as array('0'=>'one', '1'=>'two', '2'=>'three', '3'=>'four', '4'=>'five', '5'=>'six')

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.