In PHP, deleting elements from a multidimensional array requires a structured approach to identify and remove specific sub-arrays based on a designated key-value pair.
To achieve this, utilize a custom function:
<code class="php">function removeElementWithValue($array, $key, $value) { foreach ($array as $subKey => $subArray) { if ($subArray[$key] == $value) { unset($array[$subKey]); } } return $array; }</code>
This function takes three parameters: the original array, the key corresponding to the desired value, and the value to match. It iterates through each sub-array, comparing the specified key's value to the target value. If a match is found, it removes the entire sub-array using the unset() function.
For example, to delete all sub-arrays where the "year" key has the value 2011:
<code class="php">$array = removeElementWithValue($array, "year", 2011);</code>
This will effectively filter out the sub-arrays that meet the criteria, resulting in a modified array with those sub-arrays removed.
위 내용은 PHP에서 키-값 쌍을 기반으로 다차원 배열 요소를 제거하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!