skip to Main Content

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

2

Answers


  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);
    
    Login or Signup to reply.
  2. 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';
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search