問題描述
就我的數據結構而言,我有一個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 內爆的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!