In PHP is it possible to recover the values deleted with unset in another array?

0

I was testing the function of Jack P. , in which, given a $arrOriginal and a $arrRemover it is possible to obtain a $arrNuevo without the keys indicated $arrRemover .

But, if I would also like to recover the values and keys eliminated with $arrRemovidos in unset , how could I do it? That is, have two arrays, one with the keys and values that are not in $arrRemover and another with the keys and values that are in $arrRemover .

I was looking if unset had some opposite, but I found nothing.

Expressed in code would be:

$arrOriginal=
        array(
                array(
                        "Id"=> 1,
                        "Nombre"=> "Pedro",
                        "Ciudad"=> "Galilea"
                      ),

                array(
                        "Id"=> 2,
                        "Nombre"=> "Santiago",
                        "Ciudad"=> "Betsaida"
                     )
            );
$arrRemover=array("Id","Nombre");

This is what the function does:

$arrNuevo=
        array(
                array(
                        "Ciudad"=> "Galilea"
                      ),

                array(
                        "Ciudad"=> "Betsaida"
                     )
            );

This is what I would also like to obtain, but I do not know how to do it.

$arrRemovidos=
        array(
                array(
                        "Id"=> 1,
                        "Nombre"=> "Pedro"
                      ),

                array(
                        "Id"=> 2,
                        "Nombre"=> "Santiago"
                     )
            );
    
asked by A. Cedano 13.09.2017 в 04:15
source

1 answer

2

I think I have it, this is a modified version of the original function.

I added the variable $removed to keep the values as they are deleted.

In the variable $old the content of $array is copied before starting to remove elements, and then at the end it is compared with the content that has been left.

The array_diff_key function returns an array with all the keys that are in $old and are not in $array .

function array_remove_keys($array, $keys, &$removed)
{      
  $old = $array;

  // Loop over the array
  foreach ($array as $key => $value) {
    // Check if we want to remove it...

    if (!is_numeric($key) and in_array($key, $keys)) {      
        unset($array[$key]);
        continue;
   }        

    // Filter the value if it's an array also
    $array[$key] = is_array($value) ? array_remove_keys($value, $keys, $removed) : $value;

   }

   $diff = array_diff_key($old, $array);
   if (count($diff) > 0) {
     $removed[] = $diff;
   }    

   return $array;
}

Then, with the example:

$arrOriginal=
            array(
            array(
                    "Id"=> 1,
                    "Nombre"=> "Pedro",
                    "Ciudad"=> "Galilea"
                  ),

            array(
                    "Id"=> 2,
                    "Nombre"=> "Santiago",
                    "Ciudad"=> "Betsaida"
                 )
        );

$arrRemover=array("Id","Nombre");

$arrRemovidos = array();
$arrNuevo = array_remove_keys($arrOriginal, $arrRemover, $arrRemovidos);

var_dump($arrRemovidos);

Result:

array (size=2)
  0 => 
    array (size=2)
      'Id' => int 1
      'Nombre' => string 'Pedro' (length=5)
  1 => 
    array (size=2)
      'Id' => int 2
      'Nombre' => string 'Santiago' (length=8)
    
answered by 13.09.2017 / 05:58
source