我有一个像这样的大型多维数组
Array
(
[/] => Array
(
[0] => d:Cover & Label:0
[1] => d:INFO TBS:0
[2] => d:VIDEO TS:0
)
[/Cover & Label/] => Array
(
[0] => f:This Is 40 (2012) cover.jpg:197602
[1] => f:This Is 40 (2012) label.jpg:274893
[2] => f:This Is 40 (2012) poster.jpg:50100
)
[/INFO TBS/] => Array
(
[0] => f:This Is 40 (2012) INFORMATIE .txt:1333
[1] => f:Torrent Downloaded from SilverTorrents.me.txt:41
[2] => f:Torrent downloaded from Demonoid.com.txt:47
[3] => f:Torrent_downloaded_from_AhaShare.com.txt:107
[4] => f:Tracked by H33T.com.txt:19
[5] => f:silver torrent logo.jpg:64109
[6] => f:tracked By movie-torrentz.txt:36
)
[/VIDEO TS/] => Array
(
[0] => f:VIDEO_TS.BUP:12288
[1] => f:VIDEO_TS.IFO:12288
[2] => f:VIDEO_TS.VOB:59392
[3] => f:VTS_01_0.BUP:18432
[4] => f:VTS_01_0.IFO:18432
[5] => f:VTS_01_0.VOB:59392
[6] => f:VTS_01_1.VOB:39620608
[7] => f:VTS_02_0.BUP:88064
[8] => f:VTS_02_0.IFO:88064
[9] => f:VTS_02_0.VOB:59392
[10] => f:VTS_02_1.VOB:1073709056
[11] => f:VTS_02_2.VOB:1073709056
[12] => f:VTS_02_3.VOB:1073709056
[13] => f:VTS_02_4.VOB:1073709056
[14] => f:VTS_02_5.VOB:295792640
)
)
如何创建像这样的较小尺寸的数组。
Array
(
[/] => Array
(
[0] => d:Cover & Label:0
[1] => d:INFO TBS:0
[2] => d:VIDEO TS:0
)
[/Cover & Label/] => Array
(
[0] => f:This Is 40 (2012) cover.jpg:197602
[1] => f:This Is 40 (2012) label.jpg:274893
[2] => f:This Is 40 (2012) poster.jpg:50100
)
)
我想创建一个较小的数组,其中包含json_encoded($ array)< = 255
的strlen到目前为止,我已设法做到这一点,
<?php
$small_array_new = array();
foreach($DATA as $key => $mini_array){
foreach($mini_array as $mini_array_element){
if(strlen(json_encode($small_array_new)) <= 255){
$small_array_new[$key][] = $mini_array_element;
}
if(strlen(json_encode($small_array_new)) > 255){
array_pop($small_array_new[$key] );
break;
}
}
}
但是,在strlen达到255并且没有完全突破并给我这样的输出后,这并没有停止。
Array
(
[/] => Array
(
[0] => d:Cover & Label:0
[1] => d:INFO TBS:0
[2] => d:VIDEO TS:0
)
[/Cover & Label/] => Array
(
[0] => f:This Is 40 (2012) cover.jpg:197602
[1] => f:This Is 40 (2012) label.jpg:274893
[2] => f:This Is 40 (2012) poster.jpg:50100
)
[/INFO TBS/] => Array
(
)
[/VIDEO TS/] => Array
(
[0] => f:VIDEO_TS.BUP:12288
)
)
如果strlen(json_encode($$ small_array_new))&gt; 255?
更新:使用中断2后;
Array
(
[/] => Array
(
[0] => d:Cover & Label:0
[1] => d:INFO TBS:0
[2] => d:VIDEO TS:0
)
[/Cover & Label/] => Array
(
[0] => f:This Is 40 (2012) cover.jpg:197602
[1] => f:This Is 40 (2012) label.jpg:274893
[2] => f:This Is 40 (2012) poster.jpg:50100
)
[/INFO TBS/] => Array
(
)
)
答案 0 :(得分:1)
您可以将要停止的循环次数传递给break
语句。
您想要中断两个foreach
循环,因此请使用break 2;
。
修改强> 我明白了......当它排空时你必须删除它。你可以使用例如
if(strlen(json_encode($small_array_new)) > 255){
array_pop($small_array_new[$key] );
if (count($small_array_new[$key]) == 0) unset($small_array_new[$key]);
break 2;
}
答案 1 :(得分:1)
我推荐你来自Athari(或其他LINQ库)的YaLinqo库。它使用一行代码完成此类任务。
答案 2 :(得分:1)
我找到了一个用于n维数组的小函数
function junkArray2Json($data, $len = 255, &$scope = null, &$root = null)
{
if (!is_array($data)) {
$scope = $data;
return $scope;
}
if ($isInitialCall = is_null($root)) {
$root = array();
$scope = &$root;
} else {
$scope = array();
}
foreach ($data as $key => $value) {
$rootCopy = unserialize(serialize($root));
$scopeCopy = unserialize(serialize($scope));
junkArray2Json($value, $len, $scope[$key], $root);
if (empty($scope[$key])) {
unset($scope[$key]);
}
if (strlen(json_encode($root)) > $len) {
$root = $rootCopy;
$scope = $scopeCopy;
}
}
return $scope;
}
print_r(junkArray2Json($DATA, 255));
打印
Array
(
[/] => Array
(
[0] => d:Cover & Label:0
[1] => d:INFO TBS:0
[2] => d:VIDEO TS:0
)
[/Cover & Label/] => Array
(
[0] => f:This Is 40 (2012) cover.jpg:197602
[1] => f:This Is 40 (2012) label.jpg:274893
[2] => f:This Is 40 (2012) poster.jpg:50100
)
[/VIDEO TS/] => Array
(
[0] => f:VIDEO_TS.BUP:12288
)
)