PHP 如果在数组中找不到则取消设置父级
如果我在孩子身上找不到针头,我就会遇到一个令我父母不安的问题,由于某种原因,无论我尝试多少种不同的方法,我都无法让它发挥作用,有人可以为我指出正确的方向吗?这是向底深度约为 4 的多维数组抛出的。下面是代码和数组切片的示例。在这种情况下,仅应保留 Array[3],应删除 Array 的 [1-2]。
array (
1 =>
array (
197015 =>
array (
345415 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213354 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
2 =>
array (
197014 =>
array (
345414 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213353 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
3 =>
array (
197013 =>
array (
345412 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => 'Jedi',
'products_quantity' => '1',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
345413 =>
array (
'options' =>
array (
'' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
这是我尝试使用但没有成功的最后一个代码的示例,如果您替换 unset($k);返回 false;它将返回针出现在哪个辅助数组中。
// array_search with recursive searching, optional partial matches and optional search by key
function array_find_r($needle, &$haystack, $partial_matches = false, $search_keys = false) {
if(!is_array($haystack)) return false;
foreach($haystack as $key=>&$value) {
$what = ($search_keys) ? $key : $value;
if($needle===$what) return $key;
else if($partial_matches && @strpos($what, $needle)!==false) return $key;
else if(is_array($value) && array_find_r($needle, $value, $partial_matches, $search_keys)!==false) return $key;
}
unset($k);
}
$tty = array();
foreach($datas as &$k) {
$tty[] = array_find_r('Jedi', &$k, true, false);
}
$tty=array_filter($tty); rsort($tty);
echo '<pre>'; var_export($datas); echo '</pre>';
I'm having an issue unsetting my parent if I do not find my needle in my child, for some reason I just cannot get this to work no matter how many different ways I try it, could someone possibly point me in the right direction? This is being thrown at a multidimensional array with a floor depth of about 4. Here's the code and an example of what a slice of the array might look like. In this case only Array[3] should remain, Array's [1-2] should be removed.
array (
1 =>
array (
197015 =>
array (
345415 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213354 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
2 =>
array (
197014 =>
array (
345414 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213353 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
3 =>
array (
197013 =>
array (
345412 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => 'Jedi',
'products_quantity' => '1',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
345413 =>
array (
'options' =>
array (
'' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
Here's a sample of the last code I tried using without success, if you replace unset($k); with return false; it will return which secondary array the needle appears in.
// array_search with recursive searching, optional partial matches and optional search by key
function array_find_r($needle, &$haystack, $partial_matches = false, $search_keys = false) {
if(!is_array($haystack)) return false;
foreach($haystack as $key=>&$value) {
$what = ($search_keys) ? $key : $value;
if($needle===$what) return $key;
else if($partial_matches && @strpos($what, $needle)!==false) return $key;
else if(is_array($value) && array_find_r($needle, $value, $partial_matches, $search_keys)!==false) return $key;
}
unset($k);
}
$tty = array();
foreach($datas as &$k) {
$tty[] = array_find_r('Jedi', &$k, true, false);
}
$tty=array_filter($tty); rsort($tty);
echo '<pre>'; var_export($datas); echo '</pre>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不将您确实需要的数据推送到新数组中并取消整个旧数组的设置?
Why not push the data you do need into a new array and unset the whole old array?