Problem with multidimensional array 3x3 delete function item

1

Good morning colleagues, I have a problem with arrays that is breaking my head, it turns out that I have this dimensional arrangement.

 array(3) { 
[1]=> array(1) {["PC150"]=> 
array(7) { [0]=> string(5) "HABPC" [1]=> string(2) "PC" [2]=> string(1) "2" 
[3]=> string(24) "EXTRACTOR PREMIUM CHOICE" [4]=> string(5) "Pieza" [5]=> 
string(1) "2" [6]=> string(13) " $ 154.00 USD" } } 
[2]=> array(1) { ["5PC-ACC01"]=> 
array(7) { [0]=> string(5) "HABPC" [1]=> string(2) "PC" [2]=> string(1) 
"3" [3]=> string(24) "EXTRACTOR PREMIUM CHOICE" [4]=> string(5) "Pieza" 
[5]=> 
string(1) "3" [6]=> string(12) " $ 39.00 USD" } } 
[3]=> array(1) { ["5PC-ACC05"]=> 
array(7) { [0]=> string(5) "HABPC" [1]=> string(2) "PC" [2]=> string(1) 
"3" [3]=> string(24) "EXTRACTOR PREMIUM CHOICE" [4]=> string(5) "Pieza" 
[5]=> string(1) "3" [6]=> string(11) " $ 7.00 USD" } } }

In a json file it would be more understandable in the following way.

{"1":{"PC150":
["HABPC","PC","2","EXTRACTOR PREMIUM CHOICE","Pieza","2"," $ 154.00 
USD"]},
"2":{"5PC-ACC01":
 ["HABPC","PC","3","EXTRACTOR PREMIUM 
CHOICE","Pieza","3"," $ 39.00 USD"]},
"3":{"5PC-ACC05":
["HABPC","PC","3","EXTRACTOR PREMIUM CHOICE","Pieza","3"," $ 7.00 USD"]}}

What I need is to iterate the array and delete the index and reorder the indexes.

I'm doing it this way:

The variable model is the identifier that you pass example [PC150] or [5PC-ACC01], to be able to erase it.

eliminar($modelo){
  //lo obtengo de una cookie y lo guaro en arreglo

  $aCarrito = json_decode($_COOKIE['carrito'], true);
  $c  = $_COOKIE['contItems']; //tambien obtengo mi contador de articulos
 //itero la primer vez
foreach ($aCarrito as $key => $ubc) {
//en $key se guardan 1,2,3,4 $ubc es otro array
    //itero ubx para obtener el modelo
     foreach ($ubc as $mode => $mod) {
        //hago coincidir los modelos con el modelo pasado en la funcion
        if ($mode == $modelo) {
             // elimino la posicion del arreglo
            unset($aCarrito[$i][$modelo]);


        }

    }
    $i++;
}
  } 

And the arrangement remains as follows when it is removed (that works, delete the content in that index).

{"1":[ ],"2":[ ],"3":[ ]} , but I want it to be completely deleted with the index I also tried with unset($aCarrito[ $i ]); , and it also deletes the complete index (as I wish), but I need to reorder the array.

Any ideas to delete the array by passing it the $ model variable and reorder everything?

    
asked by Manueel Perezz 02.04.2018 в 19:33
source

1 answer

2

A function for you ..

/* Array de Ejemplo */ 
$array = array(array('PC150'=>['HABPC', 'PC']),
               array('5PC-ACC01'=>['HABPC', 'PC']),
               array('5PC-ACC05'=>['HABPC', 'PC']),
               array('5PC-ACC06'=>['HABPC', 'PC']));

function deleteIdx( array $arr, string $whatever ) {
    $idk = array_map(function($e) use ($whatever){
        return key($e) == $whatever ? false : $e;
    }, $arr);
    return array_values(array_filter($idk));
}

$newArr = deleteIdx($array, 'PC150');  

This function can be used in other similar arrangements, and allows you to specify the index to be deleted in the second parameter. All you have to do is store the filtered array in a variable.

    
answered by 02.04.2018 / 23:45
source