I need to find out the oldest person | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I need to find out the oldest person

/*I want to find out the oldest person in a multidimensional array, I thought of using a foreach loop which is special for iterating the arrays, in addition to that, the foreach loop must be inside a function so that it is then called I do not know which is the most appropriate way to build the matrix, so far I have found three ways but I still cannot find the largest number first form of array and the one I have the most option since it is associative*/ <?php $family = array ( array('name'=>"Guillermo ",'surname'=>"Acosta Pacheco ",'age'=>79), array('name'=>"Ruth ",'surname'=>"Bejarano Guevara ",'age'=>57), array('name'=>"Nancy ",'surname'=>"Benavides",'age'=>40), array('name'=>"Santiago ",'surname'=>"Acosta Patiño",'age'=>20), array('name'=>"Luciana ",'surname'=>"Acosta Patiño",'age'=>11), array('name'=>"Antonella ",'surname'=>"Acosta Patiño",'age'=>4), array('name'=>"María José ",'surname'=>"Amador Acosta",'age'=>10), array('name'=>"Valentina ",'surname'=>"Acosta Calderon",'age'=>8), array('name'=>"Isabella ",'surname'=>"Acosta Chavez",'age'=>5), array('name'=>"Salomé ",'surname'=>"Solorzano Acosta",'age'=>2), ); ?> /*in this second form of array it would be the basic of predefined ones, but it is not practical for that exercise*/ <?php $family = array ( array("Guillermo ","Acosta Pacheco ",79), array("Ruth ","Bejarano Guevara ",57), array("Nancy ","Benavides",40), array("Santiago ","Acosta Patiño",20), array("Luciana ","Acosta Patiño",11), array("Antonella ","Acosta Patiño",4), array("María José ","Amador Acosta",10), array("Valentina ","Acosta Calderón",8), array("Isabella ","Acosta Chavez", 5), array("Salomé ","Solorzano Acosta",2) ); ?> /*and there is the last form of array which is to declare arrays inside another array as if they were a multidimensional array made up of predetermined one-dimensional arra

1st May 2020, 11:53 AM
Jazmine Acosta
Jazmine Acosta - avatar
1 Answer
0
You can struggle this in two step. First you work out the oldest age and second you work out the family members who have this age. You can use array comprehension to make an array (ages) with the ages of your family's members then do a loop on that array to take out the oldest age. Then I think about two ways but I prefer the second because it give the possibility to have many people with the oldest age. So you can create an index array then do a loop on your family's array and check for each member if he have the age you just find out. if yes, add his index position to index array you just create before. to make this reusable, just do it in a function which take your family's array as parameter
3rd May 2020, 8:20 AM
White Hat
White Hat - avatar