-1

I have an array:

array:1 [
  "arrays" => array:2 [
    "hihi" => array:4 [
      "one" => "14000"
      "two" => "15000"
      "three" => "16000"
    ]
    "huhu" => array:4 [
      "one" => "69997"
      "two" => "72000"
      "three" => "77000"
    ]
  ]
]

I want to remove item two from the given array:

Output

array:1 [
  "arrays" => array:2 [
    "hihi" => array:4 [
      "one" => "14000"
      "three" => "16000"
    ]
    "huhu" => array:4 [
      "one" => "69997"
      "three" => "77000"
    ]
  ]
]

I tried with array_pluck but had a problem with hihi and huhu. Hoping to find a solution without the use of a loop.

Note: hihi and huhu are both dynamic arrays.

Ivan
  • 34,531
  • 8
  • 55
  • 100
TranDuc
  • 23
  • 1
  • 10

3 Answers3

2

If you’re using Laravel, then it has a helper method, array_forget():

array_forget($array, 'arrays.hihi.two');

array_forget($array, 'arrays.huhu.two');
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
1

Use array_map.

$newArray = array_map(function($v) {
    unset($v["two"]);
    return $v;
}, $array["arrays"]);

https://3v4l.org/AeXrO

HTMHell
  • 5,761
  • 5
  • 37
  • 79
  • I want to find no way to use loops – TranDuc Jul 10 '18 at 14:18
  • @TrầnNgọcĐức If your data is dynamic you can't achieve this without iterating. Maybe elaborate on why you need it without iterations so we can help you. – HTMHell Jul 10 '18 at 14:22
1
$array = array(
    'arrays' => array(
        'hihi' => array(
            'one' => '14000',
            'two' => '15000',
            'three' => '16000'
        ),
        'huhu' => array(
            'one' => '69997',
            'two' => '72000',
            'three' => '77000'
        ),
    )
);

You can use array_map function to remove the item two from the array, and by using closure use ($key) you can also perform some actions on the element of an array. Read array_map in detail

$key = 'two';
$result = array(
    'arrays' => array_map(
        function($item) use ($key) {
            unset($item[$key]);
            return $item;
        }, 
        $array["arrays"]
    )
);

print_r($result);

Output

[arrays] => Array
(
    [hihi] => Array
    (
        [one] => 14000
        [three] => 16000
    )
    [huhu] => Array
    (
        [one] => 69997
        [three] => 77000
    )
)

Updated Answer

// First array key of the given array which is "arrays"
$getParentKey = array_keys($array); 

Output:
Array
(
    [0] => arrays
)

// Then, get all values inside "arrays" array which is given below
$getValues = $array[$getParentKey[0]];

Output:
Array
(
    [hihi] => Array
    (
        [one] => 14000
        [two] => 15000
        [three] => 16000
    )
    [huhu] => Array
    (
        [one] => 69997
        [two] => 72000
        [three] => 77000
    )
)

// At last, get "hihi" and "huhu" keys
$getInnerKeys = array_keys($getValues);

Output:
Array
(
    [0] => hihi
    [1] => huhu
)

// And finally, just unset the items from the array
// "$getInnerKeys[0]" means "hihi" and "$getInnerKeys[1]" means "huhu"
unset($array[$getParentKey[0]][$getInnerKeys[0]]['two']);
unset($array[$getParentKey[0]][$getInnerKeys[1]]['two']);

Complete Code

$getParentKey = array_keys($array);
$getValues = $array[$getParentKey[0]];
$getInnerKeys = array_keys($getValues);

unset($array[$getParentKey[0]][$getInnerKeys[0]]['two']);
unset($array[$getParentKey[0]][$getInnerKeys[1]]['two']);


print_r($array);

Output

[arrays] => Array
(
    [hihi] => Array
    (
        [one] => 14000
        [three] => 16000
    )
    [huhu] => Array
    (
        [one] => 69997
        [three] => 77000
    )

)

Without using foreach loop, you can use array_keys to remove item from the array.

Another Option

You can also use recursive function, and the recursive function is a function that calls itself, and the function calling will stop on the condition. Read PHP Recursive Functions more in detail.

In below code example, the $count and $array are passing to the function once then function is calling itself and the ending point is based on the condition.

$count = 0;
$array = array(
    'arrays' => array(
        'hihi' => array(
            'one' => '14000',
            'two' => '15000',
            'three' => '16000'
        ),
        'huhu' => array(
            'one' => '69997',
            'two' => '72000',
            'three' => '77000'
        ),
    )
);

incrementCount($count, $array);

function incrementCount($count, $array) {
    $getParentKey = array_keys($array);
    $getValues = $array[$getParentKey[0]];
    $getInnerKeys = array_keys($getValues);
    $countInnerKeys = count($getInnerKeys);

    if ($count < $countInnerKeys) {
        unset($array[$getParentKey[0]][$getInnerKeys[$count]]['two']);

        incrementCount($count + 1, $array);
    }
    else {
        print_r($array);
        return;
    }
}

Calling the function and unsetting two from the array until $countInnerKeys not reached (means $count reached to huhu key).

Adeel
  • 2,901
  • 7
  • 24
  • 34
  • 1
    thank you, but i want to find solution without using loop: D, maybe this is not feasible – TranDuc Jul 10 '18 at 14:28
  • @Trần Ngọc Đức, please check the updated answer and what do you think still not feasible :D – Adeel Jul 10 '18 at 14:57
  • btw, I'm not sure that hihi and huhu are dynamic keys so that's why I unset "two" from both of them, one by one as you can see above! – Adeel Jul 10 '18 at 15:00
  • @TranDuc if last solution makes sense then it's great otherwise you need to iterate, but what's the purpose of not using loops? Could you please explain? – Adeel Jul 11 '18 at 12:59