内含“和”到底? [英] Imploding with "and" in the end?

查看:59
本文介绍了内含“和”到底?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的数组:

Array
(
    [0] => Array
        (
            [kanal] => TV3+
            [image] => 3Plus-Logo-v2.png
        )

    [1] => Array
        (
            [kanal] => 6\'eren
            [image] => 6-eren.png
        )  
    [2] => Array
        (
            [kanal] => 5\'eren
            [image] => 5-eren.png
        )

)

它可能会扩展到更多子数组。

It may expand to several more subarrays.

如何制作类似以下列表: TV3 +,6'eren和5'eren

How can I make a list like: TV3+, 6'eren and 5'eren?

推荐答案

由于数组可能会更深入,因此最好使用诸如 array_walk_recursive()

As array could potentially be to further depths, you would be best off using a recursive function such as array_walk_recursive().

$result = array();

array_walk_recursive($inputArray, function($item, $key) use (&$result) {

    array_push($result, $item['kanal']);
}

然后将其转换为逗号分隔的字符串,并用和分隔最后一个两项

To then convert to a comma separated string with 'and' separating the last two items

$lastItem = array_pop($result);

$string = implode(',', $result);

$string .= ' and ' . $lastItem;

这篇关于内含“和”到底?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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