Php JSON数组从几个数组开始

Php JSON数组从几个数组开始,php,json,Php,Json,我试图从一个站点创建JSON提要,我想在另一个站点上解码。问题是似乎有很多数组[0],所以很难循环遍历它并计算有多少个对象 如何在不获取所有这些数组的情况下进行编码和解码,以便更容易地计算对象的数量并在其中循环 目前,我将其编码如下: $data = array(); foreach ($posts as $post) { $r = str_replace("\n",'', shorten_txt($post->post_content, 500)); $n = str_r

我试图从一个站点创建JSON提要,我想在另一个站点上解码。问题是似乎有很多数组[0],所以很难循环遍历它并计算有多少个对象

如何在不获取所有这些数组的情况下进行编码和解码,以便更容易地计算对象的数量并在其中循环

目前,我将其编码如下:

$data = array();
foreach ($posts as $post) {
    $r = str_replace("\n",'', shorten_txt($post->post_content, 500));
    $n = str_replace("\r", '', $r);
    $post_data = array(
    'title' => get_the_title($post->ID),
    'link' => get_permalink($post->ID),
    'image' => catch_that_image(),
    'content' => $n,
    'time' => get_the_date( $d)." ". get_the_time( $d));
     $data[] = (array('item' => $post_data));

}
echo json_encode($data);
这将提供以下输出:

[
    {
        item: {
            title: "Hello world!",
            link: "http://URL/wordpress/?p=1",
            image: "http://URL/wordpress/wp-content/uploads/2014/04/Digital-Board-2.png",
            content: "Welcome to WordPress. This is your first post. Edit or delete it,             then start blogging!",
            time: "April 17, 2014 5:32 pm"
        }
    }
]
当我解码时,我得到:

Array ( [0] => Array ( [item] => Array( [title] => Hello world! [link] => http://URL/wordpress/?p=1 [image] => http://URL/wordpress/wp-content/uploads/2014/04/Digital-Board-2.png [content] => Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! Jeg elsker kage [time] => April 17, 2014 5:32 pm ) ) )
解码代码:

$json_string = 'http://95.85.11.40/wordpress/?page_id=20';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
print_r($obj);

如果不需要这些
数组[0]
位,则不要创建2D数组:

$data[] = (array('item' => $post_data));
那么应该是:

$data[] = $post_data;
您当前的语句读作_addto array
$data
一个数组,带有一个键:“item”,而我的版本只是说:add to
$data
添加
$post\u data
的值

对已解码数据进行解码:

$data = json_decode(file_get_contents($jsonFile), true);
foreach ($data as $idx => $item)
{
    echo 'This is item number ', $idx +1, PHP_EOL;
    print_r($item);
}
echo json_encode($data[0])好吗?