If $item is an illegal offset, then what is the proper way to retrieve the
value of a 2d array?]]>Mr Oldies2013-12-03T14:00:50-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184038&th=123316#msg_184038
noreply@example.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?
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]]>Paul Herber2013-12-03T14:16:38-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184039&th=123316#msg_184039
paul@pherber.com> wrote:
>
> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply@example.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@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments]]>Robert Heller2013-12-03T14:55:20-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184040&th=123316#msg_184040
> At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul@pherber.com> wrote:
>
>>
>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply@example.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.]]>Mr Oldies2013-12-03T15:04:44-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184046&th=123316#msg_184046
> [I didn't read the manual and my code doesn't work again]
--
Denis McMahon, denismfmcmahon@gmail.com]]>Denis McMahon2013-12-03T21:10:49-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184050&th=123316#msg_184050
> At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul@pherber.com> wrote:
>
>>
>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply@example.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]]>Mr Oldies2013-12-03T22:25:49-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184051&th=123316#msg_184051
> At Tue, 03 Dec 2013 14:16:38 +0000 Paul Herber <paul@pherber.com> wrote:
>
>>
>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply@example.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.]]>Mr Oldies2013-12-03T22:39:26-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184052&th=123316#msg_184052
noreply@example.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.]]>Ben Bacarisse2013-12-03T23:06:24-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184053&th=123316#msg_184053
noreply@example.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@pherber.com> wrote:
>>
>>>
>>> On Tue, 3 Dec 2013 09:00:50 -0500, richard <noreply@example.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?
--
Robert Heller -- 978-544-6933 / heller@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments]]>Robert Heller2013-12-03T23:16:14-00:00Re: illegal offset problem
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184055&th=123316#msg_184055
> 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.
> 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