I have facing issue in accessing the index 1 in a array.
How can i access the index 1?
Tried to access like this.
$selection = $menu_selection->{1}->vl
but i it showing undefined offset error. can anyone help me.
Array
(
[1] => stdClass Object
(
[vl] => Array
(
[0] => 1
[1] => 2
)
[op] => Array
(
[0] => O
[1] => O
)
)
[189] => Array
(
[vl] => Array
(
[0] => 1
)
)
)
From PHP 7.2 on you can do this (costly way):
((object) $menu_selection)->{1}->v1;
If way of accessing values doesn't matter it should be:
$menu_selection[1]->v1;
You are making a two errors.
$selection = $menu_selection->{1}->vl
The first error is accessing the first array position, you must do something like this $selection = $menu_selection[1].
The second error is using ->, this notation is used to access object properties and to call object functions.
To access index 1, you do something like this:
$selection = $menu_selection[1]
To access vl from array 1 try:
$selection = $menu_selection[1]->vl
As you can see in the var_dump, $menu_selection[1] is an object of type stdClass, that's why you need -> to access the vl property.
Related
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.
I have an array of values returned from Facebook - let's call it $array.
If I do print_r($array) - it looks like this:
Array
(
[code] => 200
[headers] => Array
(
[0] => Array
(
[name] => Some value
[value] => *
)
[1] => Array
(
[name] => Some value
[value] => Some value
)
[2] => Array
(
[name] => Some value
[value] => Some value
)
)
[body] => {"about":"Some more values.","can_post":true}
)
I need to extract the body part from this array.
I cannot refer to it by it's position, I'm looking for something like $array->body and receive the {....} string.
$array->body would work if the variable $array was an object
For arrays, just use:
$body = $array['body'];
(see: http://be2.php.net/manual/en/language.types.array.php)
If you want to access to your array via -> just do 1 more step:
$array = (object) $array;
And now, you can access to your body via:
$array->body;
Else without this step there is just one way:
$array['body'];
If you are more interested about converting arrays into objects, you can visit this question: How to convert an array to object in PHP?
Access array elements by using their name.
$array['body'];
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 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.
I have a multidim array that I can view using print_r($users):
Array
(
[0] => stdClass Object
(
[username] => crustacea
[created_on] => 2010-08-07 19:54:32
[active] => 1
)
)
I can also use print_r($users[0]). Since there's only one member in $users, the output looks somewhat the same.
I can't see how to retrieve the value crustacea. echo $users[0]['username']; doesn't do it. Examples I can find are echo $users['name_of_array']['username']; -- but I don't have a name of array to work with. How do I do it?
$user[0] is not an array but an object as indicated by [0] => stdClass Object.
echo $users[0]->username;
$user[0] is object and not an array. You can access your value by doing
$users[0]->username;
Or you could cast/convert your object into an array like
((array)$users[0])['username'];