Skip to content
Advertisement

How to create comma separated list from a while-loop?

$personas = [
'Hermann' => [
  'status' => '0',
  'gender' => 'maskulin'
],
'Lida' => [
  'status' => '1',
  'gender' => 'feminin'
],
'Susi' => [
  'status' => '0',
  'gender' => 'feminin'
],
'Mara' => [
  'status' => '0',
  'gender' => 'feminin'
]
];

Personas with status 0

  echo 'Personas with status 0: ';
  while ($status = current($personas)) {
    if ($status['status'] == '0') {
    $status_list = key($personas);
    echo $status_list;

  }
  next($personas);
}

Result is: Personas with status 0: HermannSusiMara

is expected: Personas with status 0: Hermann, Susi, Mara

Advertisement

Answer

A bit different solution for you.

$statusZeroPersonae = [];

foreach($personas as $personaName => $persona) {
    if ($persona['status'] === '0') {
        $statusZeroPersonae[] = $personaName;
    }
}

echo 'Personae with status 0: ' . implode(", ", $statusZeroPersonae);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement