php仅对第一列编码json数组

发布于 2024-10-20 22:26:03 字数 885 浏览 7 评论 0原文

我需要仅对包含 n 行的数组的第一列进行编码,

Array
 (
[0] => Array
    (
        [FamilyName] => Dikkartan, Bartu
        [Balance] => -2446.91
        [Mobile] => 0444497
        [HAddress] => 2/6 Tramsway Street
        [HSuburb] => Rosebery
        [HState] => NSW
        [HPCode] => 

    )

[1] => Array
    (
        [FamilyName] => King, Alan & Luka
        [Balance] => -1676
        [Mobile] => 0433 6090
        [HAddress] => 46/12 Hayberry Street
        [HSuburb] => Crows Nest
        [HState] => NSW
        [HPCode] => 

    ) ...

因此我需要仅使用第一个值 [familyName] 创建一个数组, - 我希望将此与 jquery 自动完成一起使用,因此在从自动完成中选择名称后,我使用此名称在完整数组中再次搜索,并使用数组中的数据填充一些文本字段

  • 我需要搜索在数组中,因为数据是从sql中获取的,但我无法在数据库中搜索,只需使用数组数据,

  • 这是一个好的路径还是我应该采取什么其他路径来对名称进行一些自动完成,然后用该数组中的数据填充我的文本字段

非常感谢!

I need to enconde just the first column of an array with n rows,

Array
 (
[0] => Array
    (
        [FamilyName] => Dikkartan, Bartu
        [Balance] => -2446.91
        [Mobile] => 0444497
        [HAddress] => 2/6 Tramsway Street
        [HSuburb] => Rosebery
        [HState] => NSW
        [HPCode] => 

    )

[1] => Array
    (
        [FamilyName] => King, Alan & Luka
        [Balance] => -1676
        [Mobile] => 0433 6090
        [HAddress] => 46/12 Hayberry Street
        [HSuburb] => Crows Nest
        [HState] => NSW
        [HPCode] => 

    ) ...

so I need to make an array just with the first value [familyName],
- I want to have this to use with a jquery autocomplete, so after having the name selected from autocomplete, I use this name to search again in the complete array and fill some textfields with the data from the array

  • I need to search in the array as data is taken from sql, but I cannot search in the db, just use the array data,

  • Is this a good path or what other path should I take to do some autocomplete for the name and then have my textfields filled with the data from that array

thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞳孔里扚悲伤 2024-10-27 22:26:03

使用 array_map 您可以将数组缩减为仅该元素。

$familyNames = array_map(function($object) {
    return $object['FamilyName'];
}, $objects);

json_encode($familyNames);

这使用 php 5.3 中提供的匿名函数,否则您需要使用 create_function 或对您已创建的现有函数的引用。请参阅 PHP 回调 文档。

create_function('$object', 'return $object["FamilyName"];')

或者,您可以使用简单的 foreach 循环:

$familyNames = array();
foreach($objects as $object)
    $familyNames[] = $object['FamilyName'];

Using array_map you can reduce the array to just that element.

$familyNames = array_map(function($object) {
    return $object['FamilyName'];
}, $objects);

json_encode($familyNames);

This uses an anonymous function available in php 5.3 otherwise you need to use create_function or a reference to an existing function you've created. See the PHP callback documentation.

create_function('$object', 'return $object["FamilyName"];')

Alternatively you could use a simple foreach loop:

$familyNames = array();
foreach($objects as $object)
    $familyNames[] = $object['FamilyName'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文