A
A
Alexander Kerzakov2016-05-21 01:36:22
PHP
Alexander Kerzakov, 2016-05-21 01:36:22

How to write a variable?

Execute the request

$friends_getRequests = curl( 'https://api.vk.com/method/friends.getRequests?offset=1&count=1&extended=1&need_viewed=1&access_token='.$token);

In response we get
{"response":[{"uid":33224900},{"uid":205869219},{"uid":321678938},{"uid":364608800},{"uid":360153341},{"uid":361830009},{"uid":277374452},{"uid":232364661},{"uid":81323385},{"uid":345063168},{"uid":359946852},{"uid":348982141},{"uid":19482073},{"uid":217818751},{"uid":275193481}]}

How exactly to write the first id into the $user_1 variable?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Fedorov, 2016-05-21
@PabloRosi

$response = '{"response":[{"uid":33224900},{"uid":205869219},{"uid":321678938},{"uid":364608800},{"uid":360153341},{"uid":361830009},{"uid":277374452},{"uid":232364661},{"uid":81323385},{"uid":345063168},{"uid":359946852},{"uid":348982141},{"uid":19482073},{"uid":217818751},{"uid":275193481}]}';
$data = json_decode($response, true);
if (json_last_error() === JSON_ERROR_NONE && isset($data['response'][0]['uid'])) {            
    $user_1 = $data['response'][0]['uid'];
}

R
Rou1997, 2016-05-21
@Rou1997

This is a string in JSON format, it must be parsed using json_decode, there are examples and lessons on the Internet.

A
Alexey, 2016-05-21
@alsopub

Something like this:

$s = '{"response":[{"uid":33224900},{"uid":205869219},{"uid":321678938},{"uid":364608800},{"uid":360153341},{"uid":361830009},{"uid":277374452},{"uid":232364661},{"uid":81323385},{"uid":345063168},{"uid":359946852},{"uid":348982141},{"uid":19482073},{"uid":217818751},{"uid":275193481}]}';

$json = json_decode($s);
$user_1 = $json->{'response'}[0]->{'uid'};

echo($user_1);

P
profesor08, 2016-05-21
@profesor08

I'll write a little more. First you need to check if the json is parsed correctly and no errors have occurred, and then iterate over each user and get an array of their id.

$s = '{"response":[{"uid":33224900},{"uid":205869219},{"uid":321678938},{"uid":364608800},{"uid":360153341},{"uid":361830009},{"uid":277374452},{"uid":232364661},{"uid":81323385},{"uid":345063168},{"uid":359946852},{"uid":348982141},{"uid":19482073},{"uid":217818751},{"uid":275193481}]}';

$json = json_decode($s);
$users = array();

if (!json_last_error())
{
    foreach($json->response as $user)
    {
        $users[] = $user->uid;        
    }
}

var_dump($users);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question