0

I have an array of image data like this:

[other-image] => Array
        (
            [img] => Array
                (
                    [0] => 1526973657.jpg
                    [1] => 1526973661.jpg
                    [2] => 1526973665.jpg
                )

            [path] => Array
                (
                    [0] => ../post-upload/1/
                    [1] => ../post-upload/1/
                    [2] => ../post-upload/1/
                )

            [type] => Array
                (
                    [0] => 1
                    [1] => 1
                    [2] => 1
                )

            [thumb] => Array
                (
                    [0] => thumb_1526973661.jpg
                    [1] => thumb_1526973665.jpg
                    [2] => thumb_1526973668.jpg
                )

        )

Now I want to delete an image and it's all related data from sub arrays. (path, type, thumb data)

This is how I tried it in php:

  $delkey = '1526973657.jpg';
  if(in_array($delkey, $_SESSION['other-image']['img'])){
      $imgkey = array_search($delkey, $_SESSION['other-image']['img']);
      if($imgkey) unset($_SESSION['other-image']['img'][$imgkey]);
  }

But problem is I can't delete related data from other arrays. Can anybody tell me how to do this? Thank you.

3 Answers 3

1

You should use !==false after array_search() because it may return first index i.e. 0 in some cases, so your condition will not executed. And regarding delete related data from other arrays, you have to unset other data related to that key.

if($imgkey!==false){
        unset($_SESSION['other-image']['img'][$imgkey]);
        unset($_SESSION['other-image']['path'][$imgkey]);
        unset($_SESSION['other-image']['type'][$imgkey]);
        unset($_SESSION['other-image']['thumb'][$imgkey]);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

yes its working. but I cant get the logic.. can you kindly elaborate?
From php.net/manual/en/function.array-search.php - Returns the key for needle if it is found in the array, FALSE otherwise. In your example, you are searching '1526973657.jpg' and it is on 0 index of the array so array search will return 0. if you write if($imgkey) , it will not go inside if condition so data will not be removed even it existed in the array.
1

Is the related data has same key with img? If they are same, I think you only need to add some codes to delete other data like the way was used to delete img.

if($imgkey) unset($_SESSION['other-image']['path'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['type'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['thumb'][$imgkey]);

Comments

1

If the keys in img sub-array are related with the same key(index) in sub-arrays(path, type and thumb, you can also unset those keys. e.g.

$delkey = '1526973657.jpg';
if(in_array($delkey, $_SESSION['other-image']['img'])){
    $imgkey = array_search($delkey, $_SESSION['other-image']['img']);
    if($imgkey){
        unset($_SESSION['other-image']['img'][$imgkey]);
        unset($_SESSION['other-image']['path'][$imgkey]);
        unset($_SESSION['other-image']['type'][$imgkey]);
        unset($_SESSION['other-image']['thumb'][$imgkey]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.