-2

How can I transform this array:

 Array
    (
        [0] => 6
        [1] => 25
        [2] => 29
        [3] => 27
        [4] => 24
        [5] => 7
    )

into array a comma separated string list:

6,25,29,27,24,7

without altering the order.

My goal and then I get this string array, but I do not know how to do.

aorcsik
  • 15,271
  • 5
  • 39
  • 49

3 Answers3

10

The array is an array already. If you mean you want to turn it into a string, then use the implode function:

$string = implode(",", $array);

The numbers you are seeing first (eg 0 => 6) show the KEY of the element.

You can access individual elements in your code by that key. For example:

echo $array[0];

Would output 6 - the value stored in the element with a key of 0.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
4

Use implode() in php

Try like

$arr =  array
    (
        '0' => 6,
        '1' => 25,
        '2' => 29,
        '3' => 27,
        '4' => 24,
        '5' => 7
    );
$newArr = array();
$newArr[0]  = "'".implode("','",$arr)."'";
print_r($newArr);

Output

Array ( [0] => '6','25','29','27','24','7' )

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
1
implode(',',$array)

Try to print this.

david
  • 3,225
  • 9
  • 30
  • 43