Jump to content

removing elements from a multi-dimension array?


DCM

Recommended Posts

Hi I have a multi-dimension session array as per the below:

 

$_SESSION['myarray'][][]

 

If for example there are several elements stored in the array:

 

$_SESSION['myarray'][0][0] = "aaa";

$_SESSION['myarray'][0][1] = "111";

 

$_SESSION['myarray'][1][0] = "bbb";

$_SESSION['myarray'][1][1] = "222";

 

$_SESSION['myarray'][2][0] = "ccc";

$_SESSION['myarray'][2][1] = "333";

 

$_SESSION['myarray'][2][0] = "ddd";

$_SESSION['myarray'][2][1] = "444";

 

How can i remove one of the above and shunt the other elements up?

 

I have tried using the unset command but this seems to essentailly nullify the desired values where as what i would like to do is remove them completly from the array and then  shorten the total number of records stored in the array.

 

Thanks for taking the time to read this.

Link to comment
Share on other sites

Sure the above is simply and example but if example the array at present is as above such that the values assigned are

 


$_SESSION['myarray'][0][0] = "aaa";
$_SESSION['myarray'][0][1] = "111";

$_SESSION['myarray'][1][0] = "bbb";
$_SESSION['myarray'][1][1] = "222";

$_SESSION['myarray'][2][0] = "ccc";
$_SESSION['myarray'][2][1] = "333";

$_SESSION['myarray'][2][0] = "ddd";
$_SESSION['myarray'][2][1] = "444";

 

 

A quick for loop to output the results given that $numrecs currently equals 4

 


for ($x=0; $x<$numrecs; $x++)
{
echo $_SESSION['myarray'][$x][0] ."<br>";
echo $_SESSION['myarray'][$x][1] ."<br>";
}

 

would give as output:

 

aaa

111

bbb

222

ccc

333

ddd

444

 

If say record $_SESSION['myarray'][1] has been deleted in the back end data base and $numrecs reduced by one,

then on refresh of the webpage i would the contents of the array to display as:

 

aaa

111

ccc

333

ddd

444

 

However at present i can't seem to get this to work, to delete the data in the array I do

 


unset ($_SESSION['myarray'][1][0]);
unset ($_SESSION['myarray'][1][1]);

 

However on re-running the for loop i then get the below as output:

 

aaa

111

ccc

333

 

 

When it should be:

 

aaa

111

ccc

333

ddd

444

Link to comment
Share on other sites

typo in presious reply, the original array is defined as :

$_SESSION['myarray'][0][0] = "aaa";
$_SESSION['myarray'][0][1] = "111";
$_SESSION['myarray'][1][0] = "bbb";
$_SESSION['myarray'][1][1] = "222";
$_SESSION['myarray'][2][0] = "ccc";
$_SESSION['myarray'][2][1] = "333";
$_SESSION['myarray'][3][0] = "ddd";
$_SESSION['myarray'][3][1] = "444";

Link to comment
Share on other sites

Honestly, I think all you need to use is the sort method:

 

$array[0][0] = "aaa";
$array[0][1] = "111";

$array[1][0] = "bbb";
$array[1][1] = "222";

$array[2][0] = "ccc";
$array[2][1] = "333";

$array[3][0] = "ddd";
$array[3][1] = "444";

unset($array[1]);
sort($array);

$numrecs = count($array);
for ($x=0; $x<$numrecs; $x++)
{
echo $array[$x][0] ."<br>";
echo $array[$x][1] ."<br>";
}

 

Should do what you want.

Link to comment
Share on other sites

Thanks I'll give it a try..

 

I was also looking at array_splice which may also work without me having to sort the array. Idealy i do not want to disturb the order in which data is displayed in the array as it (in my real app) will have been pr-ordered by the user selecting from a sort by radio button selection.

 

making progress though so thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.