0

I have a multidimensional array that looks like this:

{
  "groups": [
    {
      "__v": 0,
      "_create_date": "2014-08-20T23:00:12.901Z",
      "_id": "53f5287ca78473a969001827",
      "_last_message_date": "2014-08-20T23:04:36.347Z",
      "activity": 0,
      "blocked_users": [],
      "created_by": {
        "_id": "53e84b0eba84943c6d0003f8",
        "_last_modified": "2014-08-20T00:11:05.399Z",
        "first_name": "Jegg",
        "last_name": "V"
      },
      "curated": false,
      "diversity": 0,
      "featured": false,
      "flagged": false,
      "last_message": {
        "text": "let's talk beo",
        "created_by": {
          "_id": "53e84b0eba84943c6d0003f8",
          "first_name": "Jegg",
          "last_name": "V"
        },
        "_id": "53f52984a78473a969001833",
        "_create_date": "2014-08-20T23:04:36.347Z"
      },
      "member_count": 1,
      "messages_count": 1,
      "name": "Test",
      "public": true,
      "recency": 52182276.347,
      "score": 52182276.347,
      "tags": []
    },

This structure repeats over 3000 times creating a very large multidimensional array. I think I can use array_chunk($array, 300) to break the array into smaller chunks. But I can't figure out how to access them exactly.

What I want to do is independently loop through the newly chunked arrays. So I'd like to end up with something like:

$array1 = {...}
$array2 = {...}
$array3 = {...}
$array4 = {...}

... and so on

THen I could loop through each of the newly created arrays, which are essentially smaller groups of the original array, but of 3000 arrays in one multidimensional array as I have in the first place, I end up with these smaller ones of 300 arrays each.

I hope this makes sense, I'm kinda out of my league. Help is always appreciated.

9
  • That's not a PHP array. What part is repeated exactly ? Commented Aug 21, 2014 at 6:12
  • 1
    What is it you're hoping to gain by splitting the array up like that? I have to imagine a structure like this * 3000 plus 300 individual arrays would use a non-inconsequential amount of memory. Commented Aug 21, 2014 at 6:14
  • 1
    you can access the result array like $resultarray[0],$resultarray[1] ..... $resultarray[n] where each $resultarray[] with 300 items Commented Aug 21, 2014 at 6:15
  • Yes, it's a JSON response decoded. I actually am not planning on running this on an active site, it's to be run on schedule to generate an HTML sitemap with the data in the JSON object. But I can't write more than 300 records per page for SEO reasons, so i need to create many different pages. My thought (which might be lame) is to loop through each new array and write out a separate HTML file for each limited to 300 per page. @bansi that might work, how could I get a count of how many chunks were created? Commented Aug 21, 2014 at 6:23
  • use count count($resultarray) Commented Aug 21, 2014 at 6:27

2 Answers 2

1

I think your array is in json format.

First decode it and then pass to array_chunk method.

array_chunk($input_array, 300));

then access them as $input_array[0][0], $input_array[0][1]....... $input_array[0][299], $input_array[1][0], $input_array[1][1].....

Sign up to request clarification or add additional context in comments.

Comments

0

EDIT: oh, somehow I entirely misread the question. array_chunk is something worth looking into.


You could try using extract to fetch array values to the "global" variable namespace.

extract takes three arguments: the array you wish to extract, flags, and prefix if needed.

I'm not sure how non-associative arrays are extracted, but you could try

$full_array = array(
    array( ... ),
    array( ... ),
    array( ... ),
    array( ... ),
    ...
);

// EXTR_PREFIX_ALL prefixes all extracted keys with wanted prefix (the third param).
$extract_amount = extract( $full_array, EXTR_PREFIX_ALL, 'prefix' );

Now you should have the array extracted and available for use with variable names $prefix0, $prefix1, $prefix2 and so on.

I'm not sure how smart it is to extract an array with hundreds of available values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.