Home > OS >  Array_unique can't be used
Array_unique can't be used

Time:01-10

I get json data from a api and i try to remove all duplicate element (if 2 element was named "aa" i want to remove one of both) For that i do that

$plugins = Http::accept('application/json')->get('https://poggit.pmmp.io/plugins.min.json')->object();
$plugins = array_unique($plugins);

But i get

Object of class stdClass could not be converted to string.

When i gettype on $plugins i get array.

I was try with

        $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://poggit.pmmp.io/plugins.min.json'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

    $headers = array();
    $headers[] = 'Accept: */*'; // you can try to change with application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
   $plugins = json_decode($result, true); // decode to array
   $plugins = array_unique($plugins); 

But here i have array to string conversion at '$plugins = array_unique($plugins);'

Why do I have this error?

CodePudding user response:

So i try use your url to make a GET request using cURL in php, here my try:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://poggit.pmmp.io/plugins.min.json'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

$headers = array();
$headers[] = 'Accept: */*'; // you can try to change with application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
   echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$plugins = json_decode($result, true); // decode to array
$plugins = array_unique($plugins); // make it unique
print_r($plugins); // It's work!

CodePudding user response:

Do you have try convert stdClass to to Array using json_decode($plugins, true); before using array_unique($plugins).

  •  Tags:  
  • Related