Answers or help w/ Challenges?

11 replies [Last post]
tim3
Offline
Last seen: 5 years 40 weeks ago
Joined: 2012-06-01

Are there any answers, sample answers, or tips on solving "challenges"?

I'm working on the php "Array Function Challenges" where the challenge is:

"Take our multi-dimensional array example and use array_pop() to get the youngest person, and then print their favorite band to the browser."

I know that youngest person is last, so I could simply use the array_pop() function on the array as it is, but I assumed that the challenge was to sort the array in reverse order, to be certain that the last person was the youngest - then apply the array_pop() function. However, I don't think that we've been shown how to do a sort on a multi-dimensional array - and my best guesses have not worked.

Now, I'm thinking I should just skip the sort and assume that youngest person in last.

Drave Robber
Offline
Last seen: 9 years 6 weeks ago
Joined: 2012-08-07
User comments to uasort() on

User comments to uasort() on php.net contain about a dozen different ideas how to go about sorting a multidimensional array by a given key.

There's is no single best way - sometimes you're more concerned about performance, sometimes about readability, etc., etc.

tim3
Offline
Last seen: 5 years 40 weeks ago
Joined: 2012-06-01
Thanks Drave

Thanks for the resource. I had found that on my own, the problem is that I'm very new to php and am unable to apply the info on the page you provided to the "challenge" that Chris posed in the video series, with the skills I currently have.

Upon reflection, I don't think he expected us to be able to sort a multi-dimensional array with the skills demonstrated so far. I think that I was supposed to focus on the array_pop()function. So, maybe I'll come back to this problem.

FYI - for the purpose of the video series, this is the array that I'm supposed to be working with in this exercise. How would I sort by 'birthyear' or 'fav_band'?

$my_array = array(
'George' => array(
'birthyear' => 1972,
'fav_band' => 'The Cure',
'shoe_size' => 10,
),
'Sally' => array(
'birthyear' => 1975,
'fav_band' => 'Coldplay',
'shoe_size' => 8,
),
'Deepak' => array(
'birthyear' => 1969,
'fav_band' => 'Beach Boys',
'shoe_size' => 10,
),
);

// Adding an item to a multidimensional array.
$my_array['Lucy'] = array(
'birthyear' => 1984,
'fav_band' => 'The Beatles',
'shoe_size' => 9,
);

Drave Robber
Offline
Last seen: 9 years 6 weeks ago
Joined: 2012-08-07
-

It seems I'd need to watch the video in question myself to be able to comment on it. ;)

tim3
Offline
Last seen: 5 years 40 weeks ago
Joined: 2012-06-01
Challenges

Understood. It seems that the answer to this question:

Are there any answers, sample answers, or tips on solving "challenges"?" is:

"no."

Tim

me
Offline
Last seen: 9 years 31 weeks ago
Joined: 2012-07-19
Sorry about that, Tim, you're

Sorry about that, Tim, you're right. The challenges are a bit open-ended. They're typically something that you should either be ready to tackle, or you should have the resources to figure out what you need to tackle it. They're kind of a carrot to get you interested in thinking about the problem or the parts that go into it. If you're still following along with the videos without feeling lost, then definitely feel free to move on from the challenge, and the answer may come up somewhere along the way.

Cheers!
Chris

tim3
Offline
Last seen: 5 years 40 weeks ago
Joined: 2012-06-01
Thanks

Chris:

Thanks. That's all reasonable. I did simply move on and am assuming that I'll eventually find the info I need to help me solve that problem.

Keep up the good work.

Tim

P.S. - Having sample answers for the challenges available is not necessary, but might be helpful to some folks like myself.

geijutsuka
Offline
Last seen: 7 years 8 weeks ago
Joined: 2012-07-13
A Little Help

I know this thread was last commented on about a year ago, but...

