Delete Item Array

Asked

Viewed 799 times

1

I’m trying to remove an item from a array corresponding to [NUMERO] but 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] => 
        )
)

Bolei this function to returns the KEY to 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?

  • You want to remove from the array by value or key ?

  • Remove by [NUMBER value]

  • Do you know if he is finding $key correct? ever tried to print $key?

  • Yes he thinks the KEY is right

  • Related: http://answall.com/a/34790/7210

3 answers

1

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

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

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

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";
}
  • It didn’t work!.. if I go to remove the [NUMERO] => 123 he doesn’t think.

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 $note >= 0 snippet it will always return TRUE, because php automatically converts FALSE for 0 and vice versa.

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

Just below it makes unset( $_SESSION['NOTES'][$note] );

Again the $note has the value FALSE, but PHP will convert to 0.

Thus deleting the first array key.

You can do if($note) only, or else if($note > 0).

I recommend the if($note).

  • Anyway not sure see: if I am going to delete the [NUMERO] => 123 her key is 0 soon if($note) not exclude

  • @Hugo. The whole question is if( $note >= 0 ). That if will ALWAYS be TRUE. It will ALWAYS enter the subblock. understand?

1


Hugo, I’ve changed your role to receive parameters as reference, so it becomes simpler to remove unwanted lines.

Based on your comment, I thought it would be safer to exchange the comparison of array_search by a simple check of the position NUMERO. For the field case VALOR is equal to the field NUMERO, that row will be deleted as well (Example Numero = 2543 and Valor = 25, if 25 is passed to the function, this 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
  • Thanks @gmsantos, it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.