Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/183188-new-to-arrays/
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-966800
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967029
Share on other sites

// 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.

 

Link to comment
https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967085
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/183188-new-to-arrays/#findComment-967287
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.