php create json multiple arrays. Following code with php i need output like that json .. so many array and object confusing me. we using wordpres
<?php
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
));
foreach ( $categories as $category ) {
$category_id = $category->term_id;
$category_name = $category->name;
echo $category_name;
echo "<br>";
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category' => $category_id
);
$myposts = get_posts( $args );
foreach( $myposts as $post ){
$category_postname = $post->post_title;
echo $category_postname;
echo "<br>";
}
}
?>
Php output is"
Testcat1
pos1
post2
Testcat2
post3
post4
TestCat3
post5
post 6
I need create Json like this:
{
"data": [
{
"cat": "Testcat1",
"post": [
{
"name": "post1"
},
{
"name": "post2"
}
]
},
{
"cat": "Testcat2",
"post": [
{
"name": "post3"
},
{
"name": "post4"
}
]
},
{
"cat": "Testcat3",
"post": [
{
"name": "post5"
},
{
"name": "post6"
}
]
}
]
}
i need like this json output.