多维数组PHP放大 [英] Multidimensional Array PHP Implode

查看:59
本文介绍了多维数组PHP放大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我的数据结构而言,我有一个通信数组,每个communications_id本身包含三段信息:id,得分和内容.

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.

提示:的功能是 .

解决方案:

假设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放大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