illegal offset problem [message #184037] |
Tue, 03 December 2013 14:00 |
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 #184039 is a reply to message #184038] |
Tue, 03 December 2013 14:55 |
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 |
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 #184050 is a reply to message #184039] |
Tue, 03 December 2013 22:25 |
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 |
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 |
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 |
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 |
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
|
|
|
|