Multidimensional Array PHP Implode

前端 未结 5 753
梦如初夏
梦如初夏 2020-12-02 17:48

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.

5条回答
  •  时光说笑
    2020-12-02 18:04

    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);
    

    Original answer

    You need to make an array of just ids out of your array of communications. Then the implode would be trivial.

    Hint: the function for that is array_map.

    Solution:

    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);
    

提交回复
热议问题