从API调用返回的复杂数组中隔离特定值

时间:2013-03-21 01:26:41

标签: php arrays api

我正在使用API​​进行语言分类。我创建分类器,添加两个类,训练它们,然后发送一个字符串进行分类。分类调用的响应是一个数组:

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

在响应上调用print_r会打印出以下字符串(我已选中它以尝试理解它):

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

我感兴趣的数组中唯一的部分是数字(以粗体突出显示)。如何编写print语句来提取这两个数字?

编辑:未修改的print_r:

数组([0] =>数组([id] => Classify12911363801322 [分类] =>数组([0] =>数组([class] => Cool [p] => 0.636574 )[1] =>数组([class] => Uncool [p] => 0.363426))[text] =>海量文本字符串))

1 个答案:

答案 0 :(得分:1)

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