Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从数组php获取50条记录_Php_Arrays_Json_Slice - Fatal编程技术网

从数组php获取50条记录

从数组php获取50条记录,php,arrays,json,slice,Php,Arrays,Json,Slice,我有一个json文件,我正在将其转换为数组,然后尝试从数组中获取50个元素 但由于某种原因,我的代码不起作用,它抛出212个元素,而不是50个 请参阅下面我的代码: $json_file = file_get_contents("/directory/jsonfile.json"); $json_data = json_decode($json_file, true); $json_data = array_slice((array)$json_data, 0, 50); print_r (ar

我有一个json文件,我正在将其转换为数组,然后尝试从数组中获取50个元素

但由于某种原因,我的代码不起作用,它抛出212个元素,而不是50个

请参阅下面我的代码:

$json_file = file_get_contents("/directory/jsonfile.json");
$json_data = json_decode($json_file, true);
$json_data = array_slice((array)$json_data, 0, 50);
print_r (array_count_values($json_data)); // this still showing 212 results
count()
返回一个INT,其中
array\u count\u values()
返回一个数组

看起来需要
count()
来获取数组元素的计数


你确定,
array\u count\u value
是你想要的,而不仅仅是
count
?我很难相信有什么东西在没有看到实际输入的情况下向你显示了
212个结果
?它已经是一个数组了。谢谢你的帮助,当我尝试使用count()时,它显示了50个,这很好,但是当我像这样输出整个数组时,“print\r($json\u data)”,它显示了所有212个结果。我只想在slice()之后,它应该显示50条记录,而不是212条?@mak看起来像是,您有一个嵌套数组,第一层的50条记录中的每一条都有大约4-5条。@Philipp所以我的主要目标是将json文件转换为数组,然后从数组中选择最新的50条记录,然后将其转换为xml。下面是我想要实现这一目标的代码:但我总是得到212个结果。