Multidimensional Array PHP Implode(多维数组 PHP 内爆)
问题描述
就我的数据结构而言,我有一个communications数组,每个communications_id本身包含三个信息:id、score和content.
In terms of my data structure, I have an array of communications, with each communications_id itself containing three pieces of information: id, score, and content.
我想内爆这个数组以获得一个逗号分隔的 id 列表,我该怎么做?
I want to implode this array in order to get a comma separated list of ids, how do I do this?
推荐答案
PHP 5.5 更新
PHP 5.5 引入了 array_column 这是一整类 array_map 用法的便捷快捷方式;这里也适用.
Update for PHP 5.5
PHP 5.5 introduces array_column which is a convenient shortcut to a whole class of array_map usage; it is also applicable here.
$ids = array_column($communications, 'id');
$output = implode(',', $ids);
原答案
您需要从您的通信数组中制作一个仅包含 id 的数组.那么内爆将是微不足道的.
Original answer
You need to make an array of just ids out of your array of communications. Then the implode would be trivial.
提示:该函数是array_map.
解决方案:
假设为 PHP 5.3,否则您必须将回调编写为字符串.
Assumes PHP 5.3, otherwise you 'd have to write the callback as a string.
$ids = array_map(function($item) { return $item['id']; }, $communications);
$output = implode(',', $ids);
这篇关于多维数组 PHP 内爆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多维数组 PHP 内爆
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
