katie hudson
katie hudson

Reputation: 2893

Keeping only first 10 records in a specific subarray

I have some JSON which has the following format:

{
  "mainKey": [
    {
      "ID": "1018",
      "dish": "Fish",
      "desert": "Ice cream",
      "drink": "Cola"
    },
    {
      "ID": "1019",
      "dish": "Pizza",
      "desert": "Cake",
      "drink": "Water"
    },
    ...
  ],
  "anotherKey": [
    {
      "something": "something",
      "something": 123,
      "something": 123
    },
    ...
  ],
  ...
}

There are lots of keys and lots of data, I have shortened it to show the basic structure. This JSON is in a variable called $response. I am first converting this into an array:

$response = json_decode($response, true);

There are a couple of keys I needed to unset, so I simply loop the array an unset them:

foreach ($response as $key => $value) {
    if($key === 'mainKey') {

    }
    unset($response['another_key']);
    unset($response['yet_another_key']);
}

I am not unsetting all keys, just a couple. What I am trying to do now is work on the mainKey, which is why I have included an if statement in the loop.

How can I keep just the first 10 records from mainKey? I have seen things like splice, but will this preserve the other keys in my array? Will this also preserve my indexes as these are important to me?

What would be the most efficient way to do this considering that mainKey has over 100k records?

Upvotes: 1

Views: 70

Answers (2)

mickmackusa
mickmackusa

Reputation: 47904

No loop is necessary; functions can do this job in a very clean and concise way.

Code: (Demo)

$black_keys = array_flip(['another_key', 'yet_another_key']);    // blacklist

$array = array_diff_key(json_decode($json, true), $black_keys);  // decode and filter
$array['mainKey'] = array_slice($array['mainKey'], 0, 10);       // truncate mainKey subarray

var_export($array);

Alternatively, this will be slightly better performing:

$array = json_decode($json, true);
unset($array['another_key'], $array['yet_another_key']);
$array['mainKey'] = array_slice($array['mainKey'], 0, 10);

Upvotes: 2

waterloomatt
waterloomatt

Reputation: 3742

Since mainKey will have numerically based keys, you can create a filter that will remove all items that fall outside of a given range.

<?php

$json = '{
  "mainKey": [
    {
      "ID": "1018",
      "dish": "Fish",
      "desert": "Ice cream",
      "drink": "Cola"
    },
    {
      "ID": "1019",
      "dish": "Pizza",
      "desert": "Cake",
      "drink": "Water"
    }
  ],
  "anotherKey": [
    {
      "ID": "something",
      "dish": "123",
      "desert": "123"
    }
  ]
}';

$response = json_decode($json, true);

// If needed, unset your keys 
unset($response['anotherKey']);    
...

// Now, let's work only with `mainKey`
$mainKey = $response['mainKey'];

// Create a range of keys to keep
$allowed  = range(0, 10);

// Finally, filter out all keys not in the range you specified.
$filtered = array_filter(
    $mainKey,
    function ($key) use ($allowed) {
        return in_array($key, $allowed);
    },
    ARRAY_FILTER_USE_KEY
);

// `$filtered` now only has keys 0-10. 
var_dump($filtered);

Upvotes: 0

Related Questions