0

I have a long multidimensional array with timestamps as keys which contain arrays of values. Something like this but with more timestamps:

array(3) {
  [1502609400]=>
  array(2) {
    ["Item1"]=>
    array(1) {
      ["PRICE"]=>
      float(100)
    }
    ["Item2"]=>
    array(1) {
      ["PRICE"]=>
      float(50)
    }
  }
  [1502611200]=>
  array(2) {
    ["Item1"]=>
    array(1) {
      ["PRICE"]=>
      float(200)
    }
    ["Item2"]=>
    array(1) {
      ["PRICE"]=>
      float(150)
    }
  }
  [1502613000]=>
  array(2) {
    ["Item1"]=>
    array(1) {
      ["PRICE"]=>
      float(500)
    }
    ["Item2"]=>
    array(1) {
      ["PRICE"]=>
      float(250)
    }
  }

How can I remove every every second array of the array without losing the keys as timestamp? So I end up with this:

array(3) {
  [1502609400]=>
  array(2) {
    ["Item1"]=>
    array(1) {
      ["PRICE"]=>
      float(100)
    }
    ["Item2"]=>
    array(1) {
      ["PRICE"]=>
      float(50)
    }
  }
  [1502613000]=>
  array(2) {
    ["Item1"]=>
    array(1) {
      ["PRICE"]=>
      float(500)
    }
    ["Item2"]=>
    array(1) {
      ["PRICE"]=>
      float(250)
    }
  }

If I use a for loop and unset every second key I lose all the keys and end up with 0, 1, 2 etc. instead of the timestamp.

1
  • In a loop, just check for index, and unset every second element: Commented Aug 14, 2017 at 9:15

3 Answers 3

4

In a loop, check for index and unset every second element:

You can use custom $index variable, like this:

$index = 0; // points to first element

foreach ($array as $key => $value) {
    if ($index % 2 != 0) { //check for un-even
        unset($array[$key]);
    }   

    $index++; // move pointer to next element
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick reply, but $key would be 1502611200 for example. So this wont work, since it's divisible by 2.
Added custom $index variable to maintain it separately, only purpose to find-out the even/odd elements.
Just wanted to say that too, as I combined your answer and @Osama 's :) Thanks @M Idrees this works great! Marked as correct.
1
$i=1 // where the key to remove 
$x=0; //loop to determine the key position
foreach ($array as $key=>$value){
if($x==$i){
unset($array[$key]);
}
$x++;
}

4 Comments

Thanks a lot! This works perfectly. Didn't think about adding a counter which makes a lot of sense. * Edit: Sorry, this does not work with a longer array. Did I misunderstand something?
Why iterate over the entire array when all you want is modify the first element?
Depending on the same idea you can achieve that note the every second item must be 1,3,5,6 and the counter must be less than array count if you need help I can edit code or if you can do it by yourself
Thanks for the help, the count brought me up to the now correct answer by @M-Idrees.
1

In that kind of case, a simple foreach can be more efficient perfwise than a standard function :

foreach ($array as $key => $val) {
    if (array_key_exists('Item2', $val)) {
        unset($val['Item2']);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.