Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract all 2nd level values (leaf nodes) from a 2-dimensional array and join with commas?

How can I get the list of values from my array:

[data] => Array
        (
            [5] => Array
                (
                    [0] => 19
                    [1] => 18
                    [2] => 20
                )

            [6] => Array
                (
                    [0] => 28
                )

        )

Expected output result string will be: 19,18,20,28

Thanks!

like image 607
XTRUST.ORG Avatar asked Jun 10 '15 06:06

XTRUST.ORG


People also ask

How do you traverse a 2D array in python?

Traversing in a 2D array in python can be done by using a for loop. We can iterate through the outer array first and then at each element of the outer array, we have another array which is our inner array containing the elements. So for each inner array, we run a loop to traverse its elements.

How do I get to the second column in python?

“get second column dataframe” Code Answer'sdf1 = df. iloc[:, 0:2] # If you want to do it by index. Remember that Python does not slice inclusive of the ending index.

How do you make a two-dimensional list in python?

myList = [0,1,2,3,4,5,6,7,8,9]; for index in len(myList): myList[index] = 0 # Set element at "index" to 0. For a two-dimensional list, in order to reference every element, we must use two nested loops. This gives us a counter variable for every column and every row in the matrix.

What is a 2D array in python?

Two dimensional array is an array within an array. It is an array of arrays. In this type of array the position of an data element is referred by two indices instead of one. So it represents a table with rows an dcolumns of data.


1 Answers

With one line, no loop.

echo implode(',', call_user_func_array('array_merge', $data));

Try it here.

like image 182
Rene Korss Avatar answered Oct 20 '22 16:10

Rene Korss