How do I loop through numbered indexes in a stdClass Object? - php

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

Related

PHP Get / check value from associative array in array of individual stdClass Objects

Having real issues with this. I want to be able to get a value from this data which is returned via an API.
ie get value by
$CM_user_customfields['Organisation'],
$CM_user_customfields->Organisation.
is that even possible? I have tried loops and rebuilding the array but i always end up with a similar results and perhaps overthinking it.
I can't use the [int] => as the number of custom fields will be changing a lot.
$CM_user_customfields = $CM_details->response->CustomFields ;
echo '<pre>' . print_r( $CM_user_customfields, true ) . '</pre>';
// returns
Array
(
[0] => stdClass Object
(
[Key] => Job Title
[Value] => Designer / developer
)
[1] => stdClass Object
(
[Key] => Organisation
[Value] => Jynk
)
[2] => stdClass Object
(
[Key] => liasoncontact
[Value] => Yes
)
[3] => stdClass Object
...
many thanks, D.
I recommend convert to associative array first:
foreach($CM_user_customfields as $e) {
$arr[$e->Key] = $e->Value;
}
Now you can access it as:
echo $arr['Organisation'];
You can also achieve it by: (PHP 7 can convert stdClass and will do the trick)
$arr = array_combine(array_column($CM_user_customfields, "Key"), array_column($CM_user_customfields, "Value")));

Changing values of an array which consists of multiple objects

I would like to replace a value within the path array and I'm quite stuck for a while. So here is what I got.
My array:
// $myArr
Array
(
[0] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Bob
[1] => pictures
[2] => food
)
)
)
[1] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Alice
[1] => pictures
[2] => vacations
[3] => rome
)
)
)
)
PHP code:
for ($i=0; $i < count($myArr) ; $i++) {
$search = array($old_name); // pictures
$replace = array($new_name); // test
$result = str_replace($search, $replace, $myArr[$i]->doc->path);
}
Result:
It only changes one array and gives me a hint on my str_replace line. Both, $search and $replace are of type array and I know that I need to access elements in an array via array notation -> $item['price'] for example. That is not what is wrong here right?
Notice: Trying to get property of non-object in ...
Array
(
[0] => Bob
[1] => test
[2] => food
)
1) Do you see why he only modifies the last object so to speak?
2) Why is he giving me a Notice whereas I don't violate type conventions in my opinion?
Your code is working fine on my end, however..
I think the problem is in your $result variable. With every iteration, you are overwriting the last written value in the array.
You have to either use that $result variable directly, or replace $result by $myArr[$i]->doc->path. That way you can use the $myArr with the rewritten values.

Extract data from php array

I have the following array in my Moodle plugin:
Array (
[0] => stdClass Object (
[id] => 32
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1464071400
[timefinish] => 1464102000 )
[1] => stdClass Object (
[id] => 33
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1465281000
[timefinish] => 1465311600 )
)
How to get the data. Right now, when I make:
$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));
I get only data from the frist array [0]
How to get the data from every array key and then the single values? Thanks in advance.
The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.
If you have an array somewhere, then you can get fields from it by writing:
echo $myarray[0]->id;
echo $myarray[1]->id;
etc.
If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:
$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
echo $session->id;
}

Multi dimensional array, with an object for good measure - iteration?

This one is starting to get on my nerves. Still fairly new to arrays and objects.
I need to be able to pull out [id] in the numbered array, plus get access to the lonely snippet_count at the end.
I can do it if there is no top level container array using a foreach $a as $k => $v., (from an earlier SO question) but am struggling a level deeper. Thanks.
Array
(
[snippets] => Array
(
[0] => stdClass Object
(
[id] => 123456789
)
[1] => stdClass Object
(
[id] => 123456789
)
[2] => stdClass Object
(
[id] => 123456789
)
//and so on
)
[snippet_count] => 500
)
You can iterate over just the snippets array to get the IDs
$ids = array();
foreach ($array['snippets'] as $snippet) {
$ids[] = $snippet->id;
}
$count = $array['snippet_count'];
Is this what you're looking for?

Find key in Array and display the value

I have a little problem with array. Im using codeigniter. What i want to do, is something like that:
$studentSchool = $students->schoolId;
echo $shools->id[$studentSchool]->schoolName;
Its in foreach $students loop, and array with schools looks like that:
Array ( [0] => stdClass Object ( [id] => 1 [schoolName] => Akademia Ekonomiczna ) [1] => stdClass Object ( [id] => 2 [schoolName] => Politechnika ) )
Those are my first steps in php and codeigniter, so please have mercy :)
If $schools is the array, you have to access the it as an array. It won't have an id property;
You should build your $schools array such that the index of element corresponds to the ID of the school. I.e. you should have:
Array (
[1] => stdClass Object ( [id] => 1 [schoolName] => ... )
[2] => stdClass Object ( [id] => 2 [schoolName] => ... )
)
Then you can do:
echo $schools[$studentSchool]->schoolName;
Or, if the schools are sorted by ID and the IDs are continuous, you can also do:
echo $schools[$studentSchool - 1]->schoolName;
Otherwise you have to loop over the array to find the right entry for the given ID which is expensive and unnecessary.
Learn more about arrays.
Is this what you're looking for?
foreach ($students as $student):
// Prints the School name for this student
echo $student->schoolName;
endforeach;
Or maybe this?:
// Prints the School name for the first student
echo $students[0]->schoolName
EDIT: This is what you want?
$studentSchool = $students->schoolId;
echo $shools[$studentSchool]->schoolName;

Categories