Hey guys,

I'm having difficulties with a problem.
I iterate over an array of data with X rows

And i have the positions of each row incremented, i would like that when i found an exception i shift all the position id's so after each exception the next ID has to be +2 as seen in the second attachment.

After each exception i woul like to have a free unused position so i can use it later.
Any idea of how could i make this ?

Picture one is the data array

368b1e79027f622224713a0d830a0fac
Picture two is the desired result
920c682fad358fd2046f18d935e6a92a

If you post what you have done so far, that would be best.

Well something like this like in a while when comparing currency changes, but I just dont get it ...

echo '<table border="1" cellpadding="5">';

$previous = null;
foreach($garantie->TESTverificaFiecareCodDinCos($comanda) as $camp) {
    echo '<tr><td></td><td>'.$camp['cos'].'</td><td>'.$camp['pozitie'].'</td><td>'.$camp['cod_garantie'].'</td><td>'.$camp['matnr'].'</td></tr>';
    if($previous['cod_garantie'] != '') {
        echo '<tr><td><b>EMPTY</b></td><td></td><td><b style="color: red;">'.($previous['pozitie']+2).'</b></td><td></td><td></td></tr>';
    } else {
        $previous = $camp;
    }       
}

echo '</table>';

de879614b34de7489f01711f8150dbfb

can you tell what exatly output you desire as third figure is confusing me??

Yes, the EMPTY(exception) has to have the Position of previous Position + 1
This means that the non-exeption have to shift their values up by one also.

So if $camp['pozitie'] has a specific value and $camp['cod_garantie'] is empty, you have to alter all the following positions, correct?

You can add a counter and add it to the position key, here's an example:

<?php

$datum[] = array('name' => 'sun', 'pos' => 1, 'magnitude' => '-26.74');
$datum[] = array('name' => 'venus', 'pos' => 2, 'magnitude' => '-5');
$datum[] = array('name' => 'sedna', 'pos' => 3, 'magnitude' => '');
$datum[] = array('name' => 'neptune', 'pos' => 4, 'magnitude' => '8');
$datum[] = array('name' => 'io', 'pos' => 5, 'magnitude' => '');
$datum[] = array('name' => 'moon', 'pos' => 6, 'magnitude' => '-12.74');

echo PHP_EOL;

$i = 0;

foreach($datum as $key => $value)
{
        if(empty($value['magnitude']))
        {
                $i++;
        }

        if($i > 0) $value['pos'] = $value['pos'] + $i;

        echo "\t" . $value['pos'] . ' ' . $value['name'] ."\t" . $value['magnitude'] . PHP_EOL;
}

echo PHP_EOL;

will output:

1 sun       -26.74
2 venus     -5
4 sedna 
5 neptune   8
7 io    
8 moon      -12.74

Note rows 4 and 7, the original values are 3 and 5.

Cereal thanks for the heads up, it's simpler than i tought :)

This is my solving of the problem, with your help.

    <?php

    $i = 0;
    $j = 1;
    foreach($garantie->TESTverificaFiecareCodDinCos($comanda) as $camp) {       
        if(!empty($camp['cod_garantie'])) {
            $i++;
        }

        if($i > 0) {
            $camp['pozitie'] = $camp['pozitie'] + $i;
        }
        if(!empty($camp['cod_garantie'])) {
            echo ($camp['pozitie']-1)."\t\t".$camp['matnr']."\t\t".$camp['mfrpn'].'<br />';
        } else {
            echo $j."\t\t".$camp['pozitie']."\t\t".$camp['matnr']."\t\t".$camp['mfrpn'].'<br />';
        }
        $j++;
    }

    ?>
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.