How to delete array value by the value of one of the elements?

0

I have the following array:

array(3) { 
   [1]=> array(3) { 
      ["ordem"]=> string(1) "2" 
      ["img"]=> string(66) "banner.jpg" 
      ["chave"]=> string(20) "imagem_5adbea7baefaf" 
    } 
   [2]=> array(3) { 
      ["ordem"]=> int(1) "2" 
      ["img"]=> string(70) "Untitled-6.jpg" 
      ["chave"]=> string(20) "imagem_5adbeb0d0f382" 
   }
}

I need to remove, delete from the array a position depending on the value "passed key". For example if the value passed in the function is "image_5adbeb0d0f382" then I have to delete index 2 from the array. Can anyone help me?

    
asked by anonymous 22.04.2018 / 14:52

2 answers

0
<?php

$foo = [
    1 => ["ordem" => 2, "img" => "banner.jpg", "chave" => "imagem_5adbea7baefaf"],
    2 => ["ordem" => 2, "img" => "Untitled-6.jpg", "chave" => "imagem_5adbeb0d0f382"]
    ];
echo "INICIALIZAÇÃO:";
echo "<pre>";
print_r($foo);
echo "</pre>";
foreach($foo as $chave => $valor) {
    if($valor['chave'] == 'imagem_5adbeb0d0f382') {
        $chave_encontrada = $chave;
        break;
    }
}
if (isset($chave_encontrada)) {
    echo "------------<br>";
    echo "Deletando a chave ".$chave_encontrada.".<br>";
    echo "------------<br>";
    unset($foo[$chave_encontrada]);
}
echo "FINALIZAÇÃO:";
echo "<pre>";
print_r($foo);
echo "</pre>";
echo "------------<br>";

Just to use example! Now just create your own function to do that service there.

    
22.04.2018 / 16:02
0

I was able to use the following:

foreach ($array as $key => $val) {
 if ($val['chave'] === 'imagem_5adbeb0d0f382' ) {                       
       unset($array[$key]);
   }
 }

Thank you all!

    
22.04.2018 / 18:05