Frederik Spang
Frederik Spang

Reputation: 3454

Imploding with "and" in the end?

I have an array like:

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.

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

Upvotes: 1

Views: 123

Answers (7)

David Barker
David Barker

Reputation: 14620

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;

Upvotes: 4

jayxhj
jayxhj

Reputation: 3028

if you simply pass in the given array to implode() function,you can't get even the value of the subarray. see this example

assuming your array name $arr,codes are below

   $length = sizeof ( $arr );
   $out = '';
   for($i = 0; $i < $length - 1; $i ++) {
       $out .= $arr [$i] ['kanal'] . ', ';
   }
   $out .= ' and ' . $arr [$length - 1] ['kanal'];

Upvotes: 2

Dimag Kharab
Dimag Kharab

Reputation: 4519

Here you go ,

$myarray =  array(
                    array(
                            'kanal' => 'TV3+',
                            'image' => '3Plus-Logo-v2.png'
                          ),
                    array(
                            'kanal' => '6\'eren',
                            'image' => '6-eren.png'
                          ),
                    array(
                            'kanal' => '5\'eren',
                            'image' => '5-eren.png'
                          ),

                    );

foreach($myarray as $array){
    $result_array[] = $array['kanal'];
}

$implode = implode(',',$result_array);

$keyword = preg_replace('/,([^,]*)$/', ' & \1', $implode);

echo $keyword;

Upvotes: 2

Rikesh
Rikesh

Reputation: 26431

Took some time but here we go,

$arr = array(array("kanal" => "TV3+"),array("kanal" => "5\'eren"),array("kanal" => "6\'eren"));
$arr =  array_map(function($el){ return $el['kanal']; }, $arr);
$last = array_pop($arr);
echo $str = implode(', ',$arr) . " and ".$last;

DEMO.

Upvotes: 3

Barif
Barif

Reputation: 1552

I think it would work to you:

$data = array(
    0 =>['kanal' => 'TV1+'],
    1 =>['kanal' => 'TV2+'],
    2 =>['kanal' => 'TV3+'],
);
        $output = '';
        $size = sizeof($data)-1;
        for($i=0; $i<=$size; $i++) {
             $output .= ($size == $i && $i>=2) ? ' and ' : '';
             $output .= $data[$i]['kanal'];
             $output .= ($i<$size-1) ? ', ' : '';
        }

echo $output;

//if one chanel:
// TV1
//if two chanel:
// TV1 and TV2
//if three chanel:
// TV1, TV2 and TV3
//if mote than three chanel:
// TV1, TV2, TV3, ... TV(N-1) and TV(N)

Upvotes: 0

asd
asd

Reputation: 138

<?php
foreach($array as $arr)
{
    echo ", ".$arr['kanal'];
}
?>

Upvotes: -1

Matthias Dunkel
Matthias Dunkel

Reputation: 305

$last  = array_slice($array, -1);
$first = join(', ', array_slice($array, 0, -1));
$both  = array_filter(array_merge(array($first), $last));
echo join(' and ', $both);

Code is "stolen" from here: Implode array with ", " and add "and " before last item

Upvotes: -1

Related Questions