FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » illegal offset problem
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
illegal offset problem [message #184037] Tue, 03 December 2013 14:00 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
foreach ($six0 as $item){

$year=$six0[$item]['year'];
echo $year;


}

If $item is an illegal offset, then what is the proper way to retrieve the
value of a 2d array?
Re: illegal offset problem [message #184038 is a reply to message #184037] Tue, 03 December 2013 14:16 Go to previous messageGo to next message
Paul Herber is currently offline  Paul Herber
Messages: 26
Registered: February 2011
Karma: 0
Junior Member
On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply(at)example(dot)com> wrote:

>
> foreach ($six0 as $item){
>
> $year=$six0[$item]['year'];
> echo $year;
>
>
> }
>
> If $item is an illegal offset, then what is the proper way to retrieve the
> value of a 2d array?

RTFM
http://us1.php.net/manual/en/language.types.array.php

There is an example of foreach with a 2D array halfway down the page.
FFS.



--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/ twitter: @sandrilaLtd
Re: illegal offset problem [message #184039 is a reply to message #184038] Tue, 03 December 2013 14:55 Go to previous messageGo to next message
Robert Heller is currently offline  Robert Heller
Messages: 60
Registered: December 2010
Karma: 0
Member
At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul(at)pherber(dot)com> wrote:

>
> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply(at)example(dot)com> wrote:
>
>>
>> foreach ($six0 as $item){
>>
>> $year=$six0[$item]['year'];
>> echo $year;
>>
>>
>> }
>>
>> If $item is an illegal offset, then what is the proper way to retrieve the
>> value of a 2d array?
>
> RTFM
> http://us1.php.net/manual/en/language.types.array.php
>
> There is an example of foreach with a 2D array halfway down the page.
> FFS.

Note: *technically* PHP (like C and all of the other C-like languages) does
not have multi-dimensional arrays (in the sense FORTRAN's multi-dimensional
arrays). (Using OO coding it is possible to create true multi-dimensional
array as a new class, typically by allocating a 1D block of memory and
overloading the () or [] operators.) Instead, it can have an array of arrays
(in C it would be an array of pointers), which 'fakes' 2 (or more) dimensional
arrays. So the correct code for the above would be (the foreach does the array
element dereferencing automatically):

foreach ($six0 as $item){
$year=$item['year'];
echo $year;
}

OR (more verbosely, getting the keys (indexes) explicity)

foreach ($six0 as $itemkey => $item) {
$year=$six0[$itemkey]['year']; /* $item IS $six0[$itemkey]! */
echo $year;
}

>
>
>

--
Robert Heller -- 978-544-6933 / heller(at)deepsoft(dot)com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Re: illegal offset problem [message #184040 is a reply to message #184039] Tue, 03 December 2013 15:04 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Tue, 03 Dec 2013 08:55:20 -0600, Robert Heller wrote:

> At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul(at)pherber(dot)com> wrote:
>
>>
>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply(at)example(dot)com> wrote:
>>
>>>
>>> foreach ($six0 as $item){
>>>
>>> $year=$six0[$item]['year'];
>>> echo $year;
>>>
>>>
>>> }
>>>
>>> If $item is an illegal offset, then what is the proper way to retrieve the
>>> value of a 2d array?
>>
>> RTFM
>> http://us1.php.net/manual/en/language.types.array.php
>>
>> There is an example of foreach with a 2D array halfway down the page.
>> FFS.
>
> Note: *technically* PHP (like C and all of the other C-like languages) does
> not have multi-dimensional arrays (in the sense FORTRAN's multi-dimensional
> arrays). (Using OO coding it is possible to create true multi-dimensional
> array as a new class, typically by allocating a 1D block of memory and
> overloading the () or [] operators.) Instead, it can have an array of arrays
> (in C it would be an array of pointers), which 'fakes' 2 (or more) dimensional
> arrays. So the correct code for the above would be (the foreach does the array
> element dereferencing automatically):
>
> foreach ($six0 as $item){
> $year=$item['year'];
> echo $year;
> }
>
> OR (more verbosely, getting the keys (indexes) explicity)
>
> foreach ($six0 as $itemkey => $item) {
> $year=$six0[$itemkey]['year']; /* $item IS $six0[$itemkey]! */
> echo $year;
> }

Thanks. The manual did not show it this way.
Re: illegal offset problem [message #184046 is a reply to message #184037] Tue, 03 December 2013 21:10 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 03 Dec 2013 09:00:50 -0500, richard cried:

> [I didn't read the manual and my code doesn't work again]

Well read the manual then, Richard

This time you want:

http://us1.php.net/manual/en/control-structures.foreach.php

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: illegal offset problem [message #184050 is a reply to message #184039] Tue, 03 December 2013 22:25 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Tue, 03 Dec 2013 08:55:20 -0600, Robert Heller wrote:

> At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul(at)pherber(dot)com> wrote:
>
>>
>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply(at)example(dot)com> wrote:
>>
>>>
>>> foreach ($six0 as $item){
>>>
>>> $year=$six0[$item]['year'];
>>> echo $year;
>>>
>>>
>>> }
>>>
>>> If $item is an illegal offset, then what is the proper way to retrieve the
>>> value of a 2d array?
>>
>> RTFM
>> http://us1.php.net/manual/en/language.types.array.php
>>
>> There is an example of foreach with a 2D array halfway down the page.
>> FFS.
>
> Note: *technically* PHP (like C and all of the other C-like languages) does
> not have multi-dimensional arrays (in the sense FORTRAN's multi-dimensional
> arrays). (Using OO coding it is possible to create true multi-dimensional
> array as a new class, typically by allocating a 1D block of memory and
> overloading the () or [] operators.) Instead, it can have an array of arrays
> (in C it would be an array of pointers), which 'fakes' 2 (or more) dimensional
> arrays. So the correct code for the above would be (the foreach does the array
> element dereferencing automatically):
>
> foreach ($six0 as $item){
> $year=$item['year'];
> echo $year;
> }
>
> OR (more verbosely, getting the keys (indexes) explicity)
>
> foreach ($six0 as $itemkey => $item) {
> $year=$six0[$itemkey]['year']; /* $item IS $six0[$itemkey]! */
> echo $year;
> }
>
>>
>>
>>

}

foreach ($six0 as $itemkey => $item) {
$song=$six0[$itemkey]['track'];
echo "<div>$song</div>";};

works fine. shows all songs for every year.

}

foreach ($six0 as $itemkey => $item) {
$song=$six0[$itemkey]['track'];
if ($year==$show){ echo "<div>$song</div>";}
}

Show nothing. Why?

http://mroldies.net/test/menu4.html
Re: illegal offset problem [message #184051 is a reply to message #184039] Tue, 03 December 2013 22:39 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Tue, 03 Dec 2013 08:55:20 -0600, Robert Heller wrote:

> At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul(at)pherber(dot)com> wrote:
>
>>
>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply(at)example(dot)com> wrote:
>>
>>>
>>> foreach ($six0 as $item){
>>>
>>> $year=$six0[$item]['year'];
>>> echo $year;
>>>
>>>
>>> }
>>>
>>> If $item is an illegal offset, then what is the proper way to retrieve the
>>> value of a 2d array?
>>
>> RTFM
>> http://us1.php.net/manual/en/language.types.array.php
>>
>> There is an example of foreach with a 2D array halfway down the page.
>> FFS.
>
> Note: *technically* PHP (like C and all of the other C-like languages) does
> not have multi-dimensional arrays (in the sense FORTRAN's multi-dimensional
> arrays). (Using OO coding it is possible to create true multi-dimensional
> array as a new class, typically by allocating a 1D block of memory and
> overloading the () or [] operators.) Instead, it can have an array of arrays
> (in C it would be an array of pointers), which 'fakes' 2 (or more) dimensional
> arrays. So the correct code for the above would be (the foreach does the array
> element dereferencing automatically):
>
> foreach ($six0 as $item){
> $year=$item['year'];
> echo $year;
> }
>
> OR (more verbosely, getting the keys (indexes) explicity)
>
> foreach ($six0 as $itemkey => $item) {
> $year=$six0[$itemkey]['year']; /* $item IS $six0[$itemkey]! */
> echo $year;
> }
>
never mind. got it working.
Re: illegal offset problem [message #184052 is a reply to message #184050] Tue, 03 December 2013 23:06 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
richard <noreply(at)example(dot)com> writes:
<snip>
> foreach ($six0 as $itemkey => $item) {
> $song=$six0[$itemkey]['track'];
> echo "<div>$song</div>";};
>
> works fine. shows all songs for every year.
>
> }

This code is odd because it gets the array element and the index. The
body of the loop then indexes the array. Why not either only get the
index or, better, only get the element?

> foreach ($six0 as $itemkey => $item) {
> $song=$six0[$itemkey]['track'];
> if ($year==$show){ echo "<div>$song</div>";}
> }
>
> Show nothing. Why?

This code is even odder because neither $year nor $show change in the
loop. It will either do the same as the first loop and print every song
for every year, or it will print nothing at all.

I suspect that $year is a typo. Maybe it should be something like
$item['year']?

--
Ben.
Re: illegal offset problem [message #184053 is a reply to message #184050] Tue, 03 December 2013 23:16 Go to previous messageGo to next message
Robert Heller is currently offline  Robert Heller
Messages: 60
Registered: December 2010
Karma: 0
Member
At Tue, 3 Dec 2013 17:25:49 -0500 richard <noreply(at)example(dot)com> wrote:

>
> On Tue, 03 Dec 2013 08:55:20 -0600, Robert Heller wrote:
>
>> At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul(at)pherber(dot)com> wrote:
>>
>>>
>>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply(at)example(dot)com> wrote:
>>>
>>>>
>>>> foreach ($six0 as $item){
>>>>
>>>> $year=$six0[$item]['year'];
>>>> echo $year;
>>>>
>>>>
>>>> }
>>>>
>>>> If $item is an illegal offset, then what is the proper way to retrieve the
>>>> value of a 2d array?
>>>
>>> RTFM
>>> http://us1.php.net/manual/en/language.types.array.php
>>>
>>> There is an example of foreach with a 2D array halfway down the page.
>>> FFS.
>>
>> Note: *technically* PHP (like C and all of the other C-like languages) does
>> not have multi-dimensional arrays (in the sense FORTRAN's multi-dimensional
>> arrays). (Using OO coding it is possible to create true multi-dimensional
>> array as a new class, typically by allocating a 1D block of memory and
>> overloading the () or [] operators.) Instead, it can have an array of arrays
>> (in C it would be an array of pointers), which 'fakes' 2 (or more) dimensional
>> arrays. So the correct code for the above would be (the foreach does the array
>> element dereferencing automatically):
>>
>> foreach ($six0 as $item){
>> $year=$item['year'];
>> echo $year;
>> }
>>
>> OR (more verbosely, getting the keys (indexes) explicity)
>>
>> foreach ($six0 as $itemkey => $item) {
>> $year=$six0[$itemkey]['year']; /* $item IS $six0[$itemkey]! */
>> echo $year;
>> }
>>
>>>
>>>
>>>
>
> }
>
> foreach ($six0 as $itemkey => $item) {
> $song=$six0[$itemkey]['track'];
> echo "<div>$song</div>";};
>
> works fine. shows all songs for every year.
>
> }
>
> foreach ($six0 as $itemkey => $item) {
> $song=$six0[$itemkey]['track'];
> if ($year==$show){ echo "<div>$song</div>";}
> }
>
> Show nothing. Why?

Because $year != $show. What are $year's and $show's values?

>
> http://mroldies.net/test/menu4.html
>
>

--
Robert Heller -- 978-544-6933 / heller(at)deepsoft(dot)com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Re: illegal offset problem [message #184055 is a reply to message #184050] Tue, 03 December 2013 23:46 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 03 Dec 2013 17:25:49 -0500, richard wrote:

> foreach ($six0 as $itemkey => $item) {
> $song=$six0[$itemkey]['track'];
> echo "<div>$song</div>";};
>
> works fine. shows all songs for every year.
>
> }
>
> foreach ($six0 as $itemkey => $item) {
> $song=$six0[$itemkey]['track'];
> if ($year==$show){ echo "<div>$song</div>";}
> }
>
> Show nothing. Why?
>
> http://mroldies.net/test/menu4.html

I'm absolutely guessing on this one, but maybe in the second case $year
isn't equal to $show

Try dumping $show and $year

I suspect the code would be more efficient as:

if ($year==$show)
foreach ($six0 as $item)
echo "<div>{$item['track']}</div>";

1) Your code is checking $year against $show on every iteration of the
foreach loop, although neither of these variables is altered inside the
loop. Under these conditions, it makes more sense to do the comparison
outside the loop, and skip the whole loop if they don't match.

2) Having allocated the array data to $item, it is probably more
efficient to use it directly than to reference it as $six0[$itemkey],
which requires an array lookup.

I'm sure you will dismiss these points as pedantry, but pedantry such as
this equates to money in the real world of sizing server farms for the
server load.

Also, how many times do we have to point out to you that giving us links
to the output of the script doesn't really help us work out what you
screwed up.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: illegal offset problem [message #184084 is a reply to message #184046] Wed, 04 December 2013 20:13 Go to previous message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Tue, 3 Dec 2013 21:10:49 +0000 (UTC), Denis McMahon
<denismfmcmahon(at)gmail(dot)com> wrote:

> On Tue, 03 Dec 2013 09:00:50 -0500, richard cried:
>
>> [I didn't read the manual and my code doesn't work again]
>
> Well read the manual then, Richard

Please attend better to your capitalization.

>
> This time you want:
>
> http://us1.php.net/manual/en/control-structures.foreach.php
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Can I download file with address like this "http://***.com/file.php/ABC.html" automatically ?
Next Topic: request for error checking feature
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Dec 03 19:27:28 GMT 2024

Total time taken to generate the page: 0.02902 seconds