0

i'm trying to create a method for an http request using this code, but it only picks up 1 set of key and value pair ("key_1"=>"value_1"). how do i get all the array keys and their values?

<?php $userPostData = [ 'key_1' => "value_1", 'key_2' => "value_2" ]; foreach($userPostData as $key => $value) { $response = $client->post($apiUrl, [ 'json' => [ $key => urlencode($value), ], 'verify' => false ]); } 

1 Answer 1

1

You can use array_map to urlencode the array values. Also, I think you forgot to encode the array to json, so I did it for you:

$userPostData = [ 'key_1' => "value_1", 'key_2' => "value_2" ]; $userPostData = array_map('urlencode', $userPostData); $response = $client->post($apiUrl, [ 'json' => json_encode($userPostData), 'verify' => false ]); 
Sign up to request clarification or add additional context in comments.

2 Comments

seems much better and accurate than mine.+1
yes i'm not getting exactly what i want but this is taking me somewhere, thanx.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.