tim3 is right that sample answers or hints (maybe even just an extra challenge file in the resource pack with possible solutions) would help immensely. I got completely hung up trying to figure out how to sort a multidimensional array, and got my eyes watered. After reading this thread I have no doubt now that the simple, non-scalable method was probably what Chris had in mind when he presented the challenge, but if I hadn't found this page I would have continued to feel pretty helpless and stupid...

Yes--point taken--it did make me try out different things and dig a little deeper, but if I wasn't a tenacious individual, I may have just given up in frustration.

Mentioned videos:
* 1:19 - Array function challenges

mmarques
Offline
Last seen: 7 years 27 weeks ago
Joined: 2014-06-17
I agree

Aswers on how to solve challenges will be very usefull, I lost too much time on this and now discovery that I can keep going..

matthewjroybal
Offline
Last seen: 6 years 10 weeks ago
Joined: 2014-03-30
Challenge Answers

Chris can we post/share our challenge answers to help other students in the forums? I not sure if that's defeating your purpose or helping other students who are asking?

Chris Shattuck
Offline
Last seen: 9 weeks 6 days ago
Joined: 2009-08-16
Hi Matthew! Definitely feel

Hi Matthew!

Definitely feel free to post answers to challenges on here, no problem, thank you for asking, though! It's just been a matter of finding the time to get the answers together that's been a problem on my end. ;)

Thank you!
Chris

matthewjroybal
Offline
Last seen: 6 years 10 weeks ago
Joined: 2014-03-30
Array Challenge Answers!

<?php

// *************************************************
// Figure out how to sort an array in reverse order
// *************************************************

$my_array = array(
'George' => array(
'birthyear' => 1972,
'fav_band' => 'The Cure',
'shoe_size' => 10,
),
'Sally' => array(
'birthyear' => 1975,
'fav_band' => 'Coldplay',
'shoe_size' => 8,
),
'Deepak' => array(
'birthyear' => 1969,
'fav_band' => 'Beach Boys',
'shoe_size' => 10,
),
);

arsort($my_array); // arsort first sorts them aphabetically and then reverses the order
var_dump($my_array); // prints the array in the new order

// ***************************************************************************
// Find out what array functions we tried that you can use on objects as well
// ***************************************************************************
print '';

$my_object = new stdClass;

$my_object->George = 1972;
$my_object->Sally = 1975;
$my_object->Deepak = 1869;

asort($my_object); // Doesn't work
var_dump($my_object);

ksort($my_object); // Doesn't work
var_dump($my_object);

$random_item = array_rand($my_object); // Doesn't work
var_dump($random_item);

array_pop($my_object); // Doesn't work
var_dump($my_object);

$count = count($my_object); // Works. Unless I'm doing something wrong?
var_dump($my_object);

// ********************************************************************************
// Take our multi-dimensional array and use array_pop() to get the youngest person
// and print their favorite band to the browser.
// ********************************************************************************
print '' . '';

$my_array2 = array(
'George' => array(
'birthyear' => 1972,
'fav_band' => 'The Cure',
'shoe_size' => 10,
),
'Sally' => array(
'birthyear' => 1975,
'fav_band' => 'Coldplay',
'shoe_size' => 8,
),
'Deepak' => array(
'birthyear' => 1969,
'fav_band' => 'Beach Boys',
'shoe_size' => 10,
),
);

asort($my_array2); // First sort the keys aphabetically. "Sally" becomes the last key.
$fav_band = array_pop($my_array2); // Select the last key "Sally" and create a new variable.
var_dump($fav_band['fav_band']); // Print the new variable's "fav_band" value.

// ************************************************************************************
// Print a random name to the browser in the form of "Hello [randomname], how are you?"
// ************************************************************************************
print '' . '';

$name_array = array('Matthew', 'Chris', 'Tim'); // Created a simple array
$random_item = array_rand($name_array); // Randomized the array's order. Give it a new variable name.
$get_name = $name_array[$random_item]; // Change that new variable from the ordered number to the name.

print "Hello " . $get_name . ", how are you?"; // Print the required string plus the random name.

?>

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.