0

I need to remove the array index which contains the key 'parentType' value is COMPONENT. I am having 4 indexed array and in that I have 2 index with key [parentType] => SITE So I need only that indexed array. I need to filter only if the [parentType] => SITE then that array I need it. I am having the input array like this:-

$arr = Array
(
    [0] => Array
        (
            [parentType] => SITE
            [name] => PV Meter
        )

    [1] => Array
        (
            [parentType] => COMPONENT
            [name] => Inverter-01
        )

    [3] => Array
        (
            [parentType] => SITE
            [name] => Inverter-02
        )
    [4] => Array
        (
            [parentType] => COMPONENT
            [name] => Inverter-02
        )
);

I have tried with array_diff and unset($array['parentType']) and

 foreach($components_id as $value=>$val)
        {   
            echo "<br> Array[".$value."]=".$val;
        }

but I did not get the exact solution.

I need a output like this:-

$arr = Array
(
    [0] => Array
        (
            [parentType] => SITE
            [name] => PV Meter
        )
    [1] => Array
        (
            [parentType] => SITE
            [name] => Inverter-02
        )
);
1
  • Yes you can filter a 2d array by a condition. This duplicate question should be closed and not answered. I'll wait and see if the community does the right thing. This is a test. Low-hanging fruit is forbidden fruit. Commented Dec 13, 2023 at 9:41

2 Answers 2

1

You can use array_splice for remove element by index but it's not recommend because we need to deal with index changing after remove one element.

I suggest to filter result to new array is better.

My method:

<?php

class MyFilter
{
    public function __construct(
        private array $values
    ) {
    }

    public function __invoke(array $result, array $item): array
    {
        if (in_array($item['parentType'], $this->values)) {
            $result[] = $item;
        }
        return $result;
    }
}

$in = [
    [
        "parentType" => "SITE",
        "name" => "PV Meter"
    ],
    [
        "parentType" => "COMPONENT",
        "name" => "Inverter-01"
    ],
    [
        "parentType" => "SITE",
        "name" => "Inverter-02"
    ],
    [
        "parentType" => "COMPONENT",
        "name" => "Inverter-02"
    ]
];

$out = array_reduce($in, new MyFilter(['SITE']), []);

print_r($out);
Sign up to request clarification or add additional context in comments.
1

You can use the array_filter method to create a new filtered array:

$arr = [
    [
        'parentType' => 'SITE',
        'name' => 'PV Meter'
    ],
    [
        'parentType' => 'COMPONENT',
        'name' => 'Inverter-01'
    ],
    [
        'parentType' => 'SITE',
        'name' => 'Inverter-02'
    ],
    [
        'parentType' => 'COMPONENT',
        'name' => 'Inverter-02'
    ]
];

$newArr = array_filter($arr, function ($item) {
    return isset($item['parentType']) && $item['parentType'] === 'SITE';
});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.