0

i have an array like below :

 $attributeOptionsData[] = [
                    'id'           => $optionId,
                    'label'        => $attributeOption->label ? $attributeOption->label : $attributeOption->admin_name,
                    'swatch_value' => $attribute->swatch_type == 'image' ? $attributeOption->swatch_value_url : $attributeOption->swatch_value,
                    'products'     => $options[$attribute->id][$optionId],
                    'images'     => $productImage ?? null,
                ];

i simply want to instead of null safe the images if the $productImage does not have value remove it from the array i mean remove images completely is that possible ??

Farshad
  • 1,830
  • 6
  • 38
  • 70

4 Answers4

2

You can use array_filter without any callbacks to remove all falsey values (null, false, 0, '', etc.)

https://www.php.net/manual/en/function.array-filter.php

https://stackoverflow.com/a/2382510/296555

<?php
$attributeOptionsData[] = [
    'id'           => 'someValue',
    'label'        => 'someValue',
    'swatch_value' => 'someValue',
    'products'     => 'someValue',
    'images'       => null,
];

$filteredList = array_filter($attributeOptionsData[0]);

var_dump($filteredList);
// array(4) {
//   ["id"]=> string(9) "someValue"
//   ["label"]=> string(9) "someValue"
//   ["swatch_value"]=> string(9) "someValue"
//   ["products"]=> string(9) "someValue"
// }

This is also a neat function. https://www.php.net/manual/en/function.array-filter.php#111091

If you want a quick way to remove NULL, FALSE and Empty Strings (""), but leave values of 0 (zero), you can use the standard php function strlen as the callback function: eg:

<?php
 
// removes all NULL, FALSE and Empty Strings but leaves 0 (zero)
values $result = array_filter( $array, 'strlen' );

?>
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
1

You can try with array_splice

<?php

if (empty($attributeOptionsData[0]['images'])) {

  array_splice($attributeOptionsData[0], 4);
}

print_r($attributeOptionsData[0]);

/*Result
Array
(
    [id] => $optionId
    [label] => $attributeOption->label ? $attributeOption->label : $attributeOption->admin_name
    [swatch_value] => $attribute->swatch_type ==  ? $attributeOption->swatch_value_url : $attributeOption->swatch_value
    [products] => $options[$attribute->id][$optionId]
)
*/
1

I believe your array will not only contain 1 element, in case it has only 1 element, just remove foreach loop. Below is my solution for this.

    $attributeOptionsData[] = [
        'id'           => 'string',
        'label'        => 'var',
        'swatch_value' => '123',
        'products'     => '',
        'images'       => null,
    ];

    $attributeOptionsData[] = [
        'id'           => 1,
        'label'        => '2',
        'swatch_value' => null,
        'products'     => '',
        'images'       => null,
    ];


    foreach($attributeOptionsData as $key => $value)
    {
        $attributeOptionsData[$key] = array_filter($attributeOptionsData[$key]);
    }

    //$attributeOptionsData now will be your result;

Your result will look like this:

[
  0 => [
    "id" => "string",
    "label" => "var",
    "swatch_value" => "123",
  ],
  1 => [
    "id" => 1,
    "label" => "2",
  ]
]
0

this one will remove images but will not change index number

$vr = count($attributeOptionsData);
for ($i=0; $i < $vr; $i++) { 
  if (empty($attributeOptionsData[i]['images'])) {
    unset($attributeOptionsData[i]['images']);
  }
}
Tugrul Yildirim
  • 329
  • 2
  • 8