PHP Notice: Undefined index: content [duplicate] - php

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.

Related

php Multidimensional Array - Undefined offset / index [duplicate]

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']

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.

PHP array value into second array [duplicate]

This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 9 years ago.
I've been searching around but i'm failing to find an answer to something that seems it should be simple to fix!
I'm reading products from XML and putting their data into an array on a loop, the array is called $res.
Now, i need to put a value from $res into another array for loading to the DB (magento SOAP API). But when i do this i do not get a string value i wxpect, instead i get an the first array inside the second.
Here is the problem line:
$fieldDateData = array('rts_date'=>$res[0]->BackInStockDate1);
I've tried a few different things, none have worked. I thought it would be simply enough to do this:
$data = $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
But sadly not, i'm unsure as to why?
Thanks,
EDIT:
This is an example of the output
Array
(
[rts_date] => SimpleXMLElement Object
(
[0] => 28/06/13
)
)
Try
$data = (string)$res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
You need to cast the value you are setting as a string:
$data = (string) $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);

reference object with key containing brackets [duplicate]

This question already has answers here:
PHP Object Property has brackets in it
(2 answers)
Closed 9 years ago.
I'm doing a print_r on an object that is returned after running a query. Here is my print_r log.
stdClass Object
(
[MAX(sort_order)] => 3
)
I want to get the value inside [MAX(sort_order)] but I cant figure out how to target it from within php.
Like $sort_order = $object->[MAX(sort_order)]; (I know that won't work)
Does anyone know how I can do this?
Try add this in your query MAX(sort_order) AS max_sort_order in your query

Is it possible to use PHP's [ ] array vars in a GET request? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to pass an array via $_GET in php?
So I have a script which receives a $_POST with var Board[] containing an array of IDs.
I realized when adding ShareThis to the pages that folks wanting to share the result set would send an invalid link.
I definitely can add a delimited parameter to the script so ShareThis can pass
http://foo.com/bar.php?DelimitedBoard=3|4|5.
My question is whether there is a way to do this using the current Board[] var?
http://foo.com/bar.php?Board[]=3,4,5 fails. Is there a way?
Sure, just do:
http://foo.com/bar.php?Board[]=3&Board[]=4&Board[]=5
What you want to do is something like this:
http://foo.com/bar.php?Board[]=3&Board[]=4&Board[]=5
Then, if you print_r($_GET['Board']); in bar.php, you will get something like:
Array (
[0] => 3
[1] => 4
[2] => 5
)
Number array serialization is very easy. Use join(',', $Board) to form a comma-separated ID string, use explode(',', $_GET["Board"]) to get the array back.

Categories