-2

I'm creating a PHP script but I'm a beginner and I'm having a little trouble. I would like to delete these arrays which are added automatically and which come from nowhere and then merge those which have content. I have tried everything especially "array_filter" which does not change the problem of removing empty arrays. Someone can help me ?

Here is my script:

foreach ($items as $it) {
            

            $title = array_key_exists(2, $it) ? $it[2] : null;
            $description = array_key_exists(3, $it) ? $it[3] : null;
            // Multiple images in each key
            $img = array_key_exists(4, $it) ? $it[4] : null;
            $secimg = explode(';', $img);
            $cat = array_key_exists(5, $it) ? $it[5] : null;
            $price = array_key_exists(8, $it) ? $it[8] : null;
            $stock = array_key_exists(6, $it) ? $it[6] : null;


            array_unshift($secimg, $title, $description, $cat, $price, $stock);
            
            array_filter($secimg);

            if(!empty($secimg) && !NULL){
                
                echo '<pre>';
                    print_r($secimg);
                echo '</pre>';
                
            }

Here is the var dump of my script:

    Array
(
    [0] => Coin daté n°327 neufs
    [1] => Value
    [2] => Coins datés
    [3] => 58.0
    [4] => 
    [5] => e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg
    [6] => e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg
)
Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
)
Array
(
    [0] => Coin daté n°327 neufs
    [1] => Value
    [2] => Coins datés
    [3] => 58.0
    [4] => 
    [5] => e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg
    [6] => e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg
)
Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
)

Thank you !

Archer
  • 15
  • 4
  • 1
    Might need you to provide an example of the data as is, and the expected output. Not sure I follow. Does this suggest a solution? [Remove empty array elements](https://stackoverflow.com/questions/3654295/remove-empty-array-elements) – ficuscr Feb 25 '22 at 19:28
  • 2
    Can you also print_r your `$items`? – Peter Feb 25 '22 at 19:31

2 Answers2

0

This will work:

$result = array_filter($arr, fn($item) => array_filter($item));

array_filter without a callback function will not work in this case:

$result = array_filter($arr);   // will not work

because [NULL, NULL, NULL, NULL, NULL, NULL] is not evaluated as empty. See https://www.php.net/manual/en/function.array-filter.php. Especially:

If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case.

lukas.j
  • 6,453
  • 2
  • 5
  • 24
-2

This is what you are looking for, use loop and check if value is empty before assigning to new array

Below is your outputted array structure

<?php
$secimg = array(
    array(
        0 => "Coin daté n°327 neufs",
        1 => "Value",
        2 => "Coins datés",
        3 => 58.0,
        4 => null,
        5 => "e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg",
        6 => "e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg"
    ),
    array(
        0 => null,
        1 => null,
        2 => null,
        3 => null,
        4 => null,
        5 => null
    ),
    array(
        0 => "Coin daté n°327 neufs",
        1 => "Value",
        2 => "Coins datés",
        3 => 58.0,
        4 => null,
        5 => "e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg",
        6 => "e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg"
    ),
    array(
        0 => null,
        1 => null,
        2 => null,
        3 => null,
        4 => null,
        5 => null
    )
);

Use this code

   <?php
   $newArray = array();             
   foreach($secimg as $key => $value){
      foreach($value as $row){
        if(!is_null($row) && strlen($row) > 0){
          $newArray[] = $row;
        }
      }
    }
    print_r($newArray);

Output

Array
(
    [0] => Coin daté n°327 neufs
    [1] => Value
    [2] => Coins datés
    [3] => 58
    [4] => e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg
    [5] => e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg
    [6] => Coin daté n°327 neufs
    [7] => Value
    [8] => Coins datés
    [9] => 58
    [10] => e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg
    [11] => e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg
)
Peter
  • 1,860
  • 2
  • 18
  • 47
  • `empty` will not get if an array contains empty entries and qualify it as empty. Also `empty` will filter `0`, `''` and many other values that aren't `NULL` – medilies Feb 25 '22 at 20:26
  • Would argue both answers are conjecture without the data requested in the comments. – ficuscr Feb 25 '22 at 20:29