【问题标题】:Build query string with array data使用数组数据构建查询字符串
【发布时间】:2017-05-15 12:43:38
【问题描述】:

我将一个包含一些答案的对象传递给我的函数。然后我执行以下操作来创建一个关联数组

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

这会导致这样的数组

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

我需要做的是使用上述数据构建 API 调用。 API URL 看起来像这样

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

其中题号为数组左边的值,=号后面的部分为数组右边的值。

使用我拥有的数组创建此 URL 的最佳方法是什么?

谢谢

【问题讨论】:

标签: php arrays


【解决方案1】:

这可以通过php的http-build-query方法本身来实现。第二个参数接受前缀添加到键。 键应该是数字类型

你可以像这样使用http-build-query

http_build_query($field_data, 'question');

【讨论】:

  • ;) 称之为“奖牌”
猜你喜欢
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 2012-09-04
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
相关资源
最近更新 更多