Hi all, Im a still very new to PHP and don't know how to attempt the following:
I'm trying to build a dynamic array in a form i'm utilizing. Also I am passing parameters from the url as follows:
colour.php?val=3&co1=Red&co2=Blue&co3=Green
//where val is the iteration number and co1,co2,co3 is the three colours to input // I need to change in such a way that val can be any number and where i'll accordingly pass the colours to match that number
Currently my static code is:

$in_colour = new InputSelect('in_colour');
$in_colour->rows = array(array($co1,$co1),
array($co2,$co2),
array($co3,$co3));

This is working fine and creates a input select with the three colours I have passed. How do I change the code to build a dynamic array where I can change the val to lets say 4 or 5 or whatever: For example
colour.php?val=4&co1=Red&co2=Blue&co3=Green&co4=Yellow

Please can someone help me?

// get number of colors
$val = $_GET['val'];

// $val starts from 1 
for($i = 1; $i <= $val; $i++)
{
        $color = $_GET["col".$val];
        echo $color;
}

After a second thought, i'm not sure if i understood you question. Because there's no dynamic arrays involved. If you really want dynamic array.

$variable_name = "col" . $val;
echo ${$variable_name} // gives the last color

The thing is that I need to build the array as follow:
$array1 = array($co1,$co1);
$array2 = array($co2,$co2);
$array3 = array($co3,$co3);
$array.... = array($co....,$co....);
//where the ..... represents an iteration number so if the iteration number was 5 then it need to build 5 arrays
// so i've got a variable $array and also a variable $co - these two's -value correpsond

finally i need to put the arrays together:
$final_array = array($array1, $array2, $array3, $array .....);

The thing is that I need to build the array as follow:
$array1 = array($co1,$co1);
$array2 = array($co2,$co2);
$array3 = array($co3,$co3);
$array.... = array($co....,$co....);
//where the ..... represents an iteration number so if the iteration number was 5 then it need to build 5 arrays
// so i've got a variable $array and also a variable $co - these two's -value correpsond

finally i need to put the arrays together:
$final_array = array($array1, $array2, $array3, $array .....);

// this code will (should) do what you are asking for, but it's not the best method imo. 
$final_array = array();
for($i = $val; $i >= 1; $i--)
{
    $array = "array". $val;
    $co = "co".$val;
    ${$array} = array(${$co}, ${$co});
    array_push($final_array, ${$array});
}

alter the URL such that it goes
colour.php?val=3&co[0]=Red&co[1]=Blue&co[2]=Green
or
colour.php?co[]=Red&co[]=Blue&co[]=Green

// this is better.
for($i= count($co); $i >= 0; $i++)
{
        $final_array[$i] = array($co[$i], $co[$i]);
}

codes not tested but the logic is there. I think you need to read up on "php arrays", the "php array functions" and "passing arrays using HTML GET". Google for these.

Thank you so much! It's working.
Also thank you on the advise of the co[0], co[1], etc.
Very clever - Now I can access these variables with the for statement.
Thanx!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.