Finding Zodiac with Three-dimensional Array | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Finding Zodiac with Three-dimensional Array

Hey, this is my first post on this site and IT'S A QUESTION! Right now, I'm taking a PHP server scripting class at my university and am working on a Chinese Zodiac website project. It's pretty easy and fun, and one of the pages has to take in user input for their birth date and then use a PHP function to determine their zodiac, which I made. I just wanted to go an extra mile, so I created a three-dimensional array where each animal has their own sub-dimensional arrays for a start and end date. What I'm trying to do is take the user's input and loop through the arrays to find where their birth date is between the same indices of the start and end date arrays, and then return the parent animal array name. I can't figure out how to do this because I have never actually worked with multidimensional array before, so I'm still wrapping my head around this. Here's a code snippet and if anyone could help me, I'd appreciate it. So if the user enter the date of October 25th 1912, it would go through all animals and then match the date, and return 'Rat' as the sign; at least that's what I want it to do. <?php $birthYear='1912-10-25'; $findSign=''; echo "Your zodiac sign is ".$findSign."."; function findZodiac($birthYear) { $zSigns=array ( $rat=array( $startDate=array('1912-02-18', '1924-02-05', '1936-01-24', '1948-02-10', '1960-01-28', '1972-02-15', '1984-02-02', '1996-02-19', '2008-02-06', '2020-01-25', '2032-02-11', '2044-01-30'), $endDate=array('1913-02-05', '1925-01-23', '1936-02-10', '1949-01-28', '1961-02-14', '1973-02-02', '1985-02-19', '1997-02-06', '2009-01-25', '2021-02-11', '2033-01-30', '2045-02-16') ), $ox=array( $startDate=array(...), $endDate=array(...) ) ); if($zYear>=t_foreach($zSigns[0][0]) && $zYear<=t_foreach($zSings[0][1])) {$findSign=$zSigns[0];} return $findSign;

25th Apr 2017, 5:37 PM
TraƩ Delaney
3 Respostas
+ 2
your array looks weird... why don't you try the following: $zodiac = array( array('rat', array('1912-02-18', '1913-02-05'), array('1924-02-05', '1925-01-23'), ... ), array('ox', ... ); then you can use foreach($zodiac as $z) $z[x][0] is the name of the zodiac $z[x][a][0] is the beginning and $z[x][a][1] the end if there's a match, break the loop and output the name.
25th Apr 2017, 8:54 PM
Mario L.
Mario L. - avatar
+ 1
A professor at my school was able to figure it, which is AWESOME! It foes exactly what I wanted it to do, except for the fact that I keep getting these weird uninitialized string offsets. I have no idea with that one and neither does he. Here's a snippet of the loop and thanks for the help, it put me on the right but my professor and I think the foreach was screwing it up for some reason. for($i=0; $i<=11; $i++){ for($k=0; $k<=11; $k++){ $startDate=$zodiacSigns[$i][0][$k]; $endDate=$zodiacSigns[$i][1][$k]; if($date>=$startDate && $date<=$endDate{ $zodiac=$zodiacSign[$i][0]; } } }
2nd May 2017, 2:50 PM
TraƩ Delaney
0
I tried what you suggested, in multiple different ways, and couldn't seem to get it to function. It keeps printing a blank where the zodiac name should be, even when I would intentionally use a false date just to test if an else works. Maybe I'll just take a break for now since I've been at it for these past few days; let my brain rest.
27th Apr 2017, 3:34 PM
TraƩ Delaney