Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,098 members, 7,821,796 topics. Date: Wednesday, 08 May 2024 at 06:34 PM

PHP Programmers, Pls Step In - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP Programmers, Pls Step In (1488 Views)

Python Programmers, Pls Help Me Out? / Hello Programmers.. Pls I Need HELP Here Urgently!!! / Need Professional Php Programmers For A Project, Money Involved (2) (3) (4)

(1) (Reply) (Go Down)

PHP Programmers, Pls Step In by Nobody: 8:41pm On Feb 08, 2017
I'm trying to filter out object for Chelsea in league table on the api below but
$arrow returns empty an empty set. meanwhile, $json_output returns the total array in the league table.....


here is an example of $json_output

{
"leagueCaption ": "Premier League 2015/16" ,
"matchday ": 10 ,
"standing":
[
{
"rank": 1 ,
"teamName ": "Chelsea FC",
"teamId": 65 ,
"playedGames": 10 ,
"crestURI": "http://upload.wikimedia.org/wikipedia/de/f/fd/ManCity.svg" ,
"points ": 22 ,
"goals ": 24 ,
"goalsAgainst": 8 ,
"goalDifference ": 16
},
{ ... },
{ ... }
]
}

My question: How do I filter out the chealse file cos $json_output->teamName doesn't seem to return Chelsea FC

<?php
$jsonurl = "http://api.football-data.org/v1/competitions/426/leagueTable" ;
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
function rinse($obj)
{
return $obj->teamName ==
"Chelsea FC" ;
}
$arrow = array_filter($json_output,
'rinse' );
?>
<?php echo print_r($arrow); ?>
Re: PHP Programmers, Pls Step In by talk2hb1(m): 9:27pm On Feb 08, 2017
Couldn't make sense of your post undecided

1 Like

Re: PHP Programmers, Pls Step In by Nobody: 9:46pm On Feb 08, 2017
talk2hb1:
Couldn't make sense of your post undecided


sorry about that. but I want to get the objects in the api
Re: PHP Programmers, Pls Step In by FrankLampard: 11:08pm On Feb 08, 2017
This is an array if object, not just object. Look very well you have an object fellowed by an array of object (correct me when necessary).

If you have only one data to get from the array of object. You can do so $obj[0]->teamName which will get the first index.or you just loop thru.

Show us a good screenshot of your data so we can understand what you are trying to do
Re: PHP Programmers, Pls Step In by Nobody: 11:22pm On Feb 08, 2017
FrankLampard:
This is an array if object, not just object. Look very well you have an object fellowed by an array of object (correct me when necessary).

If you have only one data to get from the array of object. You can do so $obj[0]->teamName which will get the first index.or you just loop thru.

Show us a good screenshot of your data so we can understand what you are trying to do
I've tried that.
$obj[0]->teamName

my browser doesn't support uploading files... plus I'm developing wit a phone...
Re: PHP Programmers, Pls Step In by Nobody: 11:27pm On Feb 08, 2017
FrankLampard:
This is an array if object, not just object. Look very well you have an object fellowed by an array of object (correct me when necessary).

If you have only one data to get from the array of object. You can do so $obj[0]->teamName which will get the first index.or you just loop thru.

Show us a good screenshot of your data so we can understand what you are trying to do


