簡體   English   中英

在PHP中獲取stdObject數組值

[英]Getting stdObject array values in PHP

好的,我正在使用以下代碼:

$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]值。 我在這里看到了許多示例,但是我正在使用的代碼首先使用了一個函數,這讓我感到困惑。 任何幫助是極大的贊賞。 謝謝。

如果您想獲取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
                ) ...*/
foreach($result['results'] as $a){
  unset($result['results'][$a]['stats_link']);
  unset($result['results'][$a]['team_name']);
}

它將刪除數組中每個鍵的“ stats_link”和“ team_name”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM