PHP/Wordpress: how to amend foreach() to output my array in a specific way -


major edit: reframing question might easier solve...

i trying generate json microdata using php code in wordpress. i'm using foreach() method cycle through list of posts on page, put thumbnail, title , link data array, , i'll later encode array json microdata. however, array i've assembled using foreach() doesn't output data how want.

i've spent hours trying section of data output correctly, no avail.

--

what want achieve (using print_r() view , test php code) - e.g. instead of index numbers [0]. [1] etc., want each array output [associatedmedia] instead below:

array (     [associatedmedia] => array         (             [image] => http://www.website.com/thumbnail.jpg             [name] => post title             [url] => http://www.website.com/the-post         )     [associatedmedia] => array         (             [image] => http://www.website.com/second-thumbnail.jpg             [name] => second post title             [url] => http://www.website.com/the-second-post         )      // , on... ) 

my current result:

array (     [0] => array         (             [image] => http://www.website.com/first-thumbnail.jpg             [name] => first post title             [url] => http://www.website.com/the-post-one         )      [1] => array         (             [image] => http://www.website.com/second-thumbnail.jpg             [name] => second post title             [url] => http://www.website.com/the-post-two         )      [2] => array         (             [image] => http://www.website.com/third-thumbnail.jpg             [name] => third post title             [url] => http://www.website.com/the-post-three         ) // , on... ) 

my foreach method:

// other php  code  global $post; global $wp_query;  $category = $wp_query->get_queried_object(); $args = array( 'category' => $category->cat_id ); $posts = get_posts( $args );  $post_details = array(); $i = 0;  foreach( $posts $post ) {         setup_postdata($post);        $thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id(),'thumbnail' );     $post_thumbnails['image'] = $thumb_url[0];     $post_titles['name'] = get_the_title();     $post_links['url'] = get_permalink();      $post_details[$i]['image'] = $post_thumbnails['image'];     $post_details[$i]['name'] = $post_titles['name'];     $post_details[$i]['url'] = $post_links['url'];     $i++; };  wp_reset_postdata();   print_r($post_details);  

i'm beginning more advanced programming, , i'm sure code above crude. or tips on how can shorten appreciated.

edit: more $post related code added

use this

$array_details['associatedmedia'][$i] = $post_details[$i]; 

instead of

$array_details['associatedmedia'] = $post_details[$i]; 

update :

keys of array elements should unique. use of below format desired results.

foreach( $posts $post ) {     ....     $array_details['associatedmedia'][$i] = $post_details[$i];     i++; } 

results :

array (    ['associatedmedia'] => array(           [0] => array(                 [image] => http://www.website.com/first-thumbnail.jpg                 [name] => first post title                 [url] => http://www.website.com/the-post-one         )         [1] => array(                 [image] => http://www.website.com/second-thumbnail.jpg                 [name] => second post title                 [url] => http://www.website.com/the-post-two         )         [2] => array(                 [image] => http://www.website.com/third-thumbnail.jpg                 [name] => third post title                 [url] => http://www.website.com/the-post-three         ) // , on...   ) ) 

or

foreach( $posts $post ) {     ....     $array_details[$i]['associatedmedia']= $post_details[$i];     i++; } 

results :

array (      [0] => array(         ['associatedmedia'] => array(                  [image] => http://www.website.com/first-thumbnail.jpg                 [name] => first post title                 [url] => http://www.website.com/the-post-one             )     )    [1] => array(        ['associatedmedia'] => array(                  [image] => http://www.website.com/second-thumbnail.jpg                 [name] => second post title                 [url] => http://www.website.com/the-post-two         )     )   [2] => array(       ['associatedmedia'] => array(                  [image] => http://www.website.com/third-thumbnail.jpg                 [name] => third post title                 [url] => http://www.website.com/the-post-three         )     ) // , on...  ) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -