Array

How to find a key in an array by value in php
How to find a key in an array by value in php

To determine the key of an array by knowing its value, there are at least three possible solutions:

  1. You can use the well-known array_search function. We already used it earlier in the article on removing an element from an array.
  2. Use a loop to iterate through all elements of the array and compare values to identify the key.
  3. Use the array_walk function, imitating option 2.

read more...

How to remove array element by value
How to remove array element by value

It seems like a fairly trivial task, and such a function should be built into PHP’s core — but unfortunately, it’s not. So we have to find our own solutions for implementing this functionality. One such solution for removing an array element by its value is shown below:

if(($delete_key = array_search($search_value, $my_array)) !== false)
{
	unset($my_array[$delete_key]);
}

You could say it’s just two lines — and the functionality is ready.

read more...