0

I pass my function an object that contains some answers. I then do the following to create an associative array

$field_data = array();
foreach($submission->answers as $answer) {
    $field_data[$answer->question_id] = $answer->text + 1;
}

This results in array like so

array:15 [▼
  1 => 3
  2 => 4
  3 => 2
  4 => 5
]

What I need to do is build an API call using the above data. The API URL would look something like this

someAPI.com?api.php?function=calculatePrice&question1=3&question2=4&question3=2&question4=5

Where the question number is the value on the left side of the array, and the part after the = sign is the value on the right of the array.

What would be the best way to create this URL using the array I have?

Thanks

1

1 Answer 1

7

This can be achieved with the http-build-query method itself of php. The second param accepts the prefix to prepend to the key. The key should be of numeric type

You can use the http-build-query like this in your case

http_build_query($field_data, 'question');
Sign up to request clarification or add additional context in comments.

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.