【问题标题】:how to get array values in php?如何在php中获取数组值?
【发布时间】:2022-01-28 00:33:12
【问题描述】:

如何通过城市名称自动完成搜索获得城市编号

例如:

纽约:48.9436.92

数组:

<?php
$city=array(
array('New York','48.97','36.92'),
array('Los Angeles','52.75','27.41'),
array('California','34.05','-118.24'),
array('Houston','52.67','31.18'),
array('Philadelphia','47.47','32.94'),
array('Phoenix','49.84','37.41')
);
?>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<form method="post">
<input id="listcity" type="text" name="car">
</form>
<script>
$(function() {
var availableTags =  <?php echo json_encode($city,JSON_UNESCAPED_UNICODE); ?>;
    $( "#listcity" ).autocomplete({
    source: availableTags
    });
});
</script>

谢谢

【问题讨论】:

  • 如果你像array('New York : 48.97 and 36.92'),这样格式化数据会怎样
  • 不,数据必须分开。
  • 这与ajax 有什么关系?你在使用 jQuery UI 自动完成功能吗?为什么你需要城市“数字”,你不会只自动完成城市名称吗?
  • 是的,我使用 jQuery UI 自动完成功能。我想通过城市名称访问城市的经纬度。
  • JQuery UI 自动完成有documentationcustom data example 可能适合你

标签: php arrays ajax autocomplete


【解决方案1】:

也许您可以使用 php 在您想要的 javascript 的任何部分返回值,因为 PHP 在 javascript 之前运行。所以:

例如:

如果你想要 48.97,你可以使用这个echo $city[0][1];

对于其他值,只需使用 +1 位置:echo $city[0][2];

然后您只需将它们存储在这样的 javascript 变量中:

var numbers = &lt;? echo $city[0][1];?&gt;;

此变量 numbersoutput 为 48.97。

【讨论】:

  • 我想通过搜索得到这些数字。
猜你喜欢
  • 2016-08-20
  • 2018-11-24
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多