Im fetching a pdo object with data from the database. now when i foreach this data I want to add extra key values but this doest work.
foreach ($deliveryCompanies as $k=>$v) {
$k->test = 'test';
}
this returns
Type: ErrorException
Code: 2
Message: Attempt to assign property of non-object
the object looks like this
Array
(
[0] => stdClass Object
(
[delivery_id] => 2
[delivery_location_id] => 34
[delivery_category_id] => 1
)
[1] => stdClass Object
(
[delivery_id] => 4
[delivery_location_id] => 34
[delivery_category_id] => 1
)
)
1
That's because $k is the key, not the value. You need to do $v->test ='test';
Related
I have an stdClass object which looks like this, I am trying to echo out the steamid
stdClass Object
(
[response] => stdClass Object
(
[players] => Array
(
[0] => stdClass Object
(
[steamid] => 76561198039509812
[communityvisibilitystate] => 1
[profilestate] => 1
[personaname] => Mike_Ock_Hurtz
[lastlogoff] => 1506899637
[profileurl] => http://steamcommunity.com/profiles/76561198039509812/
[avatar] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg
[avatarmedium] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg
[avatarfull] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg
[personastate] => 0
)
)
)
)
Below is how I am getting the above:
foreach(updateBanList::readFile($file) as $steamprofiles){
$steam = $steamUser->GetPlayerSummariesV2($steamprofiles[0]);
print_r($steam);
$players = $steam->response->players;
}
To access the class so far I am have used
$players = $steam->response->players;
This gets me to the last array
Array
(
[0] => stdClass Object
(
[steamid] => 76561198039509812
[communityvisibilitystate] => 1
[profilestate] => 1
[personaname] => Mike_Ock_Hurtz
[lastlogoff] => 1506899637
[profileurl] => http://steamcommunity.com/profiles/76561198039509812/
[avatar] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg
[avatarmedium] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg
[avatarfull] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg
[personastate] => 0
)
)
I am trying to echo out the steamid, and I've tried a few things I know whatever it is i am doing is very simple, but its late in the day and I cannot figure it out.
If I try
$players->steamid;
I get the error
Notice: Trying to get property of non-object
If I try
$players['steamid'];
I get
Notice: Undefined index: steamid
What am I doing wrong?
Try $players[0]->steamid;
Your object is inside an array
And if you are sure you will only get one object element in the array then use
$player = $steam->response->players[0];
$player->steamid; will suffice.
You have to use the array.
$players = $steam->response->players;
$steamid = $players[0]->steamid;
or in one line;
$steamid = $steam->response->players[0]->steamid;
Now you can echo out you steamid:
echo $steamid;
If you have more than one Player in your array you have to loop thou:
$players = $steam->response->players;
$steamids = array();
foreach ($players as $key => $player)
{
$steamids[$key] = $player->steamid;
}
Now you have an array of Steam IDs in $steamids. To echo it you can do:
foreach ($steamids as $key => $steamid)
{
echo "Player " . $key . " has Steam ID:" . $steamid;
}
Notice: Trying to get property of non-object
Now that you understand that the next data type is an array and not an object, it is clear that doing something like $players->steamid; will cause an Error.
Notice: Undefined index: steamid
$players['steamid']; cloud also not work because there is no Key/Index steamid in this array only one Key/Index with the value 0
$players is an indexed array of objects.
Try this:
print_r($players[0]->steamid);
Or this to dump all the array elements:
foreach ($players as $player) {
print_r($player->steamid);
}
$players is array, You should access it's property on array element:
$players[0]->steamid;
I have JSON like this (from nested sorting)
[
{"id":13},{"id":14},
{"id":15,
"children":[
{"id":16},{"id":17},{"id":18}
]
},
{"id":19},{"id":20},
{"id":21,
"children":[
{"id":22}
]
}
]
how I PHP loop to put this JSON in MySQL
Thank you.
As with any valid JSON format string, you can use PHP's built-in json_decode to convert it into a parsable object and then loop through those parameters. In this case, from the JSON string you've given (which seems to be an array)
$array = json_decode($string);
foreach ($array as $val) {
//The object $val will be:
"id":13
}
If it's nested, you would do another foreach loop and detect for the property that needs to be looped. For example you could do this in a variety of ways (check for the "children" property, loop through the $val properties and check if it is an array, if it is then loop through that). Inside this foreach loop iteration, you can insert it, or do execute whatever statement you need to get it inside MySQL
Your question is pretty vague on the format and way you want it inserted. I'd suggest showing some code you've tried so other people can help you and know what direction you're going in. (that's probably the reason for the downvote, not mine by the way)
Looping through all the properties of object php
just put your valid json inside json_decode and assign it to a php array as below
//$php_arr = json_decode('YOUR_JSON');
$php_arr = json_decode('[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]');
/*comment the following 3 lines when done*/
echo "<pre>";
print_r($php_arr);
echo "</pre>";
/*comment the above 3 lines when done*/
output
Array
(
[0] => stdClass Object
(
[id] => 13
)
[1] => stdClass Object
(
[id] => 14
)
[2] => stdClass Object
(
[id] => 15
[children] => Array
(
[0] => stdClass Object
(
[id] => 16
)
[1] => stdClass Object
(
[id] => 17
)
[2] => stdClass Object
(
[id] => 18
)
)
)
[3] => stdClass Object
(
[id] => 19
)
[4] => stdClass Object
(
[id] => 20
)
[5] => stdClass Object
(
[id] => 21
[children] => Array
(
[0] => stdClass Object
(
[id] => 22
)
)
)
)
Now as you have PHP array do what ever you want like
foreach($php_arr as $arr){
if(!isset($arr['children'])){
$q = "insert into tbl(id) values('".$arr['id']."')";
}else{
//your logic
}
}
You need to use json_decode function to decode JSON data. Then use foreach loop to manipulate data.
Try example
$str = '
[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]
';
$json = json_decode($str);
//var_dump($json);
foreach ($json as $item)
{
//The object $val will be:
echo $item->id."<br />";
//You INSERT query is here
//echo "INSERT INTO table (field) VALUE ($item->id)";
$query = mysqli_query($conn, "INSERT INTO table (field) VALUE ($val->id)");
}
I have a big problem and I can't resolve it,
So I have my array :
Array
(
[0] => Array
(
[id] => 34
[groupe_id] => 4
[object_id] => 4
)
[1] => Array
(
[id] => 35
[groupe_id] => 4
[object_id] => 5
)
)
Now I want to create another array call $test for get the array in this forme:
Array
(
[object_id] = 4
[object_id] = 5
)
I tried but no results:
$test = array();
foreach($aObjectsGroupe as $object){
$test[] = array(
'object_id' => $object['object_id']
);
}
You can't have duplicates of the same key in a PHP array. It kind of defeats the purpose of keys. I can't think of a reason to have identical keys, as you would be unable to reference an individual element of the array by key anyways, because there are more than one.
Why not just make an array called $object_ids, and just have a normal indexed array of the all of the object_ids from the other array?
$object_ids = array();
foreach ($aObjectsGroupe as $object) {
$object_ids[] = $object['object_id'];
}
This is the output that I'm trying to access:
stdClass Object
(
[results] => stdClass Object
(
[columns] => stdClass Object
(
[name] => Name
[id] => id
)
[data] => stdClass Object
(
[team] => Array
(
[0] => stdClass Object
(
[name] => Kansas City
[id] => 47556332
)
[1] => stdClass Object
(
[name] => Chi White Sox
[id] => 03948575
)
[2] => stdClass Object
(
[name] => Detroit
[id] => 3747646625
)
)
)
)
)
)
I'm trying to get the id of this, but I'm running into troubles with accessing anything after the 0+. I have two foreach loops here because I need to iterate through the name and id and place them in a table, but I have no problem doing that. I just need to get at whatever is inside the [0]. How do I reference that 0?
foreach ($data->results->data->team as $team_data) {
foreach ($team_data->THE ID/NUMBER->name as $team_id) {
#code...
}
}
When I do the code above, I get so so many errors. I've tried different ways and keep getting an error in the form of:
Notice: Undefined property: stdClass::$0 in index.php on line 61 Notice: Trying to get property of non-object in index.php on line 61
This error is from trying a $id, and $id++ to get the numbered part. I know this question has been asked before, but I need to get through multiple numbers and not just 0, because apparently
$team_data->{'0'}->name
would work for just one, but I get errors even trying to do that, and I need to get 0, 1, 2, etc.
I think you might be overthinking the solution..
foreach ($data->results->data->team as $team_id => $team_data) {
// $team_id holds the index of the teams array
echo $team_id;
echo $team_data->name;
echo $team_data->id;
}
$team_data is already the object with name and id in it. So skip the second foreach and access $team_data['id'] and $team_data['name'] directly
I've a result from a webservice's query and I would like to get some values from it. It works but I have PHP notice issues, so I'm probably doing something wrong.
This is the $items variable content :
stdClass Object
(
[response] => stdClass Object
(
[0] => stdClass Object
(
[id] => 275
[corpid] => 16107
[name] => default
[description] =>
[status] => ok
[nbSteps] => 7
)
[defaultItem] => 275
)
[error] =>
[status] => success
)
So I tried something like :
foreach ( $items->response AS $key => $item ) {
if ( $item->name == 'default' ){ // Line 106
$Id = $item->id;
}
}
It works, $Id is equal to 275 but PHP returns a notice :
Notice: Trying to get property of non-object in /home/web/dev/webservice-form.php on line 106
Any help would be greatly appreciated.
EDIT : This is the content of the $item variable (taken from the foreach loop) :
stdClass Object
(
[id] => 275
[corpid] => 16107
[name] => default
[description] =>
[status] => ok
[nbSteps] => 7
)
275
Please note that the '275' is a part of the result.
The problem is the defaultItem entry in your inner object. Your loop will at some point reach this and try to access name, which doesn't exist, because there is no object.
Should be easily solveable with is_object().
You have mixed types, one being an objects, and one an int value, try checking what each item is:
foreach ( $items->response AS $key => $item ) {
if(is_object($item) && $item->name == 'default'){ // Line 106
$Id = $item->id;
}
else {
$Id = $item; // assume it's scalar value
}
}
Obviously it would depend on what else you can expect on what other check you need to add in there..