Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to write a little webapp that will show some data. The data comes from an API. All data from the API I get displayed, but if I only want to display specific data, I always get an error.
I have not had much experience with API's yet, but all the tutorials I've done show the data via this echo method.
My current code:
<?php
$user_id = "3bb8d3bc-ab7a-45fb-8154-ed54897b2c4c";
$api_response = file_get_contents('https://r6tab.com/api/player.php?p_id='.$user_id);
$api_response_decoded = json_decode($api_response, true);
//foreach($api_response_decoded as $val) {
//echo $val, '<br>';
//}
echo $api_response_decoded->p_name;
?>
api_response Output:
"playerfound":true,"social":{"twitter":"","instagram":"","mixer":"","twitch":"","youtube":"","bio":"","esl":"","discord":"","background":"","aliases":"","embed":""},"seasonal":{"current_NA_mmr":0,"last_NA_mmr":0,"last_NA_mmrchange":0,"current_EU_mmr":3831,"last_EU_mmr":3881,"last_EU_mmrchange":-50,"current_AS_mmr":0,"last_AS_mmr":0,"last_AS_mmrchange":0,"total_casualwins":6,"total_casuallosses":5,"total_casualtotal":11,"total_casualkills":28,"total_casualdeaths":18,"total_rankedwins":38,"total_rankedlosses":24,"total_rankedtotal":62,"total_rankedkills":244,"total_rankeddeaths":201,"total_generalwins":44,"total_generallosses":29,"total_generaltotal":73,"total_generalkills":272,"total_generaldeaths":219,"total_totalbulletshits":3851,"total_totalhs":172,"total_totaltimeplayed":0,"bomb_wins":67,"bomb_losses":41,"bomb_total":108,"secure_wins":0,"secure_losses":0,"secure_total":0,"hostage_wins":1,"hostage_losses":0,"hostage_total":1,"favorite_mode":"bomb"},"matches":[{"casual_wlstatus":"won","casual_winslost":"1 Won","casual_datatime":"10\/02\/19","ranked_wlstatus":"won","ranked_winslost":"6 Won, 2 Lost","ranked_datatime":"10\/02\/19","next":"default","db_p_total_casualwins":1,"db_p_total_casuallosses":0,"db_p_total_casualkills":2,"db_p_total_casualdeaths":2,"db_p_total_rankedwins":6,"db_p_total_rankedlosses":2,"db_p_total_rankedkills":23,"db_p_total_rankeddeaths":23,"db_p_total_totalhs":10,"db_p_NA_currentmmr":0,"db_p_EU_currentmmr":3881,"db_p_AS_currentmmr":0,"NA_mmrchange":0,"EU_mmrchange":189,"AS_mmrchange":0},
My Error Message:
Trying to get property 'p_name' of non-object in C:\xampp\htdocs\R6S-Stats\stats.php on line 12
By using the true flag on json_decode, you're creating an associated array instead of an object. Remove that flag, and you'll be able to use the object methods:
$api_response_decoded = json_decode($api_response);
echo $api_response_decoded->p_name; //Scenus
If you want to keep it as an associated array, you can access the data like this:
echo $api_response_decoded['p_name'];
when use json_decode('dsfsdf', true);
$p_name = $api_response_decoded['p_name'];
when use json_decode('dsfsdf);
$p_name = $api_response_decoded->p_name;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I receive the following error message when I attempt to call a function which I need to push an object into an array:
array_push() expects parameter 1 to be array, null given
Any clues why this is happening? Thank you in advance :)
<?php
$programming = array();
//some unrelated lines of code here inbetween
function createProgramming($data){
global $programming;
$prog = new Programming($data);
array_push($programming, $prog);
}
?>
//random HTML here
<php?
createProgramming("str");
?>
//more html
$programming is only referenced in the code at those three locations present in my extract above.
That code works fine. There are few things that could make it break:
$programming is redefined / unset before createProgramming() is called
$programming is not defined in the global scope
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello i am hoping someone can help me, when i use print_r all only get the last result from the mysqli query, my code is below.
//Fetch data from sql results
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query=array($row['name']=>$row['system']);
}
}
you are overwriting your $page_query everytime in loop, change to:
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query[] =array($row['name']=>$row['system']);
}
You need to add it to the array - not replace the entire variable with what you have in that row.
while($row = $rs->fetch_assoc())
{
//Put results in a array
$page_query[]=array($row['name']=>$row['system']);
}
The short syntax for the function you are looking Array_push is simply to pop a set of empty square brackets behind the variable and then say =something;. This appends another element to the end of the array. This function will increment the index numerically.
because every time inside while you reinitialize $page_query so you should push them inside array to collect. use array_push()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm having to put together a PHP snippet involving capturing form submission data. I'm trying to do this the "right" way with OOP PHP, and I'm struggling with array usage since my Googling seems to bring up sample code that is either too simple or too complex for what I'm trying to do.
How do I create a class method that adds keys and values to an array?
My code, which does not work currently:
class Connection
{
private $postItem = array();
public function addPostItem($key,$value)
{
$postItem[$key] -> $value;
}
public function printPostItem() {
return $this->postItem;
}
}
$c1 = new Connection;
$c1->addPostItem('FirstName','John');
$c1->addPostItem('LastName','Doe');
$c1->addPostItem('Email','JohnDoe#mail.com');
var_dump($c1->printPostItem()); // shows no array content
If I'm going against other best practices here, please let me know.
Use this:
public function addPostItem($key,$value)
{
$this->postItem[$key] = $value;
}
Change:
$postItem[$key] -> $value;
To:
$this->postItem[$key] = $value;
Also as Amal said turn on error reporting.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Im trying to json_decode the following:
"main":{
"temp":9.04,
"temp_min":9.04,
"temp_max":9.04,
"pressure":938.13,
"sea_level":1037.57,
"grnd_level":938.13,
"humidity":87
},
"weather":[
{
"id":801,
"main":"Clouds",
"description":"few clouds",
"icon":"02d"
}
],
$result = json_decode($json);
$temp = $result->main->temp; //is displaying the data
however
$id = $result->weather->id; //is not displaying anything
all i can see is difference that te second one have an extra "[]"
can you help me telling how can i get weather->id from that json, thank you
The weather element is an array: it contains a list of elements. To get what you want from that exact example you would want:
$id = $result->weather[0]->id;
You also might want to think about what you want to happen if there is more than one element in that array, or if there are zero.