2

I'm a beginner in PHP, so basically I have an array with no key specified and only values which are arrays. I want to get the name of people who are going to the springbreak (== true). I've tried this but it's not working ...

$liste = [
  ['name' => 'Elsa', 'job' => 'Student', 'springbreak' => 'true' ],
  ['name' => 'Chris', 'job' => 'Teacher', 'springbreak' => 'true' ],
  ['name' => 'Zob', 'job' => 'Student', 'springbreak' => 'false' ],
  ['name' => 'Ken', 'job' => 'Teacher', 'springbreak' => 'true' ],
  ['name' => 'Ryan', 'job' => 'Student', 'springbreak' => 'false' ],
  ['name' => 'Tenta', 'job' => 'Teacher', 'springbreak' => 'true' ]

];

  foreach($liste as $line){
        foreach($line as $value){
          if ($value['springbreak'] == true)
               echo $value['name'];
        }
  }

the output should be : Elsa,Chris,Ken,Tenta

0

4 Answers 4

2

You can make use of array_filter, array_map, and join:

<?php
$liste = [
  ['name' => 'Elsa', 'job' => 'Student', 'springbreak' => 'true'],
  ['name' => 'Chris', 'job' => 'Teacher', 'springbreak' => 'true'],
  ['name' => 'Zob', 'job' => 'Student', 'springbreak' => 'false'],
  ['name' => 'Ken', 'job' => 'Teacher', 'springbreak' => 'true'],
  ['name' => 'Ryan', 'job' => 'Student', 'springbreak' => 'false'],
  ['name' => 'Tenta', 'job' => 'Teacher', 'springbreak' => 'true']
];

// Filter to only keep values with springbreak to true
$springbreak_only = array_filter($liste, function($entry) { return $entry['springbreak'] === 'true'; });

// Grab the names (and ditch the rest)
$springbreak_names = array_map(function($entry) { return $entry['name']; }, $springbreak_only);

// Output the comma-separated names
echo join(', ', $springbreak_names);

Online snippet

Alternative with array_reduce (will only loop once):

$springbreak_names = array_reduce($liste, function($springbreak_names, $entry) {
  if ($entry['springbreak'] === 'true')
    $springbreak_names[] = $entry['name'];
  return $springbreak_names;
}, []);

echo join(', ', $springbreak_names);

See @don't angry me's answer for another simple solution, less functional but also more straightforward.

Note: you should probably use the boolean value true instead of 'true' (string) when you can.

Sign up to request clarification or add additional context in comments.

1 Comment

array_reduce() way seems promising to me :)
0

Why your existing code doesn't work?

Because you used unnecessary two foreach() whereas a single foreach() will do the trick. Another reason is you're comparing like this $line['springbreak'] == true which should be $line['springbreak'] == 'true'

So, with your existing code. Let's try,

foreach($liste as $line){
    if ($line['springbreak'] == 'true')
        echo $line['name'];
}

Why not simple foreach() loop and implode() to join the name where springbreak is true?

<?php
$liste = [
    ['name' => 'Elsa', 'job' => 'Student', 'springbreak' => 'true' ],
    ['name' => 'Chris', 'job' => 'Teacher', 'springbreak' => 'true' ],
    ['name' => 'Zob', 'job' => 'Student', 'springbreak' => 'false' ],
    ['name' => 'Ken', 'job' => 'Teacher', 'springbreak' => 'true' ],
    ['name' => 'Ryan', 'job' => 'Student', 'springbreak' => 'false' ],
    ['name' => 'Tenta', 'job' => 'Teacher', 'springbreak' => 'true' ]
];
$names = [];
foreach($liste as $list){
    if($list['springbreak']=='true'){
        $names[] = $list['name'];
    }
}
echo implode(',',$names);
?>

Output:

Elsa,Chris,Ken,Tenta

DEMO: https://3v4l.org/B7Bo6

Comments

0

You forgot if statement.

foreach($liste as $line){
    foreach($line as $value){
        if($value['springbreak'] === 'true'){
            echo $value['name'];
        }
    }
}

Comments

0

I came up with the following solution:

<?php

    $liste = [
        ['name' => 'Elsa', 'job' => 'Student', 'springbreak' => 'true' ],
        ['name' => 'Chris', 'job' => 'Teacher', 'springbreak' => 'true' ],
        ['name' => 'Zob', 'job' => 'Student', 'springbreak' => 'false' ],
        ['name' => 'Ken', 'job' => 'Teacher', 'springbreak' => 'true' ],
        ['name' => 'Ryan', 'job' => 'Student', 'springbreak' => 'false' ],
        ['name' => 'Tenta', 'job' => 'Teacher', 'springbreak' => 'true' ]

    ];


    $going_to_springbreak = array();

    //Check every item
    foreach($liste as $line){
        //Check first if the 'springbreak' is true
        if($line['springbreak'] == 'true'){
            //Then add the name of that item to the new array
            $going_to_springbreak[] = $line['name'];

            //OR you can add the entire item to the new array if you want to use the other values
            //$going_to_springbreak[] = $line;
        }
    }

    var_dump($going_to_springbreak)

?>

Output:

array(4) {
      [0]=>
      string(4) "Elsa"
      [1]=>
      string(5) "Chris"
      [2]=>
      string(3) "Ken"
      [3]=>
      string(5) "Tenta"
    } 

On a final note, I would highly recommed to store the 'true' or 'false' value as a primitive value, so:

'springbreak' => true

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.