-1

I'm trying to get data from API and I receive this

[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]

I want to get the category for each service without repeating the same category twice.

Edited* I have this code

            $servers = $this->Setting->Loop('api','WHERE is_active = 1');
            foreach($servers->result() as $server){
                foreach($this->Api_Connect->services($server->api_url, $server->api_key) as $item) {
                    echo '<option data-server='.$server->id.' data-percent='.$server->addon_percent.' data-price='.$item['rate'].' data-min='.$item['min'].' data-max='.$item['max'].' value="'.$item['service'].'">- '.$item['name'].'</option>';
                }
            }

which connect to each server with api url and key and return with the services.

Emma
  • 27,428
  • 11
  • 44
  • 69
Gamal Mohamed
  • 43
  • 1
  • 8
  • 2
    So, what you have tried to get the result ? Stackoverflow is not about what you want to achieve, but what you've tried and failed. – Shudhansh Shekhar May 15 '19 at 05:06
  • You are required to actually write some code and try something. Do some research. – ryantxr May 15 '19 at 05:07
  • I already wrote the whole code and with looping and everything but I can't loop the categories with out repeating ! http://prntscr.com/nopllh so i'm asking for help – Gamal Mohamed May 15 '19 at 05:09
  • @GamalMohamed post your code here. Images can be deleted in future and your question does not help anyone anymore. – daremachine May 15 '19 at 05:11
  • Possible duplicate of [PHP unique array by value?](https://stackoverflow.com/questions/6422562/php-unique-array-by-value) – Naing Lin Aung May 15 '19 at 05:16

4 Answers4

3

We can set a new array, and push categories in the new array, and check if the new value does not exist do so:

$data = '[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]';

$data = json_decode($data, true);

$category = array();
foreach ($data as $value) {
    if (!array_search($value["category"], $category)) {
        array_push($category, $value["category"]);
    }
}

var_dump($category);

Output

array(2) {
  [0]=>
  string(14) "First Category"
  [1]=>
  string(15) "Second Category"
}

Edit:

Based on Andreas's advice, we can also use if(in_array()) in the loop using:

 $category[$value["category"]] = $value["category"]; 

which is much more efficient.

Emma
  • 27,428
  • 11
  • 44
  • 69
1
$array = [];     
foreach($object as $key => $value) { 
        // object is data you receive from API 
        array_push($array,$value->category);
}
// to get unique values 
$array = array_unique($array); 

I hope this code solve your problem

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48
Kirsten Phukon
  • 314
  • 3
  • 17
1

The fastest method is to use array_column.
Array_column will isolate one column of the array.
The third parameter of array_column will set the key name.
This is what we want.
Because you don't want duplicates we set something as value and "category" as key.

This means it will loop once and overwrite the keys that is duplicated.
The other methods posted here will need to check if the item is already added to the list or using array_unique which needs to compare each item with every other item, that is a slow function when you give it a large array.

$data = '[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]';

$data = json_decode($data, true);

$category = array_column($data, "rate", "category");

var_dump($category);

Output of this:

// Note it's only the keys we want, the values are not interesting
array(2) {
  ["First Category"]=>
  string(4) "0.90"
  ["Second Category"]=>
  string(1) "8"
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • 1
    Not always the best answers. Yours aren't bad, it's just slightly inefficient. If you change it to adding the values in the array as an associative array with both key and value being "category" then you don't need the `if(in_array())` and I wouldn't be surprised if it would be faster than my answer. Meaning `$category[$value["category"]] = $value["category"];` in your loop. Don't worry you earned your 25 points! You go shopping :-) @Emma – Andreas May 15 '19 at 18:45
0
$jsonResponse='[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]';

$listData=json_decode(jsonResponse,true);


foreach($listData as $valRes){
echo $valRes['category'];

}

you can get category


Sarfaraz
  • 146
  • 8