Deleting a value from an array

Hi all,

I have an array stored in a $_SESSION. I'm trying to remove an element but it doesn't seem to be working.

unset($_SESSION['favorite_link_array'][$link_id]);

Open in new window


The array is a list of ids in no particular order like array(12453, 23123, 23424, 43535, 64334) etc..

How do I delete an entry with a matching value? Like from the above example, remove 23424.

Cheers
dolythgoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

babbloCommented:
Try with this function:

 
<?php
function remove_item_by_value($array, $val = '', $preserve_keys = true) {
	if (empty($array) || !is_array($array)) return false;
	if (!in_array($val, $array)) return $array;

	foreach($array as $key => $value) {
		if ($value == $val) unset($array[$key]);
	}

	return ($preserve_keys === true) ? $array : array_values($array);
}
?>

Open in new window


Usage example:

 
<?php
$myArray = array('item1', 'item2', 'item3' ...);
$removeThis = 'item2';

$newArray = remove_item_by_value($removeThis, $removeThis);
?>

Open in new window

0
HainKurtSr. System AnalystCommented:
Usage Example Line 5 above should be

$newArray = remove_item_by_value($myArray, $removeThis);

I guess... are you sure $_SESSION['favorite_link_array'] is array? Maybe it is just a string!
in that case, you can use replace function, or split it make array, remove one item and then join the array to make it a string again and put into session back as string...

$_SESSION['favorite_link_array'] = replace($_SESSION['favorite_link_array'], $link_id,"");

but php supports array in session, so using it as array will make life easier...

http://stackoverflow.com/questions/2306159/array-as-session-variable

your code looks fine as long as yo have a valid array in session...
<?php
$myArray = array('item1', 'item2', 'item3' ...);
$removeThis = 'item2';

$newArray = remove_item_by_value($myArray, $removeThis);
?>

Open in new window

0
kelvinwkwCommented:
<?php

$arr = array('80396','80397','80398','777');

function remove_element($arr, $data){
      $new_array = "";
      foreach($arr as $key => $val ){
            if ( $val != $data )
                  $new_array[] = $val;
      }
      return $new_array;
}

$arr = remove_element($arr, '80397');
print_r($arr);
?>
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Ray PaseurCommented:
The correct design pattern might be to use an array in which the keys had some meaning that was relevant to the process.  I say "might" because there are a number of subtle design questions that would need to be understood before you could choose the right approach.  I am guessing from the context of the constant and variable names in the question that what you are dealing with here are unique elements of some sort -- I may have several favorite links, and I may have an order of preference, but I do not need to keep more than one link id to know that it represents one of my favorite links.  If that's the case, this design might serve you well.  I use it all the time when I have a one-to-many relationship between the data I want to keep and the external inputs.  It works because an array can have only one key of a given value.

Sidebar note: numerically indexed arrays and associative arrays use loose typing for the keys.

HTH, ~Ray
<?php // RAY_temp_dolythgoe.php
error_reporting(E_ALL);
echo "<pre>\n";


// THE TEST DATA SET
$links  = array(12453, 23123, 23424, 43535, 64334);
$remove = 23424;
print_r($links);
print_r($remove);
echo PHP_EOL;

// THE HARD WAY
foreach ($links as $key => $value)
{
    if ($value == $remove) unset($links[$key]);
}

// SHOW THE RESULT
var_dump($links);


// THE TEST DATA SET
$links  = array
( 12453 => 12453
, 23123 => 23123
, 23424 => 23424
, 43535 => 43535
, 64334 => 64334
)
;
$remove = 23424;
print_r($links);
print_r($remove);
echo PHP_EOL;

// THE EASY WAY
unset($links[$remove]);

// SHOW THE RESULT
var_dump($links);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dolythgoeAuthor Commented:
Cheers guys - Ray, professional as always :)

Yes, they are indeed unique values.

So populating your later example, would that be something like:

		while($row=mysqli_fetch_array($result))
		{
			$favorite_link_array[$row['link_id']] = $row['link_id'];
		}

Open in new window

0
Ray PaseurCommented:
Yes, that looks right to me.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.