tags:

views:

172

answers:

2

In php is there a function to increment the

values of subsequent values twice(*2) in an array

column based on an initial value?

$beta = array(
array('5', '1''1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'),
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2')

);

/*Example: '5' will be '10' (5*2 =10 and output 10 to web)
         '2' will be '4'  (2*2 = 4 and output 4 to web)
The next '2' will be '16' (4*2 = 8 and output 8 to web)
The next '2' will be '32' (8*2 = 16 and output 16 to web)
And so forth? */

Furthermore is there an easier way to construct this array, cause I firmly believe there is, but not something too complicated in terms of construct such that a noob will not understand it, again thanks.

[Disclaimer: I have spent 3 days trying to understand arrays, I now understand them; however, I am still new and am currently having some issues when trying to manipulate the values in my array and output them to the web.And I am still pretty sure I have a lot to read and learn, so please no flamers, I just need some help, found this problem in this C++ book:

[http://books.google.com/books?id=4Fn%5FP7tdOZgC&pg=PT424&lpg=PT424&dq=subsequent+++column+is+twice+the+value&source=bl&ots=gSvQ%5FLhxoI&sig=dG%5FIlf1iLO86lqX936cT1PpkPc8&hl=en&ei=OEEBS%5FeODYyotgOFtJD3CQ&sa=X&oi=book%5Fresult&ct=result&resnum=1&ved=0CAgQ6AEwAA#v=onepage&q=subsequent%20%20%20column%20is%20twice%20the%20value&f=false%5D%5B1%5D

+3  A: 

You can try array_map:

<?php
function increase($n) {
     return is_array($n) ? array_map('increase', $n) : $n * 2;
}

$new_beta = array_map("increase", $beta);

As for constructing the array, there are other methods to do so but I believe this is the most performent and clean.

MitMaro
Will read about array_map and I will try you code snippet, first time hearing about array_map, thanks for the info.
Newb
you is suppose to be "your," sorry damn QWERTY.
Newb
+1  A: 

Here is an answer for each question in that section of the book, enjoy!

<?php

// Declare an array alpha of 10 rows and 20 columns of type int
// Initialise the array alpha to 0
$alpha = array(array());
for($i = 0; $i < 10; $i++)
{
    for($j = 0; $j < 20; $j++)
    {
        $alpha[$i][$j] = 0;
    }
}

// Store 1 in the first row and 2 in the remaining rows
for($i = 0; $i < 10; $i++)
{
    for($j = 0; $j < 20; $j++)
    {
        if($i == 0)
        {
            $alpha[$i][$j] = 1;
        }
        else
        {
            $alpha[$i][$j] = 2;
        }
    }
}

// Store 5 in the first column, and make sure that the value in
// each subsequent column is twice the value in the previous column
// (Beware this doesn't build off the initial value of 5 in the first
// column but the previously set values above)
for($i = 0; $i < 10; $i++)
{
    for($j = 0; $j < 20; $j++)
    {
        if($j == 0)
        {
            $alpha[$i][$j] = 5;
        }
        else
        {
            if($j - 1 >= 1)
            {
                $alpha[$i][$j] = $alpha[$i][$j-1] * 2;
            }
        }
    }
}

// Print the array alpha one row per line
print "Printing the array alpha one row per line:<br/>";
for($i = 0; $i < 10; $i++)
{
    for($j = 0; $j < 20; $j++)
    {
        print "[". $alpha[$i][$j] ."] ";
    }

    print "<br/>";
}

print "<br/>";

// Print the array alpha one column per line
print "Printing the array alpha one column per line:<br/>";
for($j = 0; $j < 20; $j++)
{
    for($i = 0; $i < 10; $i++)
    {
        print "[". $alpha[$i][$j] ."] ";
    }

    print "<br/>";
}

?>
Ambrosia
Thank you for the comments, Ambrosia.
Newb