Jump to content

remove from multiple arrays - related by key


AikenDrum

Recommended Posts

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

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();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.