A
A
askhatCH2019-10-03 16:34:03
PHP
askhatCH, 2019-10-03 16:34:03

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;
  }
}

Further, I did not understand something. The brain boils. I need it to give the index of the array where there is an online key (well, its value: 0 or 1). And so that as a result, in order, it gives out a list of online users. How to do this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nujabes37, 2019-10-03
@Nujabes37

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.

V
vasiiil, 2019-10-04
@vasiiil

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

D
devil's jackal, 2019-10-16
@Deathfinite

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 question

Ask a Question

731 491 924 answers to any question