Delete Item Array

1

I'm trying to remove an item from a array corresponding to [NUMBER] but it's not working.

Array:

Array
(
    [0] => Array
        (
            [NUMERO] => 123
            [DATA] => 11/11/1111
            [VALOR] => 2,22
            [ARQUIVO] =>
        )

    [1] => Array
        (
            [NUMERO] => 456
            [DATA] => 12/12/1212
            [VALOR] => 33,33
            [ARQUIVO] => 
        )
)

I called this function to return the KEY and then remove it:

function hasArrayKey( $array, $value ){
    foreach( $array as $key => $temp ){
        if( array_search( $value, $temp ) !== false ){
            return $key;
        }
    }
  return false;
}

Plus it removes the KEY "0" if it does not find:

$nota = hasArrayKey( $_SESSION['NOTAS'], '999' );
if( $nota >= 0 ){
    unset( $_SESSION['NOTAS'][$nota] );
}
if( !$nota ){
    echo "Nota not found\n";
}

Any ideas, guys?

    
asked by anonymous 29.10.2014 / 17:11

3 answers

1

Hugo, I've changed your function to get parameters as a reference , so it's simpler to remove the unwanted lines.

Based on your comment, I found it safer to change the array_search comparison to a simple NUMERO check. If the field VALOR is equal to the field NUMERO , this line is also deleted (Example Numero = 2543 and Valor = 25 , case is passed 25 to the function, that position will be removed).

<?php

function removeByNumero(&$array, $numero){
    foreach($array as $key => $row){
        if($row['NUMERO'] == $numero){
            unset($array[$key]);
        }
    }
}

$array = array(
    0 => array(
            'NUMERO' => 123,
            'DATA' => '11/11/1111',
            'VALOR' => 2.22,
            'ARQUIVO' => 'XPTO'
        ),
    1 => array(
            'NUMERO' => 456,
            'DATA' => '12/12/1212',
            'VALOR' => 33.33,
            'ARQUIVO' => 'XPPT'
    ),
);

removeByNumero($array, 234);
var_dump($array);   // Não remove nada

removeByNumero($array, 123);
var_dump($array);   // Remove a key 0
    
29.10.2014 / 17:54
1

The problem is in the second part of the code.

$nota = hasArrayKey( $_SESSION['NOTAS'], '999' );
if( $nota >= 0 ){
    unset( $_SESSION['NOTAS'][$nota] );
}
if( !$nota ){
    echo "Nota not found\n";
}

In the $ snippet segment, it will always return TRUE , as php automatically converts FALSE to 0 and vice versa.

Then it goes through if, after all FALSE == 0 is TRUE .

0) .

I recommend the if ($ note) .

    
29.10.2014 / 17:31
1

What is happening is that if it does not find it returns false for the variable $nota .

No if when you make a mathematical comparison it converts to int .

So if if( $nota >= 0 ) is the same as 0 >= 0 and so it enters if and removes the 0 key element.

Consider changing the code to look like this:

function hasArrayKey( $array, $value ){
    foreach( $array as $key => $temp ){
        if( array_search($value, $temp) ){
            return $key; 
        }
    }
}

$keyToRemove = hasArrayKey( $_SESSION['NOTAS'], '123' ) ;
if( $keyToRemove >= 0 ) {
    unset( $_SESSION['NOTAS'][$keyToRemove] ); 
} else {
    echo "Nota not found\n";
}
    
29.10.2014 / 17:27