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.
这种结构重复超过3000次,创建了一个非常大的多维数组。我想我可以使用array_chunk($ array,300)将数组分成更小的块。但我无法弄清楚如何准确地访问它们。
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.
然后,我可以循环遍历每个新创建的数组,这些数组本质上是较小的原始数组,但是在一个多维数组中有3000个数组,就像我在第一个地方一样,我最终得到了每个300个数组的较小数组。
I hope this makes sense, I'm kinda out of my league. Help is always appreciated.
我希望这是有道理的,我有点不在我的联盟中。总是感谢帮助。
2 个解决方案
#1
1
I think your array is in json format.
我认为你的数组是json格式。
First decode it and then pass to array_chunk method.
首先解码它,然后传递给array_chunk方法。
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]
.....
然后将它们作为$ input_array [0] [0],$ input_array [0] [1] ....... $ input_array [0] [299],$ input_array [1] [0],$ input_array [1]访问它们] [1] .....
#2
0
EDIT: oh, somehow I entirely misread the question. array_chunk
is something worth looking into.
编辑:哦,不知怎的,我完全误解了这个问题。 array_chunk是值得研究的东西。
You could try using extract
to fetch array values to the "global" variable namespace.
您可以尝试使用extract将数组值提取到“全局”变量名称空间。
extract
takes three arguments: the array you wish to extract, flags, and prefix if needed.
extract有三个参数:您想要提取的数组,标志和前缀(如果需要)。
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.
现在你应该提取数组,并且可以使用变量名$ prefix0,$ prefix1,$ prefix2等。
I'm not sure how smart it is to extract an array with hundreds of available values.
我不确定提取具有数百个可用值的数组有多聪明。
#1
1
I think your array is in json format.
我认为你的数组是json格式。
First decode it and then pass to array_chunk method.
首先解码它,然后传递给array_chunk方法。
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]
.....
然后将它们作为$ input_array [0] [0],$ input_array [0] [1] ....... $ input_array [0] [299],$ input_array [1] [0],$ input_array [1]访问它们] [1] .....
#2
0
EDIT: oh, somehow I entirely misread the question. array_chunk
is something worth looking into.
编辑:哦,不知怎的,我完全误解了这个问题。 array_chunk是值得研究的东西。
You could try using extract
to fetch array values to the "global" variable namespace.
您可以尝试使用extract将数组值提取到“全局”变量名称空间。
extract
takes three arguments: the array you wish to extract, flags, and prefix if needed.
extract有三个参数:您想要提取的数组,标志和前缀(如果需要)。
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.
现在你应该提取数组,并且可以使用变量名$ prefix0,$ prefix1,$ prefix2等。
I'm not sure how smart it is to extract an array with hundreds of available values.
我不确定提取具有数百个可用值的数组有多聪明。