Tiffany
Tiffany

Reputation: 237

Isolating a specific value from complicated array returned from API call

I'm working with the API for a language classifier. I create the classifier, add two classes, train them, and then send a string to be classified. The response for the classification call is an array:

$response = $uclassify->classify($bigString, $title);
print_r($response);

Calling print_r on the response prints the following string (I've tabbed it to try and make sense of it):

Array ( 
    [0] => Array ( 
        [id] => Classify12911363801322 
        [classification] => Array ( 
            [0] => Array ( 
                [class] => Cool [p] => **0.636574** 
            ) 
            [1] => Array ( 
                [class] => Uncool [p] => **0.363426** 
            ) 
        ) [text] => 
    )
)

The only part of the array I'm interested in is the numbers (highlighted in bold). How do I write a print statement to extract these two numbers?

EDIT: unmodified print_r:

Array ( [0] => Array ( [id] => Classify12911363801322 [classification] => Array ( [0] => Array ( [class] => Cool [p] => 0.636574 ) [1] => Array ( [class] => Uncool [p] => 0.363426 ) ) [text] => massive text string))

Upvotes: 0

Views: 57

Answers (1)

galchen
galchen

Reputation: 5290

$numbers = array();
foreach($response[0]['classification'] as $o){
    $numbers[] = $o['p'];
}
print_r($numbers);

Upvotes: 1

Related Questions