Answer the question
In order to leave comments, you need to log in
Find the array element of the array where the key is online and enter its value in order?
I have a VK bot. It's for conversation. Here you need to enter a list of online users. I now got to this stage: Using the messages.getConversationMembers method, I get a list of users in the conversation. It returns JSON as an array. Etc. Here's what happened:
if (preg_match("/\AОнлайн\Z/ui", $message)){
$online_info = json_decode(file_get_contents("https://api.vk.com/method/messages.getConversationMembers?peer_id=2000000001&fields=online&access_token={$vk->token}&v=5.101"));
$online_users = $online_info->response->profiles;
for ($i=0; $i < count($online_users); $i++) {
$answer = $online_users[$i]->online;
}
}
Answer the question
In order to leave comments, you need to log in
If you want to get an associative array from a json string, use jsone_decode($json, true)
. Then you just debug with wardumps and work with your array.
https://www.php.net/manual/ru/function.uasort.php - just sort the array. if you need to leave only users online, then https://www.php.net/manual/ru/function.array-filter.php
if relevant, then I will leave the code that I needed to assign admins through the command, or rather part of the code.
perhaps this will help you.
UPD: if anything, comments added
$json = file_get_contents("https://api.vk.com/method/messages.getConversationMembers?v=5.101&peer_id={$peer_id}&group_id={$group_id}&access_token={$VK_KEY}"); //посылаем messages.getConversationMembers.
$A = json_decode($json,true); //декодируем в массив. можно и просто, но мне так удобнее.
if($A['error']){ //если же произошла ошибка, оповещаем об этом и останавливаем код
$vk->sendMessage($peer_id,"Произошла ошибка {$A['error']['error_code']} - {$A['error']['error_msg']}.\nПопробуйте предоставить мне доступ к переписке или роль Администратора."); exit();
}
$a = $A["response"]["items"]; //не помню зачем уж. вроде как для удобства.
$C = --$A["response"]["count"]; //тут какбы общее число людей, но массивы с нуля нумеруются, потому так.
for ($b = 0;$b<=$C;$b++) { //начнём с массива 0, закончим последним членом беседы.
$var1 = $a[$b]; //открываем его краткую стату в messages.getConversationMembers
if(array_key_exists('is_admin', $var1)){ //ищем, админ ли он
if (mb_substr($var1["member_id"], 0,1) != "-"){ //и проверяем, что это не группа. |P
//а тут уж твой код, дружище.
}
}
}
//зэ энд.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question