array_fill help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

array_fill help

Post by glennnz »

Hi

I'm using this code:

Code: Select all

 
<?php
    $row = 1;
    $no_purchased = $_POST['no_purchased'];
    while($row <= $no_purchased)
        {
        $serial = "serial".$row;
        $strings = array_fill(1, $no_purchased, $serial);
        $row++;
        }
    foreach ($strings as $testcase)
        {
        if (ctype_digit($testcase))
            {
            $valid = "Yes";
            }
        else 
            {
            $valid = "No";
            }
        }
    if ($valid == "No")
        {
        print_r ($strings);
        }
    else
        {
        print_r ($strings);
        }
?>
 
The array it generates is:

[1] => serial2
[2] => serial2

This is wrong. I want it to generate:

[1] => serial1
[2] => serial2
[3] => serial3
.......

Why doesn't it work??

Help...

Thanks
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: array_fill help

Post by Ziq »

Why are u use array_fill()? Read documentation http://php.net/

Replace 8 line code:

Code: Select all

 
    $strings[] = $serial;
 
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

Re: array_fill help

Post by glennnz »

Cool, it works with this:

Code: Select all

$strings[] = $_POST['serial'.$row];
but the array starts at [0], how can I get it to start at [1]?

Thanks
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: array_fill help

Post by Ziq »

It's really simple task

Code: Select all

 
    $strings[$row] = $serial;
 
Learn PHP! :)
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

Re: array_fill help

Post by glennnz »

Great, thanks heaps!!

Now I need to get this part working:

Code: Select all

    foreach ($strings as $testcase)
        {
        if (ctype_alnum($testcase))
            {
            $valid = "Yes";
            }
        else 
            {
            $valid = "No";
            }
        }
 
I want it to loop through, and as soon as $valid = "No", exit the loop and keep $valid = "No"

At the moment it loops through the entire array, and $valid ends up as being for the last entry in the array.

G
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: array_fill help

Post by Ziq »

If I have correctly understood

Code: Select all

   
foreach ($strings as $testcase)
        {
        if (ctype_alnum($testcase))
            {
            $valid = "Yes";
            }
        else 
            {
            $valid = "No";
            break;
            }
        }
 
or

Code: Select all

 
$valid = "Yes";  
foreach ($strings as $testcase)
        {
        if (!ctype_alnum($testcase))
            {
            $valid = "No";
            break;
            }
        }
 
Look at continue; and break; in PHP
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

Re: array_fill help

Post by glennnz »

Brilliant!!

Thanks heaps! :D

Glenn
Post Reply