PHP: How to set array using implode function

36 Views Asked by At

I am working with tracking API and this is my php function

$speedaf_tracking = $response['billCode'];
$speedaf_labels[] = $speedaf_tracking;
$print_labels = implode(',', $speedaf_labels);

$print_parameters =  [
    "waybillNoList" => [$print_labels],
    "labelType" => 6
];
print_r ($print_parameters);

It give the print_r response:

Array ( [waybillNoList] => Array ( [0] => 'PK020001059806','PK020001059911' ) [labelType] => 6 )

But I want print_r should display like:

Array ( [waybillNoList] => Array ( [0] => PK020001059806 [1] => PK020001059911 ) [labelType] => 6 )

Please help

1

There are 1 best solutions below

0
Luuk On

I would use explode, and not implode:

$speedaf_tracking = 'PK020001059806,PK020001059911'; //$response['billCode'];
$speedaf_labels[] = $speedaf_tracking;
//$print_labels = implode(',', $speedaf_labels);
$print_labels = explode(',', $speedaf_tracking);

$print_parameters =  [
    "waybillNoList" => $print_labels,
    "labelType" => 6
];
print_r ($print_parameters);

output:

Array
(
    [waybillNoList] => Array
        (
            [0] => PK020001059806
            [1] => PK020001059911
        )

    [labelType] => 6
)