0

I have two arrays I wan to convert in two strings..

One Array in first string 2nd Array in Second String

Like this array1 = 5,6,7,8

Like this array2 = 15,16,17,18

Should be in loop no matter How many rows there are

and Soo on.. Help me please I am stuck Here from Many days...

Array
(
    [0] => Array
        (
            [0] => 5
            [ride_id] => 5
        )

    [1] => Array
        (
            [0] => 6
            [ride_id] => 6
        )

    [2] => Array
        (
            [0] => 7
            [ride_id] => 7
        )

    [3] => Array
        (
            [0] => 8
            [ride_id] => 8
        )


)
Array
(
    [0] => Array
        (
            [0] => 15
            [ride_id] => 15
        )

    [1] => Array
        (
            [0] => 16
            [ride_id] => 16
        )

    [2] => Array
        (
            [0] => 17
            [ride_id] => 17
        )

    [3] => Array
        (
            [0] => 18
            [ride_id] => 18
        )
)
Strawberry
  • 33,750
  • 13
  • 40
  • 57

2 Answers2

1

Use array_column() to get just the ride_id element from each nested array, and use implode() to combine them into a string.

$string1 = implode(',', array_column($array1, 'ride_id'));
$string2 = implode(',', array_column($array2, 'ride_id'));
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I like the other answer as I'm a big fan of array_column. Another way, if the values are unique, especially if you don't know the column names, is to flatten array_merge, make unique array_unique and then implode:

$result1 = implode(',', array_unique(array_merge(...$array1)));
$result2 = implode(',', array_unique(array_merge(...$array2)));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87