Robert_Rmz
Robert_Rmz

Reputation: 47

How to remove an element from an array using its key

I have an array thas has other arrays inside and I wanro to delete the elements that have the key "PTemp_C_Avg"

try doing it by creating a function that go through the original array and eliminate the key that is assigned (PTemp_C_Avg, in this case). However, this dosen´t remove the specific element.

//arrProvAvg array
Array
(
[0] => Array
    (
        [PTemp_C_Avg] => 17.28
        [T0_10cm_Avg] => 22.58
        [T1_1m_Avg] => 27.91
        [T2_2m_Avg] => 31.95
        [T3_3m_Avg] => 36.32
        [T4_4m_Avg] => 41.73
        [T5_5m_Avg] => 45.78
        [T6_6m_Avg] => 48.55
        [T7_7m_Avg] => 53.48
        [T7_5_7_5m_Avg] => 47.82
    )

[1] => Array
    (
        [PTemp_C_Avg] => 14.2
        [T0_10cm_Avg] => 20.94
        [T1_1m_Avg] => 27.36
        [T2_2m_Avg] => 32.12
        [T3_3m_Avg] => 36.33
        [T4_4m_Avg] => 41.4
        [T5_5m_Avg] => 46.58
        [T6_6m_Avg] => 48.8
        [T7_7m_Avg] => 52.69
        [T7_5_7_5m_Avg] => 48.9
    )

[2] => Array
    (
        [PTemp_C_Avg] => 11.83
        [T0_10cm_Avg] => 20.23
        [T1_1m_Avg] => 26.9
        [T2_2m_Avg] => 32.39
        [T3_3m_Avg] => 36.95
        [T4_4m_Avg] => 41.48
        [T5_5m_Avg] => 46.41
        [T6_6m_Avg] => 48.82
        [T7_7m_Avg] => 52.58
        [T7_5_7_5m_Avg] => 49.42
    )

)


function eliminaClave($arrOriginal, $key){
    foreach($arrOriginal as $clave => $valor){
        foreach($valor as $c => $v){
            unset($v[$key]);
        }
    }
    return $arrOriginal;
}

//Call the eliminaClave function
$arrPromAvg = eliminaClave($arrPromAvg, "PTemp_C_Avg");

This is output that I expet

 Array
(
[0] => Array
    (
        [T0_10cm_Avg] => 22.58
        [T1_1m_Avg] => 27.91
        [T2_2m_Avg] => 31.95
        [T3_3m_Avg] => 36.32
        [T4_4m_Avg] => 41.73
        [T5_5m_Avg] => 45.78
        [T6_6m_Avg] => 48.55
        [T7_7m_Avg] => 53.48
        [T7_5_7_5m_Avg] => 47.82
    )

[1] => Array
    (
        [T0_10cm_Avg] => 20.94
        [T1_1m_Avg] => 27.36
        [T2_2m_Avg] => 32.12
        [T3_3m_Avg] => 36.33
        [T4_4m_Avg] => 41.4
        [T5_5m_Avg] => 46.58
        [T6_6m_Avg] => 48.8
        [T7_7m_Avg] => 52.69
        [T7_5_7_5m_Avg] => 48.9
    )

[2] => Array
    (
        [T0_10cm_Avg] => 20.23
        [T1_1m_Avg] => 26.9
        [T2_2m_Avg] => 32.39
        [T3_3m_Avg] => 36.95
        [T4_4m_Avg] => 41.48
        [T5_5m_Avg] => 46.41
        [T6_6m_Avg] => 48.82
        [T7_7m_Avg] => 52.58
        [T7_5_7_5m_Avg] => 49.42
    )

)

The element with the "PTemp_C_Avg" has been removed

Upvotes: 2

Views: 60

Answers (3)

Rahul
Rahul

Reputation: 18557

Here is without loop,

function eliminaClave($arr, $key)
{
    return array_map(function ($item) use ($key) {
        unset($item[$key]); // unsetting elements
        return $item; // saving back changed item
    }, $arr);
}

Demo.

Upvotes: 1

user10051234
user10051234

Reputation:

you don't need the 2nd foreach..

<?php

$a[0]['PTemp_C_Avg']='1';
$a[0]['foo']='1';

$a[1]['PTemp_C_Avg']='2';
$a[1]['foo']='1';


foreach($a as $k=> $b){

    unset($a[$k]['PTemp_C_Avg']);
}

print_r($a);

output:

Array (
    [0] => Array
        (
            [foo] => 1
        )

    [1] => Array
        (
            [foo] => 1
        )

)

Upvotes: 0

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

How about with a single foreach() and unset()?

function eliminaClave($arrOriginal, $key){
    foreach($arrOriginal as $clave => $valor){
        unset($valor['PTemp_C_Avg']);
        $arrOriginal[$clave] = $valor;  
    }
    return $arrOriginal;
}

Upvotes: 2

Related Questions