Good day, tell me, please:

print_r ($b['response']['wall'][0]['text']); print_r ($b['response']['profiles'][0]['first_name']); 

In the array ['response'] there are two subarrays ['wall'] and ['profiles'], in [' wall '] the text contains the [' text '] post that the author posted and the [' profiles'] contains the name of the posted [' first_name '];

It turns out to sort out and get only all the posts

 foreach($b['response']['wall'] as $value) : $text = strip_tags ($value['text']);?> <textarea rows="17" cols="47" name="text" ><? echo $text; ?></textarea> <textarea rows="17" cols="47" name="autor" ><? echo $autor; ?></textarea> <?endforeach;?> 

Tell me how to synchronously get posts and the name of the author in the $ autor variable?

  • I recommend changing the structure of the query for retrieving data. One row of the resulting sample should contain all the data for a particular post. It seems that the problem is in the structure of the database and initially the data when saved is distributed as xs. - Kirill Korushkin
  • I work with the Vkontakte API, unfortunately I don’t know how to change the request structure. - Dmitry

3 answers 3

The data obtained is crooked, respectively, the solution is the same curve. But it should work =):

 // предполагаем, что кол-во элементов 'wall' = кол-ву элементов 'profiles' $count = count($b['response']['wall']); for($i = 0; $i < $count; $i++) { ?> <textarea rows="17" cols="47" name="text" ><?=$b['response']['wall'][$i]['text'];?></textarea> <textarea rows="17" cols="47" name="text" ><?=$b['response']['profiles'][$i]['first_name'];?></textarea> <?php } ?> 
  • I understand correctly, should I use foreach instead of loop? - Dmitry
  • in this case, yes. - Kirill Korushkin
  • Removed foreach put your code writes "Page is not available" - Dmitry
  • You copied it directly? notice the opening and closing php tags. and I have a typo there in the condition, now corrected. - Kirill Korushkin
  • Yes, all tags learned. - Dmitry

If I understood correctly, then something like this:

  foreach($b['response'] as $item) { foreach ($item['wall'] as $post) { echo $text; echo $item['profiles'][0]['first_name']; } } 

If each post may have a different author, then you need to build a new array in a loop and then output again in a loop.

    As far as I understand, the number of elements in the subarrays of wall and profiles is the same. That is, one post corresponds to one author.

    Therefore, the cycle turns into this:

     foreach($b['response']['wall'] as $key => $value) : // добавим $key $text = strip_tags ($value['text']);; $author = $b['response']['profiles'][$key]['first_name']; // автор с тем же $key endforeach;