bookmark_borderHow can I remove a specific item from an array that is stored in a table using PHP?

This is a question posted on Stackoverflow – How can I remove a specific item from an array that is stored in a table using PHP?

The actual issue unknown and it may be related to the Database ORM being used, which I have never sighted before BUT as I spent a lil bit of time looking in to this and ended up writing an answer, I didn’t submit it as it’s not quite addressing the issue but, instead of just deleting it all I thought I’d post it here for reference.

So this was my unposted answer.

Your code appears to work in regards to the removal of the entry from the array… But the indexing will be out of order I.E a Gap will occur.

To simulate this, removing the file “stuff” and working from your code and array example.

NOTE: You can run this in its own file as it’s standalone code.

<?php
// Simulate the Values from the Database
$file_array =  ["166038569262f7799c8af00.jpg", "166038569262f7799caedab.jpg", "166038569262f7799cdc59c.jpg"];
// Simulate the File to Delete from the $file_array
$file_to_delete = "166038569262f7799c8af00.jpg";

$image_sliders = [];
$images = $file_array;

if(!empty($images)) {
    $image_sliders = $images;
    foreach ( $image_sliders as $key=>$value ) {
        echo $key. ' '.$value;
        echo '<br>';;
        if ( $value == $file_to_delete ) {
            unset($image_sliders[$key]);
            $image_sliders = array_values($image_sliders); //Re Index the array
            break;
        }
    }
}
/** @todo  DEBUG */
echo '<pre>';
echo 'LINE: '. __LINE__.'<br>';
var_dump($images);
echo '</pre>';

/** @todo  DEBUG */
echo '<pre>';
echo 'LINE: '. __LINE__.'<br>';
var_dump($image_sliders);
echo '</pre>';  

Or using array_diff to remove an entry by value.

foreach ( $image_sliders as $value ) {
    if ( $value == $file_to_delete ) {
        $image_sliders = array_diff( $image_sliders, array( $file_to_delete ) );
        $image_sliders = array_values($image_sliders);//Re Index the array
        break;
    }
} 

Or you can replace the whole foreach code using array_search..

if(!empty($images)) {
    $image_sliders = $images;
    if (($key = array_search($file_to_delete, $image_sliders)) !== false) {
        unset($image_sliders[$key]);
        $image_sliders = array_values($image_sliders);//Re Index the array
    }
}

And there are other ways to achieve this but that should get you started.

I just found some “other ways” that are worth a read. Deleting an element from an array in PHP


And that is the end of that… Or is it?

bookmark_borderBack from the Wild

Yes, this site has been in limbo for a wee while due a number of reasons I won’t bore you to death with. BUT, it’s time to get back on the horse, so to speak.

The number one reason I have this blog and the major incentive of bringing it back, is to have a place where I can plonk all of my latest and greatest discoveries AND be able to come back and view them.

For the time being, it will mainly be for coding in PHP and Codeigniter 4 (which I am now loving). CodeIgniter 4 has a lot of potential headaches for folks coming from CodeIgniter 2 or 3. It’s a completely different beast and adheres to most parts, the latest PHP standards. Plus it’s been rewritten for PHP 7 AND it uses Namespaces. Namespaces use to put a “blank stare” on my face BUT now I’ve been playing in CI4, it all makes sense, finally.

Of course I am about to embark on the 3D Printing route as soon as that hits the doorstep ( about bleeding time ).

I’m also going to get back into the ole FoamBoard Planes ( See Flitetest.com and discover the joys of it all )

And a whole bunch of other stuff…

As I love to write stuff down, writing stuff on my PC is nice but not as accessible as it should be. I mean, I forget where stuff is. So by putting it on here, I can share it with you and I can also come back and find how I did something in the past.

I use Google a whole bunch, so instead of my reading what others have done, and I am truly grateful to all the great content writers out there, you know who you are, I thought I’d add to the collective Knowledge Pot.

Plus I need to learn to write shorter sentences… Like this one.

So let’s get the show rolling. There’s lots of stuff to do, so I’ll stop flapping my fingers on here and get to some work.

Enjoy!