Recovery of certain values in an PHP array
I have a table containing several groups of numbers such
3 4 5 6 7 8 9 15 16 17 18 19 20 52 53 54 55 56 57 58
I get the first and last element of each group of numbers in a new table. In my example, I therefore in my new table 3 9 15 20 52 58
That is what I wrote but it does not work.
PHP Code:
$tab1 = array ();
$i = 1;
$j = 0;
$debut1 = current ($tab); // first element of my first table
$val = $debut1;
$val2 = $val + $i;
foreach ($tab as $value)
{
$val2 = $val + $i;
$val3 = next ($ tab);
if ($val3 = $val2)
{
$i++;
$tab1 [ $j ] = $val;
}
else
{
$j++;
$tab1 [ $j ] =prev ( $tab );
$val = current ($tab);
}
}
Re: Recovery of certain values in an PHP array
These numbers come from where? you need to do this for what? I ask this because there are several ways to do so I prefer to ask more details to make it match what you need
Re: Recovery of certain values in an PHP array
Quote:
Originally Posted by
windows_user
I have a table containing several groups of numbers such
3 4 5 6 7 8 9 15 16 17 18 19 20 52 53 54 55 56 57 58
How groups are defined and identified?
Re: Recovery of certain values in an PHP array
Just try this. I suppose it should definitely work.
PHP Code:
$tab = array ();
$tab [0] ='3 4 5 6 7 8 9 ';
$tab [1] ='15 16 17 18 19 20 ';
$tab [2] ='52 53 54 55 56 57 58 ';
for ($i = 0; $i <count ($tab); $i++)
{
$vals = array ();
$vals = explode ( "", $tab [$i]);
$val1 = $vals [0];
$val2 = $vals [count ($vals) - 1];
// Val1 and val2 contain here the first and last value
}