So I have next array:
Array ( [1] => Array ( [FIRST_NAME] => John [LAST_NAME] => Bon )
[2] => Array ( [FIRST_NAME] => Ray [LAST_NAME] => Bam )
)
How can I get the next result?:
Names: John Bon, Ray Bam
So I have next array:
Array ( [1] => Array ( [FIRST_NAME] => John [LAST_NAME] => Bon )
[2] => Array ( [FIRST_NAME] => Ray [LAST_NAME] => Bam )
)
How can I get the next result?:
Names: John Bon, Ray Bam
You can use a foreach()
loop. The code below gives you the index of the inner array (0, 1, 2, 3, etc.
) and the inner array itself. Then you can use the keys of the inner arrays to display the the values from that key.
$array = array(
array("FIRST_NAME" => "Jon", "LAST_NAME" => "Doe"),
array("FIRST_NAME" => "Jane", "LAST_NAME" => "Doe"),
array("FIRST_NAME" => "Johnny", "LAST_NAME" => "Doe"),
array("FIRST_NAME" => "Janie", "LAST_NAME" => "Doe"));
$output = "";
foreach ($array as $i => $values) { // get the inner arrays from the outer array
if ($i != 0) { // check if it is not the first array
$output .= ", {$values['FIRST_NAME']} {$values['LAST_NAME']}";
} else {
$output .= "{$values['FIRST_NAME']} {$values['LAST_NAME']}";
}
}
echo $output;
// Jon Doe, Jane Doe, Johnny Doe, Janie Doe
In this code the values (e.g. {$values['FIRST_NAME']}
)from the inner arrays are interpolated in the string, you can check this answer for more information on why you should do it like this.
See https://3v4l.org/K5s82 for the output.