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 ??

2
  • You can use array_filter to remove all FALSE values. Commented Nov 28, 2020 at 18:52
  • Does this answer your question? Remove empty array elements Commented Nov 28, 2020 at 18:52

4 Answers 4

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' );

?>
Sign up to request clarification or add additional context in comments.

Comments

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]
)
*/

2 Comments

OP's code is a multi-dimensional array $attributeOptionsData[] = [...
It's multidimensional but with only index 0, alredy fixed.
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",
  ]
]

Comments

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']);
  }
}

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.