php Multidimensional Array - Undefined offset / index [duplicate] - php

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I have an array that holds a number of arrays. I access them in turn e.g.
print_r($orderData[0]) and the output is:
Array (
[201] => Array (
[name] => SuperMarket One
[type] => line_item
[item_meta] => Array (
[_qty] => Array (
[0] => 1
)
)
)
)
My issue is, I can't seem to access anything e.g. print_r($orderData[0]['name']) gives me:
Notice: Undefined index: name in /Path
Likewise, I can't use print_r($orderData[0][0]) etc.
I must be missing something quite obvious here.

You have another array in it with the key 201. Use PHP's function var_dump($orderData[0]) to view the complete structure of your array.

Access it using
$orderData[0][201]['name']

Related

PHP Remove dupllicate array value which is a date [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(26 answers)
Closed 3 years ago.
I have an array,
`Array
(
[0] => 2019-08-02
[1] => 2019-08-02
[2] => 2019-08-03
)`
I need to remove duplicates from this array.I tried converting strtotime and compared But not getting result.
Please help
Regards,
Rekha
You can just write the code like this:
$arr = array_unique($arr)
Hope this helps.

PHP Array 1 Value Extract - Gives Error [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I know this question has been asked before.
but whenever I try to get the value of my array it does not work, only difference I can see is the name of a value is a number?
$returnValue = json_decode('{"content": [{"id": 1249029}]}');`
That gives me an array that looks like:
stdClass::__set_state(array(
'content' =>
array (
0 =>
stdClass::__set_state(array(
'id' => 1249029,
)),
),
))
So getting my id should be as simple as:
var_dump($returnValue['content']->0->id);
This code gives me an error 500.
Thanks,
You need to access it like this:
$returnValue->content[0]->id;

PHP Notice: Undefined index: content [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 4 years ago.
I know this question sounds silly, and seems like a duplicate to the other questions, but I can almost assure you it is not a duplicate.
I am getting an issue from PHP:
Notice: Undefined index: content in FILE_PATH on line 21
Line 21: echo htmlentities($about['content']);
I know for sure that $about['content'] is set because for debugging, I have
print_r($about)
Which gives me
Array
(
[0] => Array
(
[id] => 1
[page] => About Us
[content] => I have stuff here.... content for test purpose.
)
)
Any help is appreciated.
Thanks
Edit
This question is not duplicate because all the answers to the other questions don't talk about array of array. Please do not mark this question as duplicate.
From your print_r looks like you need to correct the source, or use:
echo htmlentities($about[0]['content']);
The $about is an array of array that contains the content index. One of the solution is to use:
$about = $about[0]; // Only if you know that this is the only case.
The right way is to correct at the source, why it is not being a source array, but has another sub-array.

Fetching data from array inside array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
I've finally figured out how to fetch data from Google Analytics to website, unfortunately output (print_r) is PHP array inside an array:
Array ( [0] => Array ( [0] => 196 ) )
I need to get the number 196 & "save" it as variable in order to use it in calculations or output it, any ideas how to achieve this?
Im looking for overall solution/principle/rule because some data is even array inside array inside array etc.
you need to grab the first array element -position 0 - and from this inner array you pick the first element - also position 0 - which is 196, Check this: PHP Fiddle - hit run to execute
// getting 196
echo $myArr[0][0];
Use this:
$newVariable = $yourArray[0][0];
echo $newVariable; //This will echo the answer.
//This answer is self explanatory.

Getting an item with a # from an stdClass [duplicate]

This question already has answers here:
Special characters in property name of object
(2 answers)
Closed 9 years ago.
When I do a print_r($this->info->artist->image[4]);, I get this:
stdClass Object
(
[#text] => http://userserve-ak.last.fm/serve/_/68145708/AWOLNATION+PNG.png
[size] => mega
)
How do I get the [#text] part?
You would do:
$this->info->artist->image[4]->{'#text'};
$this->info->artist->image[4]->{'#text'};

Categories