-4

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

JohnMonro
  • 33
  • 5
  • 3
    [Stack Overflow](https://stackoverflow.com/) is not a free code writing service. You are expected to try to **write the code yourself**. After [doing more research](https://meta.stackoverflow.com/questions/261592) if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a [**Minimal, Complete, and Verifiable example**](https://stackoverflow.com/help/mcve). I suggest reading [How to Ask](http://stackoverflow.com/questions/how-to-ask) a good question. – Tom Udding May 14 '17 at 11:46
  • @TomUdding If you feel smart you could have just provided something that I could have searched and documented myself in order to achieve what I asked. – JohnMonro May 14 '17 at 11:48
  • You should search for something before asking about it on Stack Overflow. This might help you; foreach loop, implode, concatenate. – Tom Udding May 14 '17 at 11:53
  • @TomUdding You see? Now you're being useful . Thank you. – JohnMonro May 14 '17 at 11:57
  • 2
    You are supposed to make at least a little effort for yourself. – RiggsFolly May 14 '17 at 11:59

1 Answers1

0

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.

Community
  • 1
  • 1
Tom Udding
  • 2,264
  • 3
  • 20
  • 30