As in the code, they swap "firstname" ("Martin") and the name of the array ("Krönström"), or rather, assigning the value $firstname = $lastnames[$key]['firstname']; in the first foreach loop $firstname = $lastnames[$key]['firstname']; , use it in $newlastnames[$firstname]["firstname"] = $key; ?

Please explain what is assigned to funcion a ?

 function a ($lastnames){ $newlastnames; foreach($lastnames as $key => $value){ $firstname = $lastnames[$key]['firstname']; $newlastnames[$firstname]["firstname"] = $key; $newlastnames[$firstname]["age"] = $lastnames[$key]['age']; $newlastnames[$firstname]["sex"] = $lastnames[$key]['sex']; echo $lastnames[$key]['firstname'].'<br>'; } return $newlastnames; } 

Whole code here

 $lastnames = array( "Krönström" => array("firstname" => "Martin", "age" => 20, "sex" => "male"), "Kulper" => array("firstname" => "Laura-Liis", "age" => 17, "sex" => "female"), "Ööbik" => array("firstname" => "Tõnis", "age" => 21, "sex" => "male"), "Mitri" => array("firstname" => "Martten", "age" => 35, "sex" => "male"), "Lõsenko" => array("firstname" => "Evelina", "age" => 16, "sex" => "female"), "Reinaus" => array("firstname" => "Richard", "age" => 21, "sex" => "male")); $newlastnames = a($lastnames); saveFile($newlastnames); function saveFile($lastnames){ file_put_contents("q.json",json_encode($lastnames)); } function a ($lastnames){ $newlastnames; foreach($lastnames as $key => $value){ $firstname = $lastnames[$key]['firstname']; $newlastnames[$firstname]["firstname"] = $key; $newlastnames[$firstname]["age"] = $lastnames[$key]['age']; $newlastnames[$firstname]["sex"] = $lastnames[$key]['sex']; echo $lastnames[$key]['firstname'].'<br>'; } return $newlastnames; } function sortA($lastnames,$sortValue){ foreach($lastnames as $key => $value){ if($sortValue == "firstname"){ $throwArray[$key] = $key; }else{ $throwArray[$key] = $value[$sortValue]; } } array_multisort($throwArray, SORT_DEC, $lastnames); return $lastnames; } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Explain what exactly is incomprehensible?

  1. In the function we pass an array, where the key is the last name, and the value is an array with user data

  2. Create a new array $ newlastnames

  3. We begin to loop through the array passed to the function.

  4. $firstname we assign a username.

  5. Then we start stuffing the data into our new array $newlastnames , using the name $firstname as the key, and to its value we throw the data from the fields firstname , age , sex .

  • $ firstname = $ lastnames [$ key] ['firstname']; I understand that the value of "Martin" from the array "Krönström" and $ newlastnames [$ firstname] ["firstname"] = $ key; $ newlastnames is written under the key "Martin" and the value of $ key "Krönström" is written in the 'firstname' - NoProgress
  • that's right - DaVASrK 8:40