1

Ok I'm working with the following code:

$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);

That returns the following:

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"

and continues on for a however long.

I need to get the [team_name] and [stats_link] values out of that array. I've seen a number of examples on here, but the code I'm working with uses a function first and that's where I get confused. Any help is greatly appreciated. Thank you.

3
  • you need team_name and stats_link removed or you want to use them someplace else ? Commented Apr 27, 2014 at 4:59
  • I want to save them to a variable if possible, not remove them. Commented Apr 27, 2014 at 19:29
  • I see the poor use of wording on my part. My mistake. Commented Apr 27, 2014 at 19:30

2 Answers 2

1

If you want to get team_name and stats_link for other purposes you need to loop that array. Best way is to make another array with those values, eg:

$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
    .....*/

If you want those removed you can do:

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
                ) ...*/
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome. Thank you. I used your first answer. If I can ask another question, how would I save those to variables? So in that for loop I could send them to a database or w.e. else I wanted.
foreach($teams as $team => $link){ }
0
foreach($result['results'] as $a){
  unset($result['results'][$a]['stats_link']);
  unset($result['results'][$a]['team_name']);
}

it will remove 'stats_link' and 'team_name' of each key of the array.

3 Comments

Ok so I get the following error when I put that in on the unset lines: Parse error: syntax error, unexpected ']', expecting ',' or ')'
No worries :) Now it's telling me Warning: Illegal offset type on those lines. Any thoughts?
Now it just repeats that error over and over. Before it just displayed it once. Thanks for all the help :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.