Thursday, July 22, 2010

Lets put our knowledge to use!

< ?php
$massive1=array(array("1a","2a","3a","4a","5a","6a"), array("1b","2b","3b","4b","5b"));
$massive2=array(array("1c","2c","3c","4c","5c"), array("1d","2d","3d","4d","5d"),
array("1e","2e","3e","4e","5e"));

$massivelib=array("massive1", "massive2");
$v=0;
$arv=0;
$massiveliblength=count($massivelib);
$massivelength=count(${$massivelib[0]});
$currentmassive=${$massivelib[0]};
$arraylength=count($currentmassive[0]);
foreach( $currentmassive[$v] as $globalmassive[$arv] )
{
$arv=$arv+1;
if($arv<$arraylength)
{
$v=$v+1;
}
if($v<$massivelength)
{
if($i<$massiveliblength)
{
$i=$i+1;
}
$currentmassive=${$massivelib[$i]};
}
}
print_r($globalmassive);
?>
well let's see....
The aim of this code is to make 2 multidimensional arrays become one array.
So what do you need to know to understand this code..... what means ${$variable}.. This is called variables variable.
For example:
$car="Porche";
$Porche= "red";
here ${$car} has the value of red....
$massiveliblength=count($massivelib);
$massivelength=count(${$massivelib[0]});
$currentmassive=${$massivelib[0]};
$arraylength=count($currentmassive[0]);
$massiveliblength counts how many of those $massives there are....here we have 2 $massives.
$massivelength counts how many arrays there are in the first $massive.
$currentmassive acts as a pointer and right now it is set on the $massive1.
$arraylength counts how many elements there are in the first array of massive1
foreach( $currentmassive[$v] as $globalmassive[$arv] )
{
$arv=$arv+1;
if($arv<$arraylength)
{
$v=$v+1;
}
if($v<$massivelength)
{
if($i<$massiveliblength)
{
$i=$i+1;
}
$currentmassive=${$massivelib[$i]};
}
}
As we see here currentmassive(the massive that is so called pointer $currentmassive on) number $v array is giving its value to the $globalmassive number $arv element.Well its confusing a bit but basically it goes like that...at first $v=0 then $currentmassive[0] is same as $massive1, and you remember how foreach works, so $globalmassive is being created....first is $globalmassive[0]= first element of $massive1 first array. Then after that code within { } is being executed.$arv becomes 1 and now $globalmassive[1]= second element of $massive1 first array and so on...

No comments:

Post a Comment