Jump to content

Adding And Removing From A MultiDimensional Array


tarun

Recommended Posts

This Is My QUICK Example

 

items.php Contains The Array

<?php
$itemList = array(

'000001'=>array('ID' => '000001','name' => 'Item 1','price' => '99.99','img' => 'images/1.jpg','cat' => 'Category 1'),

'000002'=>array('ID' => '000002','name' => 'Item 2','price' => '49.99','img' => 'images/2.jpg','cat' => 'Category 2')

);
?>

 

 

add.php Is The Page Which Adds To The Array

It Uses include 'items.php'; To Include The Array In The File

 

delete.php Is The Page Which Removes Things From The Array

It Also Uses include 'items.php'; To Include The Array In The File

 

BUT How Do I Actually Add And Remove Things To/From THIS Array

 

 

Thnx,

Tarun

Link to comment
Share on other sites

see here

 

<?php
$itemList = array(
'000001'=>array('ID' => '000001','name' => 'Item 1','price' => '99.99','img' => 'images/1.jpg','cat' => 'Category 1'),
'000002'=>array('ID' => '000002','name' => 'Item 2','price' => '49.99','img' => 'images/2.jpg','cat' => 'Category 2')
);

echo "before<br>";
print_r($itemList);

$itemList['000003'] = array('ID' => '000003','name' => 'Item 3','price' => '69.99','img' => 'images/3.jpg','cat' => 'Category 3');

echo "<br><br>after add<br>";
print_r($itemList);

echo "<br><br>after remove<br>";
unset($itemList['000003']);
print_r($itemList);

?>

 

 

Oh yeah just incase

 

<?php
//to change data
$itemList['000003']['name'] = "New data 3";
?>

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.