-2

I have one array like below

Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Segment] => Storage
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 100
        [TotalTransfersOut] => 75
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 25
        [ApprovedActivities] => 0
        [AvailableBalance] => 25
        [TotalApprovedClaims] => 0
        [Balance] => 25
        [TotalUnApprovedClaims] => 0
        [Exposure] => 25
    )

I need to remove some elements from this array and formatted to new structure.

I need to change below structure after format my array

Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Segment] => Storage
        [Region] => 
        [SubRegion] => 
        [Country] => 
    )

Is any method available in php for remove some elements in an array using php?

Bibin James
  • 87
  • 1
  • 1
  • 11
  • Just delete the variables using [unset](http://php.net/unset), `unset($arr['Country'])` will remove Country from the array. – Kalkran Feb 17 '16 at 13:02
  • You really ought to create an array of keys you wish to end up with, rather than unsetting each unwanted one. What if more keys are passed in future? – rybo111 Feb 17 '16 at 13:12
  • Would be nice if you could show what you already tried – zachu Feb 17 '16 at 13:13
  • My example: `foreach($keys as $key){ $new_array[$key] = $bad_array[$key]; }` – rybo111 Feb 17 '16 at 13:17

6 Answers6

1

Unset the variables of array using unset()

unset($array[typeofrec]);
unset($array[TotalAllocations]);
unset($array[TotalTransfersOut]);
unset($array[TotalTransfersIn]);
unset($array[ApprovedActivities]);
unset($array[AvailableBalance]);
unset($array[TotalApprovedClaims]);
unset($array[Balance]);
unset($array[TotalUnApprovedClaims]);
unset($array[Exposure]);
Ninju
  • 2,522
  • 2
  • 15
  • 21
0

If you want to remove a particular element from an array use unset().

unset($array[typeofrec]);
unset($array[TotalAllocations]);
....
PriestVallon
  • 1,519
  • 1
  • 22
  • 44
0

Use array_search to get the key and remove it with unset if found:

if (($key = array_search('strawberry', $array)) !== false) {
    unset($array[$key]); // This will remove the element
}

Here is some more ideas PHP: How to remove specific element from an array?

Community
  • 1
  • 1
zachu
  • 671
  • 2
  • 7
  • 19
0

There are many methods which can be used to remove array elements: unset(), array_filter, array_reduce.

One possible way to do it would be:

$myArray = array_filter($myArray, function ($key) {
    return in_array($key, array('AllocationPool', 'Quarter', 'Segment', 'Region', 'SubRegion', 'Country'));
}, ARRAY_FILTER_USE_KEY);
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

try for each key

unset( $arr["key"] )

http://php.net/manual/en/function.unset.php

assuming your array is $arr, do

unset( $arr["TotalAllocations"] ); ...

if you want to keep some consecutive values, you may use slice()

$output = array_slice($input, 0, 6); // keep first 6 values

0

You can have a look at array_filter($array, $callback, $flag) (http://php.net/manual/en/function.array-filter.php). With this function you can filter (remove) elements from the array. If you only provide the array parameter the function will remove all the values equal to FALSE from the array. And if you want, you can provide a callback function to perform a more sophisticated filter, if the callback returns false, then the given item will be filter from the array.

beni0888
  • 1,050
  • 1
  • 12
  • 40