skip to Main Content

Please, somebody help me with the" If line" i’m making an exercise the rests of my code work, but the last line with If statment doesn’t How can do this?

`<?php

$employees = array (
array("Name" => " Harry ", "Age" => 25 , "Department" => " Management"),
array("Name" => " Jack ",   "Age" =>   31,  "Department" => " Developer"),
array("Name" => " Harry ",  "Age" =>  35,  "Department" => " Developer")`

);

foreach ($employees as $element){

    echo " Empolyers name :" ,$element ['Name'] ,'<br/>' ;
    echo " Age :" ,$element['Age'] ,'<br/>' ;
    echo " Function :" ,$element ["Department"];
    echo "<br>";
} 
$element =0;  

 while ($element <count($employees)){
    echo $employees [$element ]["Name"];
     echo "<br>";
 $element++ ;
 
 echo "<br>";

 if ([$element ]["Name"] == "Jack" ) {
    echo "He is the senior Developer", $element["Name"] ;

} }

I tried to set condition based on the array name, or only $key but i can’t get the function to react`

2

Answers


  1. if ([$element ]["Name"] == "Jack" )

    there is no array just 2 indexes

    Login or Signup to reply.
  2. First of all, you need to have to add $employees in the if statement.

    if ($employees[$element ]["Name"] == "Jack" ) {
        echo "He is the senior Developer", $element["Name"] ;
    }
    

    You change the value of $element, so you get the value of the next array values.

    Second, you can use array_filter.

    $filter = 'Jack';
    $newArray = array_filter($employees, function($e) use ($filter){
                                                    //    ^ import criteria
        return $e['Name'] === $filter;
    });
    echo "He is the senior Developer" . $newArray["Name"] ;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search