Implode() - specific values from multidimensional array in PHP

How should I use implode() function in PHP to get the specific data from multidimensional array ?
0
give a positive ratinggive a negative rating
Hi,

There are multiple methods how to get the values from multidimensional array. Some methods are usable mostly for the specific types of array. You have to check the following examples:


Get values from one array by using the specific identifiers:

$multidimensional_array = array('a' => array('one' => array(10,20,30), 'two' => '40' ), 'b' => array('one' => array(100,200,300), 'two' => '400') );

echo implode(",", $multidimensional_array['a']['one']);


Get values from more arrays by using the identifier in array_column() function:

$multidimensional_array = array('a' => array('one' => array(10,20,30), 'two' => '40' ), 'b' => array('one' => array(100,200,300), 'two' => '400') );

echo implode(",", array_column($multidimensional_array, 'two'));


Get all values from multimensional array, which can be used for example in combination with array_column results or other situation, when input is a multidimensional array:

$multidimensional_array = array('a' => array('one' => array(10,20,30), 'two' => '40' ), 'b' => array('one' => array(100,200,300), 'two' => '400') );

function get_values_in_multidimensional_array($multidimensional_array) {

global $values;

foreach ($multidimensional_array as $array_value) {
if ( is_array($array_value) ) {
get_values_in_multidimensional_array($array_value);
}
else {
$values[] = $array_value;
}
}

return $values;
}

echo "All values in multidimensional array: " . implode(",", get_values_in_multidimensional_array($multidimensional_array));

This solution is based on the function that is calling itself. It will get the array values in multidimensional array, not the array keys.
Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us