I need one help. I need to remove value from multiple array by matching the key using PHP. I am explaining my code below.
$str="ram,,madhu,kangali";
$strid="1,2,,3";
$arr=explode(",",$str);
$arrId=explode(",", $strid);
I have two array ie-$arr,$arrId
which has some blank value. Here I need if any of array value is blank that value will delete and the same index value from the other array is also delete. eg-suppose for $arr the 1 index value is blank and it should delete and also the the first index value ie-2 will also delete from second array and vice-versa.
please help me.
try this:
$str="ram,,madhu,kangali";
$strid="1,2,,3";
$arr=explode(",",$str);
$arrId=explode(",", $strid);
$arr_new=array();
$arrId_new=array();
foreach ($arr as $key => $value) {
if(($value != "" && $arrId[$key] != "")){
array_push($arr_new, $value);
array_push($arrId_new, $arrId[$key]);
}
}
var_dump($arr_new);
var_dump($arrId_new);
Try:
foreach ($arr as $key => $value) {
if ( empty($value) || empty($arrId[$key]) ) {
unset($arr[$key]);
unset($arrId[$key]);
}
}
You can zip both lists together and then only keep entries where both strings are not empty:
$zipped = array_map(null, $arr, $arrId);
$filtered = array_filter($zipped, function ($tuple) {
return !empty($tuple[0]) && !empty($tuple[1]);
});
$arr = array_map(function($tuple) {return $tuple[0];}, $filtered);
$arrId = array_map(function($tuple) {return $tuple[1];}, $filtered);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.