1

I have an associative array:

$comboUserPosts = array();
array(
     'link'=> get_permalink(),
     'dates'=> $value,
     'title'=> get_the_title(),
     'id'=> $post->ID
)

I need to output all values of id like: "232,555,797"

I know I can do:

foreach ($comboUserPosts as $value) { 
    $value['id']
}

But I need to output that list of ids in the following, and obviously I cannot insert that within the foreach or it'll duplicate.

echo do_shortcode('[ajax_load_more pause="true" post_type="post" post__in="'.json_encode($comboUserPosts['id']).'" progress_bar="true" progress_bar_color="FF0080"]');

As you can see I tried using json_encode($comboUserPosts['id']) but it is not accessing that object

rob.m
  • 9,843
  • 19
  • 73
  • 162
  • read about implode function – JYoThI Jun 08 '18 at 06:03
  • @JYoThI I already tried to use `echo implode(",",$comboUserPosts['id']);` but where do I put that? if I put it within the do_short.. it won't work, as it cannot access the array and it says `Warning: implode(): Invalid arguments passed in` – rob.m Jun 08 '18 at 06:05
  • 1
    How about `echo implode(",",array_column($comboUserPosts,'id'));`? – Eddie Jun 08 '18 at 06:12
  • 1
    @Eddie thanks a lot, it works. Put that into an answer and I'll accept it. BTW this is not the first time you resolved my issue with `array_column`, you must like it a lot :) I have to read in regards. Thanks again – rob.m Jun 08 '18 at 06:14
  • lol. Kinda used it alot lately. It is very useful :) – Eddie Jun 08 '18 at 06:20

1 Answers1

2

You can use array_column to get the values from a single column from the input array

$comboUserPosts = array(
    array(
     'link'=> '',
     'dates'=> '',
     'title'=> '',
     'id'=> '1'
    ),
    array(
     'link'=> '',
     'dates'=> '',
     'title'=> '',
     'id'=> '2'
    )
);

echo implode( "," , array_column( $comboUserPosts , 'id' ) );

This will result to:

1,2 

Doc: array_column()

Eddie
  • 26,593
  • 6
  • 36
  • 58