Find key in Array and display the value - php

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;

Related

Deleting complex array element

So I want to delete an array element from a JSON array based on an id in a sub-array. I know it sounds weird. Here's an example of the array. I want to delete the entire array [0] based on the [dealer][id] array where the [id] = 20220 in this example.
Array
(
[results] => Array
(
[offset] => 1
[length] => 15
[data] => Array
(
[0] => Array
(
[dealer] => Array
(
[id] => 20220
[name] => apple
)
)
)
)
}
In reality there are a lot more elements in the [results] array. I'm not sure how to go about it.
Any help is greatly appreciated!
Loop thru data key first then check if dealer id matches the searched id
$id = 20220;
foreach ($array['results']['data'] as $key => $value) {
if ($value['dealer']['id'] == $id) {
unset($array['results']['data'][$key]);
}
}
use array_filter,
$array['results']['data'] = array_filter($array['results']['data'], function($v){return $v['dealer']['id'] != 20220;});

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.

Can you retrieve nested array by name in php

I have an array like this:
(
[data] => Array
(
[account_id] => 1
[description] => my asset
[value] => Estimate
[value_amount] => 85000
[type] => Vehicle
[owner] => Array
(
[app_id] => 123
[percent] => 100
)
)
)
Clearly I can loop through the array and pull out the nested owner array that way, but is there something similar to array_column that will get the entire owner nested array without having to loop ?
Use the indexes, no function necessary.
$owner = $array['data']['owner']
or..
$percent = $array['data']['owner']['percent']
In php it's call associative array which mean index will not numeric it can be anythink.
So you can retrieve data like
<?php echo $array['data']['owner']['app_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?

How to access stdclass object after a specific key value pair?

I have a stdclass object as shown below:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
I iterate over multiple such objects, some of which have
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
$value = (array) $object;
Third Solution
$value = json_decode(json_encode($object), true);
to get value of converted array
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
$object->values->{'0'}->id
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
echo get_object_vars($object)['values']['0']['id'];
I had the same issue, still not so sure why but I was able to get it working using this workaround:
$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2; //****Not Working
$tmp = (object)$elements;
$tmp = $tmp ->id; //****Working
//$tmp =$elements['id'] ; //****Not Working
return $tmp == $k2;
I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).
$elements can be Array to but I chose to demonstrate with json string.
I hope this helps somehow !!!
$Obj=stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
$Values= $result->values;
$Item = $Values[0];
$id=$Item->id;
$text = $Item->text;
$url=$Item->url;
I'm doing the same thing and all I did was this;
<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
this can help you accessing subarrays in php using codeigniter framework
foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
echo $novo_puto_ultimos_30_dias;
What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like
Object['values'][0]['id']
or
Object['values'][0]->id
which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.

Categories