rjcode
rjcode

Reputation: 1349

Implode column values with commas

I have below array,

Array
(
 [1] => Array
  (
    [0] => Array
        (
            [location] => X33
            [usernumber] => 1
            [order] => XX
            [part_number] => Hi
        )

    [1] => Array
        (
            [location] => X33
            [usernumber] => 1
            [order] => YY
            [part_number] => 68730
        )

  )

I want desired output string to echo as below,

'Hello ur oder - XX, YY, part number-Hi, 68730'

How to achieve this output? I was thinking to run a foreach loop, but I'm not sure how I could convert this to a string.

Upvotes: 3

Views: 145

Answers (5)

Khalil Kamran
Khalil Kamran

Reputation: 65

I changed the array structure a little bit. The code below would be of help:

$arr = array(
    array(
        'location' => 'X33',
        'usernumber' => '1',
        'order' => 'XX',
        'part_number' => 'Hi',
    ),
    array(
        'location' => 'X33',
        'usernumber' => '1',
        'order' => 'YY',
        'part_number' => '68730',
    )
);

$order = '';
$p_no = '';
foreach ($arr as $ar) {
    $order .= $ar['order'] . ',';
    $p_no .= $ar['part_number'] . ',';
}

$order = rtrim($order, ',');
$p_no = rtrim($p_no, ',');
$final_str = 'Hello ur order - ' . $order . ' part number - ' . $p_no;
echo $final_str;

Upvotes: 0

Maxim Tkach
Maxim Tkach

Reputation: 1697

Your must help this code:

<?php
    $array = [
        [
            [
                'order'       => 1,
                'part_number' => 1
            ],
            [
                'order'       => 2,
                'part_number' => 2
            ]
        ]
    ];
    $orders = [];
    $partnumber = [];
    foreach($array as $v) {
        foreach($v as $item) {
            $orders[] = $item['order'];
            $partnumber[] = $item['part_number'];
        }
    }
    $str = sprintf("Hello ur oder - %s, part number-%s", implode(', ', $orders), implode(', ', $partnumber));
    echo $str;

Result:

Hello ur oder - 1, 2, part number-1, 2

Upvotes: 0

xate
xate

Reputation: 6379

@Frayne Konoks Solution is a nice oneliner:

His Solution with quotes fixed :)

echo 'Hello, ur order - '.implode(", ", array_column($var[1], 'order')).', '.'part number-'.implode(", ", array_column($var[1], 'part_number'))."'";

$var is your array.

Working example: http://sandbox.onlinephpfunctions.com/code/c95545bdf9d9216d6c80cc06542517e596a0360a

Upvotes: 0

chandresh_cool
chandresh_cool

Reputation: 11830

Run a foreachloop and concat

$orderNumber = '';
$partnumber = '';
foreach ($yourArray as $array) {
    if ($orderNumber !="") {
        $orderNumber.=",";
    }
    $orderNumber.= $array['order'];
    if ($partNumber !="") {
        $partNumber.=",";
    }
    $partNumber.= $array['part_number'];
}
echo "Hello ur order - ".$orderNumber." part number-". $partNumber;

Upvotes: 4

Faisal Ashfaq
Faisal Ashfaq

Reputation: 2678

It's as simple as:

foreach($array[1] as $item){
   echo "Hello ur order - " . $item["order"] . ", " $item["location"] . ", part number-" . $item["order"] . ", " . $item["part_number"];
}

Upvotes: -1

Related Questions