$Three3 Posted November 28, 2009 Share Posted November 28, 2009 Hi I am new to PHP (a week and a half now) and I am just beginning to read about arrays. I understand simple arrays, associative arrays, and multidimensional arrays also. The book I am reading explains the array types in an easy to understand way but the example programs they show at the end of the chapter are pretty confusing. They seem to get advanced when combining arrays with for and while loops. So my questions is, is there a good source out there to learning and dealing with arrays? It does not matter to me if it is a book, online, or in any other form. Also, are arrays very important to know when it comes to PHP? Thanks in advance for your time and answers. Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/ Share on other sites More sharing options...
smerny Posted November 28, 2009 Share Posted November 28, 2009 arrays are important in any language if you are dealing with data that fits well in arrays (which is often the case) check this out first i guess http://php.net/manual/en/control-structures.foreach.php Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-966785 Share on other sites More sharing options...
$Three3 Posted November 28, 2009 Author Share Posted November 28, 2009 arrays are important in any language if you are dealing with data that fits well in arrays (which is often the case) check this out first i guess http://php.net/manual/en/control-structures.foreach.php Hey thanks for the great link. That helped me quite a bit. Here is the code for one of the example programs at the end of the chapter. <?php // if form not yet submitted // display form if (!isset($_POST['submit'])) { ?> <form method="post" action="primes.php"> Enter a list of numbers, separated by commas: <br /> <input type="text" name="num" /> <p> <input type="submit" name="submit" value="Submit" /> </form> <?php // if form submitted // process form input } else { // retrieve number from POST submission // convert to array by splitting on comma $numStr = $_POST['num'] ; $numArr = explode(',', $_POST['num']) ; $primes = array() ; $primeFlag = 0 ; // iterate over array // get absolute values of each number foreach ($numArr as $n) { $n = trim(abs($n)); // test each number for prime-ness: // check the number by dividing it // by all the numbers between 2 and itself // if not perfectly divisible by any, // number is prime for ($i=2; $i<$n; $i++) { $primeFlag = 0 ; if (($n%$i) == 0) { break; } $primeFlag = 1; } // if prime // add to output array if ($primeFlag == 1) { array_push($primes , $n) ; } } // check if any primes were found // if yes, sort and remove duplicates from array // print message if (count($primes) > 0) { $primes = array_unique($primes) ; sort($primes) ; echo 'The following numbers are prime: ' . implode($primes, ' ') ; } else { echo 'No prime numbers found' ; } } ?> The problem I am having with the code is that it will check for prime numbers, but will only display the last prime number entered into the text box. I attached a 2 screenshots to the post to help you understand what I am talking about. What is wrong with the code? I copied and pasted word for word out of the book but it still does not work. Thanks. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-966800 Share on other sites More sharing options...
play_ Posted November 28, 2009 Share Posted November 28, 2009 Just tried your code and it worked fine. it displayed "The following numbers are prime: 3 11 29" Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-966832 Share on other sites More sharing options...
$Three3 Posted November 28, 2009 Author Share Posted November 28, 2009 Just tried your code and it worked fine. it displayed "The following numbers are prime: 3 11 29" Hey thanks for trying it out. I got it working now. It was just a minor error on my part. If you do not mind though, I am having little trouble understanding this line of code : // test each number for prime-ness: // check the number by dividing it // by all the numbers between 2 and itself // if not perfectly divisible by any, // number is prime for ($i=2; $i<$n; $i++) { $primeFlag = 0 ; if (($n%$i) == 0) { break; } $primeFlag = 1; } I understand the rest of the code but this is throwing me off. Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967029 Share on other sites More sharing options...
smerny Posted November 28, 2009 Share Posted November 28, 2009 for ($i=2; $i<$n; $i++) { // start $i at 2, increment by 1 until it is greater than $n $primeFlag = 0 ; if (($n%$i) == 0) { // if you can divide $n by $i without any remainder ($n%$i will return the remainder after division) break; } $primeFlag = 1; } Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967060 Share on other sites More sharing options...
phant0m Posted November 28, 2009 Share Posted November 28, 2009 // start $i at 2, increment by 1 until it is greater than $n not greater, it will stop when $i == $n Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967080 Share on other sites More sharing options...
$Three3 Posted November 28, 2009 Author Share Posted November 28, 2009 // start $i at 2, increment by 1 until it is greater than $n not greater, it will stop when $i == $n Hey thanks for clarifying that. I was confused but thanks for clearing that up. Could you explain what this line means? if (($n%$i) == 0) I know the "%" operator divides the 2 numbers and then gives you the remainder but for example, if I was to put "57" in the text box to see if it is a prime number, what is 57 being divided by? Is it being divided by "2" which would would leave a remainder of "1" which would not equal "0" so it should be considered a prime correct? Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967085 Share on other sites More sharing options...
smerny Posted November 29, 2009 Share Posted November 29, 2009 yes, it is divided by 2 first, then 3, then 4 etc Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967265 Share on other sites More sharing options...
$Three3 Posted November 29, 2009 Author Share Posted November 29, 2009 yes, it is divided by 2 first, then 3, then 4 etc Thanks a lot for the help. I understand the script now. I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967271 Share on other sites More sharing options...
jay7981 Posted November 29, 2009 Share Posted November 29, 2009 just to further clearify, in examples, and typical coding "for ($i=2; $i<$n; $i++)" the var $i is usally meant as "increment" although you can set it as just about anything by simply changing the verbage. ($inc=2; $inc<$n; $inc++) for ($bob=2; $bob<$n; $bob++)... ect. Quote Link to comment https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967287 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.