Implode an array without first element
Asked Answered
F

4

11

I have an array like this:

[0]=>array( [cname] => ABC
            [12] => 60.7500
            [13] => 33.7500
            [14] => 47.7500
            [15] => 86.0000
            [16] => 62.2500
            [17] => 59.5000
            [18] => 78.0000
            [19] => 42.7500
            [20] => 36.0000
            [21] => 40.0000
            [22] => 40.0000
            [23] => 24.0000
    )
)

Now, I have to print the cname in one field and in next field I have to print its data using implode function. It works fine. But When I implode it, it also gives the company name as well, which I do not want.

Desired Result:

Name: ABC
Data: 60.7500, 33.7500, 47.7500 ....

How can I skip the first element using implode?

Frantic answered 27/1, 2015 at 5:49 Comment(4)
Why do you have an array like that in the first place? Why not a multidimensional array('name' => 'ABC', data => array(...))?Siddur
Because I have to display data in the highcharts therefore, it is necessary to have the array format like that.Frantic
what is cname? looks like constant.Bibi
implode(', ', array_slice($array, 1)) ?Sanskritic
A
15

Just copy the array, then delete the cname property before calling implode.

$copy = $arr;
unset($copy['cname']);
implode($copy);

This works because in PHP, array assignment copies. (Sort of bizarre, but it works.)

Accoucheur answered 27/1, 2015 at 5:51 Comment(1)
I know this is old but came up to it right know and was wondering which solution is more efficient or clean, yours or what @Sanskritic suggests in the comments above (comments of question): implode(', ', array_slice($array, 1))?Palladic
H
6

Use array_shift followed by implode.

$array = YOUR_ORIGINAL_ARRAY;

$cname = array_shift($array);
$string = implode(',', $array);
Hardandfast answered 27/1, 2015 at 5:56 Comment(0)
W
4

Since not all viewers are reading the comments, here the best answer for me from @darren:

implode(', ', array_slice($array, 1))
Winkelman answered 30/3, 2020 at 7:57 Comment(0)
C
3

Try the following:

$removedElementValue = array_shift($yourArray);
$implodedArray = implode(',', $yourArray);
Corrasion answered 27/1, 2015 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.