D
D
Dark192015-06-24 16:15:26
PHP
Dark19, 2015-06-24 16:15:26

How to check if an array is finite?

Good afternoon, I need to conditionally check whether I have reached the end of the array $arr['i'] and $arr['j'] (they have different lengths), if I have reached then I need to perform some actions. Can you tell me how to check whether the array has reached the end? It is not possible to check using a loop, because there, according to the conditions, the necessary elements are formed into a new array, and removed from the current one, and the current array is cycled through the loop until it becomes empty.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis, 2015-06-24
@Dark19

A little review... and refactoring.

$iter = ceil((count($data[$arrCenter[$i]]['j']) + count($data[$arrCenter[$i]]['i'])) / 2) + ceil($w / $machin);

// Зачем вам индексированый массив?
for ($a = 0; $a < $iter; $a++) {

    // Где проверка на существование $data[$arrCenter[$i]]['i'][$a] $data[$arrCenter[$i]]['j'][$a] ?
    
    $center = $arrCenter[$i];
    $arrayI = $data[$center]['i'][$a];
    $arrayJ = $data[$center]['j'][$a];
    
    // !$var === ($var == 0 || $var == '')
    if (!$arrayJ['order'] && !$arrayI['order']) {
        
        if (($arrayI['weight'] + $arrayJ['weight']) <= $machin) {

            $weight[$a] = $arrayI['weight'] + $arrayJ['weight'];
            
            array_unshift($path[$center][$c], $arrayI['name']);
            
            // use buffer
            echo $arrayJ['name'] . '<br/>';
            
            // array_push(...) === $var[] = $value
            $path[$center][$c][] = $arrayJ['name'];
            
            unset($data[$center]['i'][$a], $data[$center]['j'][$a]);
        }
    }
    //                  ^
    //                  |
    // +----------------
    // |
    // Вы только что их убили.  +-- Где проверка на существование?
    //                          |
    //                          V

    if ($data[$arrCenter[$i]]['j'][$a]['order'] < $data[$arrCenter[$i]]['i'][$a]['order']) {
        // ...
    }

As for the next element in the array.
Are you confused yourself?
Expand the line and you will see:
if (
            (
                !next($data[$arrCenter[$i]]['j']) && 
                !next($data[$arrCenter[$i]]['i']) && 
                (
                    !empty($data[$arrCenter[$i]]['j']) && 
                    !empty($data[$arrCenter[$i]]['j'])
                )
            )
    ) {
        // ...
    } else {
        // ...
    }

Warning
This function can return either boolean FALSE or a non-boolean value that casts to FALSE. See Boolean type for more information. Use the === operator to test the value returned by this function.
Simplify...
if (
        false === next($data[$arrCenter[$i]]['j']) && 
        false === next($data[$arrCenter[$i]]['i'])
    ) {
        // ...
    } else {
        // ...
    }

Clean up your code.
Use temporary variables.
Since you "kill" by index, check for existence later in the code.

S
Stalker_RED, 2015-06-24
@Stalker_RED

If to check not in a cycle that where? After the infinite loop, or before it? :)
I hope I understood the task correctly

do {
 // some code
} while (!empty($array))

But all in all it's weird.

D
Dark19, 2015-06-24
@Dark19

I wrote the code myself, so I didn’t really want to spread it, the bike. This is the final stage of the Clark-Wright algorithm. Here I check the finiteness of arrays like this

