在PHP中获取stdObject数组值

时间:2014-04-27 04:05:28

标签: php arrays json api

好的我正在使用以下代码:

$userGuid = "2d7c4ca4-d1b6-4c2a-9106-33df1251d946";
$apiKey = "wuZBiCx2jWwbNJgw88M6jJvxp0LYBBG9o/cxgHZx+cdNVKaPnMASmbdbj/4oVrch5NZZlPULad0pamUar9kUrA==";

function query($connectorGuid, $input, $userGuid, $apiKey, $additionalInput) {

  $url = "https://api.import.io/store/connector/" . $connectorGuid . "/_query?_user=" . urlencode($userGuid) . "&_apikey=" . urlencode($apiKey);

  $data = array("input" => $input);
  if ($additionalInput) {
    $data["additionalInput"] = $additionalInput;
  }

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  curl_setopt($ch, CURLOPT_POSTFIELDS,  json_encode($data));
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  $result = curl_exec($ch);
  curl_close($ch);

  return json_decode($result, true);

}

// Query for tile Teams and Links
$result = query("3997cc2e-4fce-4431-89da-0dc542d83b84", array(
  "webpage/url" => "http://msn.foxsports.com/foxsoccer/bundesliga/teams",
), $userGuid, $apiKey, false);

var_dump($result);

返回以下内容:

array(6) {
    ["offset"] => int(0)["results"] => array(18) {
        [0] => array(5) {
            ["stats_link/_source"] => string(52)
            "/foxsoccer/bundesliga/teams/1-fc-nurnberg/stats/5131" ["team_name"] => string(14)
            "1. FC Nurnberg" ["stats_link/_title"] => string(28)
            "Stats - 1. FC Nurnberg Stats" ["stats_link/_text"] => string(5)
            "Stats" ["stats_link"] => string(76)
            "http://msn.foxsports.com/foxsoccer/bundesliga/teams/1-fc-nurnberg/stats/5131"
        }[1] => array(5) {
            ["stats_link/_source"] => string(54)
            "/foxsoccer/bundesliga/teams/1899-hoffenheim/stats/6859" ["team_name"] => string(15)
            "1899 Hoffenheim" ["stats_link/_title"] => string(29)
            "Stats - 1899 Hoffenheim Stats" ["stats_link/_text"] => string(5)
            "Stats" ["stats_link"] => string(78)
            "http://msn.foxsports.com/foxsoccer/bundesliga/teams/1899-hoffenheim/stats/6859"

并且持续一段时间。

我需要从该数组中获取[team_name]和[stats_link]值。我在这里看到了很多例子,但我正在使用的代码首先使用了一个函数,这就是我感到困惑的地方。任何帮助是极大的赞赏。谢谢。

2 个答案:

答案 0 :(得分:1)

如果您想将team_namestats_link用于其他目的,则需要循环该数组。最好的方法是使用这些值创建另一个数组,例如:

$teams = array();
foreach($result['results'] as $team){
    $teams[$team['team_name']] = $team['stats_link'];
}
print_r($teams);

/*Array
(
    [1. FC Nurnberg] => http://msn.foxsports.com/foxsoccer/bundesliga/teams/1-fc-nurnberg/stats/5131
    [1899 Hoffenheim] => http://msn.foxsports.com/foxsoccer/bundesliga/teams/1899-hoffenheim/stats/6859
    .....*/

如果你想删除那些,你可以这样做:

foreach($result['results'] as $num => $team){
    unset($result['results'][$num]['team_name']);
    unset($result['results'][$num]['stats_link']);
}
print_r($result);

/*[0] => Array
                (
                    [stats_link/_source] => /foxsoccer/bundesliga/teams/1-fc-nurnberg/stats/5131
                    [stats_link/_title] => Stats - 1. FC Nurnberg Stats
                    [stats_link/_text] => Stats
                ) ...*/

答案 1 :(得分:0)

foreach($result['results'] as $a){
  unset($result['results'][$a]['stats_link']);
  unset($result['results'][$a]['team_name']);
}

它将删除' stats_link'和' team_name'数组的每个键。