Home > Mobile >  Delete empty arrays and merge arrays
Delete empty arrays and merge arrays

Time:02-26

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 !

CodePudding user response:

Solution

/**
 * @param array $arr
 * Returns true if all array content is NULL,
 */
function is_empty_array($arr): bool
{
    foreach ($arr as $item)
        if (!is_null($item))
            return false;
    return true;
}
// Supposer that $source is an array that contains the dump you shared

$filtered_array = [];

foreach($source as $entry)){
  if(!is_empty_array($entry){
    $filtered_array[] = $entry;
  }
}

Simulating

$source = [
    [NULL, NULL, NULL, NULL, NULL, NULL],
    [
        "Coin daté n°327 neufs",
        "Value",
        "Coins datés",
        58.0,
        NULL,
        "e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg",
        "e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg",
    ]
];

enter image description here

CodePudding user response:

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
)
  • Related