Jump to content

Numeric Arrays


Dysan

Recommended Posts

I presume manually, please correct me if mistaken.

 

Basically I want to store link's ID (The ID at the end of each link) inside an array, upon a link being clicked (e.g. Link 1 shown below). Then upon another link being clicked (e.g Link 2 shown below), delete the appropriate ID from the array, depending on what ID the link contains?

 

1) www.website.co.uk/functions.php?action=add&id=1 - Add Item

 

2) www.website.co.uk/functions.php?action=delete&id=1 - Delete Item

 

Cheers.

Link to comment
Share on other sites

There are a number of ways to do things like you're after. Here are a few functions to look at:

array_push()

array_pop()

unset()

 

Something like this script may help, too:

<?php
header('Content-type: text/plain');
$numbers = array(); // empty
for ($i = 1; $i <= 10; $i++)
{
  $numbers[] = $i;
}
print_r($numbers);

unset($numbers[4]);
print_r($numbers);

array_pop($numbers);
print_r($numbers);

$new = array();
foreach ($numbers as $k => $v)
{
  if ($k != 3 && $k != 7)
  {
    $new[$k] = $v;
  }
}
print_r($new);
?>

Link to comment
Share on other sites

What does that do?

Can you give me some example code, on how to actually add the id number found at the end of the links, to the array?

 

I think what you're wanting is something like this:

<?php
$id = $_GET['id'];

$array = array(1, 2, 3, 4, 5);
if ($_GET['action'] == 'add')
{
  $array[] = $id;
  // ...or...
  array_push($array, $id);
}
elseif ($_GET['action'] == 'delete')
{
  $new = array();
  foreach ($array as $item)
  {
    if ($item != $id)
    {
      $new[] = $item;
    }
  }
  $array = $new;
}
?>

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.