!next($data[$arrCenter[$i]]['j']) && !next($data[$arrCenter[$i]]['i']

but it doesn't work correctly.
$machin = 1200;
$iter = ceil((count($data[$arrCenter[$i]]['j']) + count($data[$arrCenter[$i]]['i'])) / 2) + ceil($w / $machin);

                    for($a=0;$a<$iter;$a++){

                            if($data[$arrCenter[$i]]['j'][$a]['order'] == 0 && $data[$arrCenter[$i]]['i'][$a]['order'] == 0){

                                if(($data[$arrCenter[$i]]['i'][$a]['weight'] + $data[$arrCenter[$i]]['j'][$a]['weight']) <= $machin) {
//проверяем вес, если меньше допустимого, записываем точку пути в новый массив и удаляем данный элемент с текущего
                                    $weight[$a] = $data[$arrCenter[$i]]['i'][$a]['weight'] + $data[$arrCenter[$i]]['j'][$a]['weight'];

                                    array_unshift($path[$arrCenter[$i]][$c], $data[$arrCenter[$i]]['i'][$a]['name']);
                                    unset($data[$arrCenter[$i]]['i'][$a]);
                                    echo $data[$arrCenter[$i]]['j'][$a]['name'];echo "<br/>";
                                    array_push($path[$arrCenter[$i]][$c], $data[$arrCenter[$i]]['j'][$a]['name']);
                                    unset($data[$arrCenter[$i]]['j'][$a]);

                                }
                            }

                            if($data[$arrCenter[$i]]['j'][$a]['order'] < $data[$arrCenter[$i]]['i'][$a]['order']){
                                //проверяем вес, если меньше допустимого, записываем точку пути в новый массив и удаляем данный элемент с текущего
                                if((array_sum($weight) + $data[$arrCenter[$i]]['j'][$a]['weight']) <= $machin) {

                                    $weight[$a] = array_sum($weight) + $data[$arrCenter[$i]]['j'][$a]['weight'];

                                    array_push($path[$arrCenter[$i]][$c], $data[$arrCenter[$i]]['j'][$a]['name']);
                                    unset($data[$arrCenter[$i]]['j'][$a]);

                                }
//проверяем вес, если меньше допустимого, записываем точку пути в новый массив и удаляем данный элемент с текущего
                                if((array_sum($weight) + $data[$arrCenter[$i]]['i'][$a]['weight']) <= $machin){

                                    $weight[$a] = array_sum($weight) + $data[$arrCenter[$i]]['i'][$a]['weight'];

                                    array_unshift($path[$arrCenter[$i]][$c], $data[$arrCenter[$i]]['i'][$a]['name']);
                                    unset($data[$arrCenter[$i]]['i'][$a]);

                                }

                            }


                            if($data[$arrCenter[$i]]['i'][$a]['order'] < $data[$arrCenter[$i]]['j'][$a]['order']){
                                //проверяем вес, если меньше допустимого, записываем точку пути в новый массив и удаляем данный элемент с текущего
                                if((array_sum($weight) + $data[$arrCenter[$i]]['i'][$a]['weight']) <= $machin) {

                                    $weight[$a] = array_sum($weight) + $data[$arrCenter[$i]]['i'][$a]['weight'];
                                    array_unshift($path[$arrCenter[$i]][$c], $data[$arrCenter[$i]]['i'][$a]['name']);
                                    unset($data[$arrCenter[$i]]['i'][$a]);

                                }
//проверяем вес, если меньше допустимого, записываем точку пути в новый массив и удаляем данный элемент с текущего
                                if((array_sum($weight) + $data[$arrCenter[$i]]['j'][$a]['weight']) <= $machin){
                                    $weight[$a] = array_sum($weight) + $data[$arrCenter[$i]]['j'][$a]['weight'];
                                    array_push($path[$arrCenter[$i]][$c], $data[$arrCenter[$i]]['j'][$a]['name']);
                                    unset($data[$arrCenter[$i]]['j'][$a]);

                                }
                            }
//если дошли до конца текущих массивов и они не пустые обнуляем значения
                        if ((!next($data[$arrCenter[$i]]['j']) && !next($data[$arrCenter[$i]]['i']) && (!empty($data[$arrCenter[$i]]['j']) && !empty($data[$arrCenter[$i]]['j'])))){
                            $a = -1;
                            $weight = array();
                            $data[$arrCenter[$i]]['j']=array_values($data[$arrCenter[$i]]['j']);
                            $data[$arrCenter[$i]]['i']=array_values($data[$arrCenter[$i]]['i']);
                            $c++;
                            $path[$arrCenter[$i]][$c] = array();
                            $iter = ceil((count($data[$arrCenter[$i]]['j']) + count($data[$arrCenter[$i]]['i'])) / 2);
                        }
                        else{
                            break;
                        }
                    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question