PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

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

 August 29, 2022     csv, php, while-loop     No comments   

Issue

$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


Solution

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);


Answered By - MyLibary
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing