Struggling with this problem for an hour now and searched both stackoverflow and google and couldn't find an answer that helped. I have the following from xdebug:
- list (array)
-[0] (object)
---id (string)
---proj_name (string)
---proj_desc (string)
I am trying to pull out the value of id. Can anyone tell me how to do this? Thank you.
$list[0]->id
$list is an array containing an object at position 0, therefore this object can be accessed with $list[0]
the object has a property named id, which can be accessed with $object->id
If you have more than one object in the array, then you can loop through the values contained in the array with foreach. For example:
foreach ($list as $object) {
echo $object->id . "<br/>";
}
This will take each object in the array and display its id value.
Use foreach to loop through the list:
foreach($list as $obj) // Where $list is the list containing the objects
{
$id = $obj->id;
}
Related
i want get data from stdclass object. i use foreach method to get key and value.
with var_dump i can get all infromation about the post, buy i want extract all 'display'.
foreach($data as $key=>$value){
var_dump($value);
}
var_dump result :
i just want extract all display_ur property. can anyone exlain me ?
do like below:-
foreach($data as $key=>$value){
foreach($value as $val){
echo $val->node->display_url;
echo PHP_EOL;
}
}
Your original array is made out of stdClassObjects. Each of these class objects has a public property called node that is also a stdClassObject.
That means that if you want to retrieve the display_url for each of these objects, you need:
foreach ($array as $object) {
$node = $object->node;
var_dump($node->display_url); // this should return what you are looking for
}
$value->display_url. I think this will work for you.
With type cast
$array=(object) $stdClassObject;
foreach($array as $key=>$value){
var_dump($value);
}
I am trying to access to this object $results, and I can't figure out why the first echo and the print_r give me the expected output, but the last echo gives me the error "Undefined property".
foreach($results as $number => $hotel){
echo $number;
print_r($hotel);
echo $results->{$number};
}
-----------------------------------Better explanation---------------------------------
I noticed that you need some other informations to help me, so:
$results is an object containing results of a query: it contains a lis of hotels (objects) and everyone of them is descripted by some fields->value.
In order to modify this object (delete one hotel from the object $results) I was looking for the method to reference an item of the object $results, and I thought that the way was $results->{$number}
So the thing that i really want to do is:
foreach($results as $number => $hotel){
foreach($hotel as $field => $value) {
if($field == 'id' && $value == '18'){
// here I wanna delete the hotel from the object $results
// but unset($results->{$number}) doesn't work.
}
}
}
The foreach as you show is normaly use with an array. So to access the items you need to use the good operator that is []
In the foreach the $number is the key , the $hotel are items in the array.
So to acces an hotel you need $result[$number]
Hello I can't figure out how to loop through this json encoded array and for each object, get all its values. I need each value as a variable for itself.
echo json_encode($formulars);
This is what i get when i echo it out
[{"project_name":"polle","type":"support","title":"vi","reason":"prover","solution":"igen","comments":"okay ","date_stamp":"2013-08-20 14:06:37","used_time":132},{"project_name":"dolla","type":"support","title":"lolol","reason":"skl","solution":"dskal","comments":"kflafda ","date_stamp":"2013-08-20 14:11:36","used_time":210},{"project_name":"polle","type":"fejl","title":"lol","reason":"aksdl","solution":"fdjks","comments":"djsks ","date_stamp":"2013-08-20 14:13:27","used_time":1230}]
I have tried this piece of code and I managed to get out the project_name from the first object and that's it:
foreach ($formulars as $current => $project_name) {
$project_name['project_name'];
}
So is there any way i can get all the variables for each object in my array instead of just the project_name?
Like this:
foreach ($formulars as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
Thanks in advance
Seems like you have an objects inside an array. So you will need to loop through the array and get each object. Just JSON_DECODE your encoded string like below.
Perhaps:
$data = json_decode($formulars,true);
/* Since it's only one object inside the array, you could just select element zero, but I wil loop*/
//You should now be able to do this
foreach ($data as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
The reason I loop is because there is a object inside an array(Javascript way I think).
Use json_decode to convert the json object to an array; then use foreach to loop through the array. That should work.
<?php
$arr_json = json_decode($formulars);
foreach($arr_json as $key => $value)
//Code to perform required actions
?>
This should give you some ideas.
Use json_decode (with TRUE for getting an associative array)to convert your JSON object to an associative array. After that, you can use a foreach loop to traverse through your multidimensional array and print the required values.
Code:
$json = json_decode($string, true);
foreach ($json as $key => $value) {
foreach($value as $key2 => $value2) {
echo $value2."\n";
}
}
Working Demo!
I have the following array that is in PHP (this is a print_r() output).
I'm currently struggling to loop through this data - need to be able to process each part and access the values in each array item. How can I do this.
I've tried the following unsuccessfully...
foreach (array as $key => $value) {
echo $key;
}
Try this. Since you have an array of objects, you should be able to access each object property using ->
foreach($array as $value) {
echo $value -> userid;
}
It should echo out all the user id in that array of objects
You have an array of objects, so try something like this:
<?php
foreach ($array as $value) {
echo $value->userid;
echo $value->action;
echo $value->photo_name;
}
You don't need the $key since you're not using it in the loop. Each iteration will put the object in the $value variable, on which you can access it's properties.
I am trying to print the contents of a nested array, but it simply returns "Array" as a string yet will not iterate with a foreach loop. The arrays are coming from a mongodb find(). The dataset looks like this:
User
Post_Title
Post_Content[content1,content2,content3]
I am trying to get at the content1,2,3.
My current code looks like this:
$results = $collection->find($query);
foreach ($results as $doc)
{
echo $doc['title']; //this works
$content[] = $doc['content'];
print_r($content); //this prints "Array ( [0] => Array )"
foreach ($content as $item)
{
echo $item;
}
}
All this code does is print the Title, followed by Array ( [0] => Array ), followed by Array.
I feel quite stupid to not figure out something that seems so basic. Most posts on stack overflow refer to multidimensional associative arrays - in this case, the top level array is associative, but the content array is indexed.
I have also tried
foreach ($doc['content'] as $item)
But that gives
Warning: Invalid argument supplied for foreach()
I even tried iterating over the returned array again using a nested foreach, like the following:
foreach ($results as $doc)
{
echo $doc['title']; //this works
$content[] = $doc['content'];
print_r($content); //this prints "Array ( [0] => Array )"
foreach ($content as $item)
{
foreach ($item as $next_item)
{
echo $next_item;
}
echo $item;
}
}
The second foreach failed with an invalid argument..
Any thoughts would be greatly appreciated.
edit: perhaps it has something to do with how I am inserting the data to the DB. That code looks like this
$title = $_POST['list_title'];
$content[] = $_POST['list_content1'];
$content[] = $_POST['list_content2'];
$content[] = $_POST['list_content3'];
$object = new Creation();
$object->owner = "$username";
$object->title = "$title";
$object->content = "$content";
$object->insert();
Is this not the proper way to add an array as a property to a class?
The issue is some of array content is an array while other parts are strings. You should test for the sub content $doc being an array using is_array(). If it is, loop through the $doc like you would any other array, and if it isn't, you can echo the content (may need to test the content type before doing this if you're uncertain as to what content it can be)
The problem is in the code that you are saving data. You should replace the following line
$object->content = "$content";
with
$object->content = $content;