AikenDrum Posted February 13, 2010 Share Posted February 13, 2010 Hi there, I have 3 POSTed arrays - each with the same amount of elements, but in some elements the values will be missing. I am trying to remove related items from the arrays where $postQty has no value in it. In other words - in each array, the element is only valid where there is a value in $postQty in the corresponding index Example array after POSTing: $postID = [O] => 2 [1] => 4 [2] => 7 $postQty = [O] => 20 [1] => [2] => 5 $postQtyGot = [O] => 7 [1] => [2] => In $postQty above, the second element [1] has no value. I need the corresponding (by index) elements removed in all the arrays - so if in $postQty, element [1] has no value, then element [1] should be removed from all 3 arrays - as below $postID = [O] => 2 [1] => 7 $postQty = [O] => 20 [1] => 5 $postQtyGot = [O] => 7 [1] =>" The elements have been removed from the arrays where the index corresponds to the element index in $postQty where there was no value. Here is my code so far : $countID = count($postID); // how many have been posted $x=0; // counter echo $countID; // test while ($x < $countID) { if( $postQty[$x]==''){ unset($postID[$x]); unset($postQty[$x]); unset($postQtyGot[$x]); continue; } $x++; } echo '<pre>'; print_r($postID); print_r($postQty); print_r($postQtyGot); echo '</pre>'; exit(); It takes ages to run, then only echos the $countID, nothing is retunred from print_r Anyone able to see what I am doing wrong ? much Appreciated, really, in advance, AikenD Link to comment https://forums.phpfreaks.com/topic/191938-remove-from-multiple-arrays-related-by-key/ Share on other sites More sharing options...
cwarn23 Posted February 13, 2010 Share Posted February 13, 2010 Is this what your after. <?php $postID=stripslashes($_POST['postid']); $postQty=stripslashes($_POST['postqty']); $postQtyGot=stripslashes($_POST['postqtygot']); $countID = count($postID); // how many have been posted $x=0; // counter echo $countID; // test for ($x=0;$x < $countID;$x++) { if( $postQty[$x]=='' || $postID[$x]=='' || $postQtyGot[$x]==''){ unset($postID[$x],$postQty[$x],$postQtyGot[$x]); } } echo '<pre>'; print_r($postID); print_r($postQty); print_r($postQtyGot); echo '</pre>'; exit(); Link to comment https://forums.phpfreaks.com/topic/191938-remove-from-multiple-arrays-related-by-key/#findComment-1011651 Share on other sites More sharing options...
teamatomic Posted February 13, 2010 Share Posted February 13, 2010 $new_array= array_filter($array); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/191938-remove-from-multiple-arrays-related-by-key/#findComment-1011702 Share on other sites More sharing options...
AikenDrum Posted February 13, 2010 Author Share Posted February 13, 2010 Hi there, when I use it together with array_intersect_key it works - many thanks I really need to go to bed before silly hour ! Thank you Link to comment https://forums.phpfreaks.com/topic/191938-remove-from-multiple-arrays-related-by-key/#findComment-1011869 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.