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
https://forums.phpfreaks.com/topic/78250-numeric-arrays/#findComment-395993
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
https://forums.phpfreaks.com/topic/78250-numeric-arrays/#findComment-396021
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
https://forums.phpfreaks.com/topic/78250-numeric-arrays/#findComment-396028
Share on other sites

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.