I can't seem to grab d datas because of
this standing element
"standing":
[
{
"rank": 1 ,
Re: PHP Programmers, Pls Step In by ChinenyeN(m): 11:46pm On Feb 08, 2017
You don't have direct access to the teamName. From what it looks like, teamName is in a nested array. You'd have to access standing, then index then access teamName.

$json_output->standing[0]->teamName

Going by the PHP manual, I believe that should work.
Re: PHP Programmers, Pls Step In by Nobody: 11:59pm On Feb 08, 2017
ChinenyeN:
You don't have direct access to the teamName. From what it looks like, teamName is in a nested array. You'd have to access standing, then index then access teamName.

$json_output->standing[0]->teamName

Going by the PHP manual, I believe that should work.

owk bro, gonna try it right away. tanx a lot
Re: PHP Programmers, Pls Step In by daPretender: 7:24am On Feb 09, 2017
ChinenyeN:
You don't have direct access to the teamName. From what it looks like, teamName is in a nested array. You'd have to access standing, then index then access teamName.

$json_output->standing[0]->teamName

Going by the PHP manual, I believe that should work.

OP this is the correct answer
Re: PHP Programmers, Pls Step In by onojagodday(m): 9:37am On Feb 09, 2017
The code is perfectly ok but the object is not returning the string because u used relational operator(return $obj=>teamName=="Chelsea Fc") instead of assignment operator( return $obj=>teamName="Chelsea Fc") ).

Also make sure that "teamName" exists as global variable of the object($obj).
Re: PHP Programmers, Pls Step In by Nobody: 5:41pm On Feb 10, 2017
onojagodday:
The code is perfectly ok but the object is not returning the string because u used relational operator(return $obj=>teamName=="Chelsea Fc"wink instead of assignment operator( return $obj=>teamName="Chelsea Fc"wink ).

Also make sure that "teamName" exists as global variable of the object($obj).


tanx bro... I've seen my mistake, but fell into anoda... I changed changed Chelsea fc to a variable, and its giving a null result. for example
$team = "Chelsea FC";
$obj=>teamName="$team"

1 Like

Re: PHP Programmers, Pls Step In by ChinenyeN(m): 6:08pm On Feb 10, 2017
lankykid:
tanx bro... I've seen my mistake, but fell into anoda... I changed changed Chelsea fc to a variable, and its giving a null result. for example
$team = "Chelsea FC";
$obj=>teamName="$team"

It would help of you showed a little bit more context for your problem. Nonetheless, at first glance here is what I'm understanding.

It seems to me that you want to access the teamName member, but you're doing two things wrong.

1. If your JSON is still the same one you showed earlier, then you still need to access the standing member first, index and then access the teamName member.

2. You need to change the => to ->. For PHP, => is the assignment operator for associative arrays, and -> is the operator for accessing object properties.

Lastly, since I do not know the full context of your NULL result, I will also speculate that you might also have an issue with the part where it says ="$team". Depending on what your objective is, you are either trying to assign a value (which requires a single equal sign =) or compare the values (which requires two equal signs ==).
Re: PHP Programmers, Pls Step In by Nobody: 8:44pm On Feb 10, 2017
ChinenyeN:


It would help of you showed a little bit more context for your problem. Nonetheless, at first glance here is what I'm understanding.

It seems to me that you want to access the teamName member, but you're doing two things wrong.

1. If your JSON is still the same one you showed earlier, then you still need to access the standing member first, index and then access the teamName member.

2. You need to change the => to ->. For PHP, => is the assignment operator for associative arrays, and -> is the operator for accessing object properties.

Lastly, since I do not know the full context of your NULL result, I will also speculate that you might also have an issue with the part where it says ="$team". Depending on what your objective is, you are either trying to assign a value (which requires a single equal sign =) or compare the values (which requires two equal signs ==).




tanx sir, but if I say I can understand u, I'm Lying. I've left php for a while now. I learnt it myself while I was 16... here is what I want, I want to get the index number of d team from competition array

for example

/noble.php?id=427&home=Reading

427 is d id of a competition, while Reading is a participating team. I wanna get the index of Reading using this code


<?php

$club = $_GET['home'];
$id = $_GET['id'];
$jsonurl = "http://api.football-data.org/v1/competitions/$id/leagueTable" ;
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);



$houseow = array_filter($json_output->standing, function($obj)
{
return $obj->teamName=="$club" ;
})
;



$u = key($houseow);

?>



<?php $link = print_r($houseow[$u]->goals);

echo $link;?>




<?php echo
"The index is $u, Club name is $club, comp id is $id";
?>
Re: PHP Programmers, Pls Step In by ChinenyeN(m): 2:54am On Feb 12, 2017
Lankykid, your code is fine actually. The only issue is the below:

$houseow = array_filter($json_output->standing, function($obj) { return $obj->teamName=="$club"; });

The PHP interpreter will consider $club to be undefined, because it considers your previous $club = $_GET["home"]; statement to be out of scope. There are one of two ways to solve this. The first way, probably the simplest and most compatible way would be to use the $_GET superglobal again. So, essentially, you would have the following:

$houseow = array_filter($json_output->standing, function($obj) { return $obj->teamName==$_GET["home"]; });

The second way to solve this would be with the use() syntax. Basically, it looks like this:

$houseow = array_filter($json_output->standing, function($obj) use($club) { return $obj->teamName=="$club"; });

The use() syntax of PHP allows you to pass variables from the parent scope to your anonymous functions. The only thing is that you will have to be running version 5.3+ of the PHP interpreter, since that is when the use() syntax was introduced. If the version of PHP you are running is older than 5.3, then I would suggest going with the $_GET["home"] option.
Re: PHP Programmers, Pls Step In by Nobody: 8:27pm On Feb 12, 2017
ChinenyeN:
Lankykid, your code is fine actually. The only issue is the below:



The PHP interpreter will consider $club to be undefined, because it considers your previous $club = $_GET["home"]; statement to be out of scope. There are one of two ways to solve this. The first way, probably the simplest and most compatible way would be to use the $_GET superglobal again. So, essentially, you would have the following:



The second way to solve this would be with the use() syntax. Basically, it looks like this:



The use() syntax of PHP allows you to pass variables from the parent scope to your anonymous functions. The only thing is that you will have to be running version 5.3+ of the PHP interpreter, since that is when the use() syntax was introduced. If the version of PHP you are running is older than 5.3, then I would suggest going with the $_GET["home"] option.



bless u sir.... php is really fun... I figured it out few days ago. but learnt new tins from u. tanx. and God bless

(1) (Reply)

Top 3 Challenges For Artificial Intelligence (AI) Startup Companies / Currently Building My First Ecommerce Site From Scratch / Help, In Love With Programming But In Need Of A Laptop

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 39
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.