How to find value in array and remove it?

array:10 [
 0 => array:8 [
 "url" => "example.com"
 "ip" => "2345"
]
 1 => array:8 [
 "url" => "example2.com"
 "ip" => "3453"
],
 2 => array:8 [
 "url" => "test.com"
 "ip" => "222"
 ]

you need to find in the array example.com if it is then remove the array where it is found
ie this one
0 => array:8 [
 "url" => "example.com"
 "ip" => "2345"
 ]
March 23rd 20 at 19:39
4 answers
March 23rd 20 at 19:41
Solution
Problem: need to filter array element according to one criterion (you have it -- the presence of a url in one of its subelements)

Solution: use filter (function array_filter made specifically for these operations), which will check for the required condition (our criteria) and return false or true, thereby say-to keep the item or delete it.

$badUrl = "example.com";

$resultArray = array_filter($startArray, function($arr) use ($badUrl) {
 return !(isset($arr['url']) && $arr['url'] === $excludeUrl);
});


PHP 7.4 (when available short lambda):
$badUrl = "example.com";
$resultArray = array_filter(
 $startArray, 
 fn($arr) => !(isset($arr['url']) && $arr['url'] === $excludeUrl)
);


An example of work for your example: https://ideone.com/eXWm3o
Documentation: https://www.php.net/manual/ru/function.array-filter.php
what excludeUrl ? - Miguel.Ratke commented on March 23rd 20 at 19:44
@Miguel.Ratke, so I quickly sketched, we can not fix are already there, there is already renamed to $badUrl - Hank.Kuhic commented on March 23rd 20 at 19:47
March 23rd 20 at 19:43
Sounds like a job)))
In fact to iterate over the array using foreach(possible to add $i to know the number of iterations for comparison with the keys of the array),you could find the correct value if it is to take $i and delete the item with the same key using unset,but I think there is a more correct option,so in haste.
March 23rd 20 at 19:45
$arr = []; // here the original array

$deleteFromUrl = 'example@com';

foreach ($arr as $key => &$item) {
 if (isset($item['url']) and $item['url'] === $deleteFromUrl) {
unset($item[$key]);
}
}

print_r($arr, true); // here the filtered array
Note a side effect of your decision!

Link $item to the last item of the array will remain after the end of the cycle
foreach.
Read: https://habr.com/ru/post/136835/

And method not working
https://ideone.com/9GUh7V - Hank.Kuhic commented on March 23rd 20 at 19:48
I have not tested this idea.

I agree, generally, in php 7, in the foreach loop by default, there is a reference to $item, so it is necessary to take into account is always now - Catalina.Gre commented on March 23rd 20 at 19:51
@Catalina.Gre, well, lower link unset(item) -- not working idea :--) example you specially with that accent... did remove all the items in it without conditions - Hank.Kuhic commented on March 23rd 20 at 19:54
@Hank.Kuhic, maybe the implementation is not working but the idea is correct. I told you that it was just an idea - Catalina.Gre commented on March 23rd 20 at 19:57
@Catalina.Gre,

we have funny enough, the 2 branches there is a discussion, I apologize, looks like a trailer... it was an accident, I assure you.


BUT! what is it that I am forced to argue, in this case, the idea is not implementation but the availability cycle - Hank.Kuhic commented on March 23rd 20 at 20:00
@Hank.Kuhic, corrected response without reference to $item, tested works - Catalina.Gre commented on March 23rd 20 at 20:03
@Hank.Kuhic, I did not understand in what cases the unset is not working, in my case it is - Catalina.Gre commented on March 23rd 20 at 20:06
@Catalina.Gre, Yes, Yes, but the idea is a different (working with an array), but maybe I'm clinging to... well, the keys of the array "leaky", though a trifle - Hank.Kuhic commented on March 23rd 20 at 20:09
@Catalina.Gre, sorry, you suck... just go through Google and try option and will make mistakes - Hank.Kuhic commented on March 23rd 20 at 20:12
@Hank.Kuhic, understand, and have no complaints - Catalina.Gre commented on March 23rd 20 at 20:15
March 23rd 20 at 19:47
$result = array_filter($vasiu, function ($n) {
 return $n['url'] !== 'example.com';
});

Find more questions by tags PHP