|
|
|
|
|
fixed! (was: foreach problem part two) [message #184277 is a reply to message #184268] |
Thu, 19 December 2013 16:07   |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On Thu, 19 Dec 2013 13:41:51 -0500, richard wrote:
> <?php
> foreach ($aname as $item){
> echo $aname[$item][1];
> echo " (".$aname[$item][2].")";
> }
> ?>
>
> I decided to create a second array that holds only the artist and number of
> records.
> So why am I getting "invalid argument" with this?
<?php
$result=count($art60);
$acount=0;
while ($acount<=$result){
echo "<div class='blu'>";
$number=count($art60[$acount]);
echo $art60[$acount][0];
echo " (".$number.")";
echo "</div>";
$acount++;
}
?>
This version not only shows the names, but the number of songs each
produced as well.
http://mroldies.net/artists/60artists.php
|
|
|
|
Re: foreach problem part two [message #184279 is a reply to message #184278] |
Thu, 19 December 2013 18:39   |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
> richard <noreply(at)example(dot)com> wrote in news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
> 40tude.net:
>
>> <?php
>> foreach ($aname as $item){
>> echo $aname[$item][1];
>> echo " (".$aname[$item][2].")";
>> }
>> ?>
>>
>> I decided to create a second array that holds only the artist and number of
>> records.
>> So why am I getting "invalid argument" with this?
>
> Because you don't understand how foreach() works. RTFM.
I did.
There is a flaw in the works that is not discussed.
That being, it won't work with brackted arrays.
Works fine with standard arrays.
While (){}. however works and could care less which is used.
|
|
|
Re: foreach problem part two [message #184280 is a reply to message #184279] |
Thu, 19 December 2013 19:51   |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On 12/19/2013 6:39 PM, richard wrote:
> On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
>
>> richard <noreply(at)example(dot)com> wrote in news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
>> 40tude.net:
>>
>>> <?php
>>> foreach ($aname as $item){
>>> echo $aname[$item][1];
>>> echo " (".$aname[$item][2].")";
>>> }
>>> ?>
>>>
>>> I decided to create a second array that holds only the artist and number of
>>> records.
>>> So why am I getting "invalid argument" with this?
>>
>> Because you don't understand how foreach() works. RTFM.
>
> I did.
> There is a flaw in the works that is not discussed.
> That being, it won't work with brackted arrays.
> Works fine with standard arrays.
>
> While (){}. however works and could care less which is used.
>
What do you mean by "bracketed arrays"? There is no such thing!
foreach() works fine with ANY array.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
Re: foreach problem part two [message #184283 is a reply to message #184279] |
Thu, 19 December 2013 22:11   |
Doug Miller
Messages: 171 Registered: August 2011
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
richard <noreply(at)example(dot)com> wrote in news:y1yigdragnyw$.c2kv48q50osj.dlg@
40tude.net:
> On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
>
>> richard <noreply(at)example(dot)com> wrote in news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
>> 40tude.net:
>>
>>> <?php
>>> foreach ($aname as $item){
>>> echo $aname[$item][1];
>>> echo " (".$aname[$item][2].")";
>>> }
>>> ?>
>>>
>>> I decided to create a second array that holds only the artist and number of
>>> records.
>>> So why am I getting "invalid argument" with this?
>>
>> Because you don't understand how foreach() works. RTFM.
>
> I did.
Then read it again, because you obviously didn't understand it first time through.
> There is a flaw in the works that is not discussed.
No, there isn't. The only flaw here is your failure to understand the way foreach() works.
RTFM.
> That being, it won't work with brackted arrays.
That's *not* where the problem is in your code. RTFM.
|
|
|
Re: foreach problem part two [message #184284 is a reply to message #184281] |
Thu, 19 December 2013 23:05   |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker wrote:
> richard wrote:
>
>> There is a flaw in the works that is not discussed.
>> That being, it won't work with brackted arrays.
>> Works fine with standard arrays.
>
> Nonsense. PHP has only one array type.[1]
>
>> While (){}. however works and could care less which is used.
>
> Assume, you have an array:
>
> $array = array('one', 'two', 'three');
>
> Now you want to echo its elements in order. What is simpler and more
> readable?
>
> $i = 0;
> while ($i < count($array)) {
> echo $array[$i];
> }
>
> or
>
> foreach ($array as $element) {
> echo $element;
> }
>
> Additionally, the foreach loop is most likely faster.
>
> [1] <http://www.php.net/manual/en/language.types.array.php>
As you and others so kindly keep repeating, RTFM!
$array[0][0]="data"
Is 100% valid!
In the arrays manual, it shows the use of bracketed arrays several times.
So why don't you tell them they are wrong?
|
|
|
Re: foreach problem part two [message #184285 is a reply to message #184284] |
Thu, 19 December 2013 23:37   |
|
On 12/19/13, 11:05 PM, richard wrote:
> On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker wrote:
>
>> richard wrote:
>>
>>> There is a flaw in the works that is not discussed.
>>> That being, it won't work with brackted arrays.
>>> Works fine with standard arrays.
>>
>> Nonsense. PHP has only one array type.[1]
>>
>>> While (){}. however works and could care less which is used.
>>
>> Assume, you have an array:
>>
>> $array = array('one', 'two', 'three');
>>
>> Now you want to echo its elements in order. What is simpler and more
>> readable?
>>
>> $i = 0;
>> while ($i < count($array)) {
>> echo $array[$i];
>> }
>>
>> or
>>
>> foreach ($array as $element) {
>> echo $element;
>> }
>>
>> Additionally, the foreach loop is most likely faster.
>>
>> [1] <http://www.php.net/manual/en/language.types.array.php>
>
> As you and others so kindly keep repeating, RTFM!
>
> $array[0][0]="data"
>
> Is 100% valid!
>
> In the arrays manual, it shows the use of bracketed arrays several times.
> So why don't you tell them they are wrong?
>
As far as I can tell, the PHP Manual NEVER calls this a "bracketed
array", and an array created this way is indistinguishable (after
creation) from the array created with the array() operator.
Creating a previously non-existent member by this member in a previously
created array does have some advantages.
I will also note, that you original program had no line like this (at
least that you showed us).
|
|
|
Re: foreach problem part two [message #184286 is a reply to message #184279] |
Fri, 20 December 2013 01:52   |
Arno Welzel
Messages: 317 Registered: October 2011
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
richard, 2013-12-20 00:39:
> On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
>
>> richard <noreply(at)example(dot)com> wrote in news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
>> 40tude.net:
>>
>>> <?php
>>> foreach ($aname as $item){
>>> echo $aname[$item][1];
>>> echo " (".$aname[$item][2].")";
>>> }
>>> ?>
>>>
>>> I decided to create a second array that holds only the artist and number of
>>> records.
>>> So why am I getting "invalid argument" with this?
>>
>> Because you don't understand how foreach() works. RTFM.
>
> I did.
> There is a flaw in the works that is not discussed.
> That being, it won't work with brackted arrays.
> Works fine with standard arrays.
Sigh...
There is no thing like "bracketed arrays" or "standard arrays". This is
called one-dimensional and multi-dimensional. And of course foreach()
works fine with every kind of array.
<?php
$myarray = array(
array( 'Apple', 1 ),
array( 'Pie', 2 )
);
foreach($myarray as $item)
{
echo $item[0] . ' - ' . $item[1] . '<br />';
}
?>
You just don't understand it - as usual.
> While (){}. however works and could care less which is used.
As usual. while() needs more code, is slower and is prone to errors.
Really - software development is not your thing. It seems you are
mentally not capable of understanding the principles.
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
|
|
|
|
|
Re: foreach problem part two [message #184291 is a reply to message #184284] |
Fri, 20 December 2013 07:11   |
Norman Peelman
Messages: 126 Registered: September 2010
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On 12/19/2013 11:05 PM, richard wrote:
> On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker wrote:
>
>> richard wrote:
>>
>>> There is a flaw in the works that is not discussed.
>>> That being, it won't work with brackted arrays.
>>> Works fine with standard arrays.
>>
>> Nonsense. PHP has only one array type.[1]
>>
>>> While (){}. however works and could care less which is used.
>>
>> Assume, you have an array:
>>
>> $array = array('one', 'two', 'three');
>>
>> Now you want to echo its elements in order. What is simpler and more
>> readable?
>>
>> $i = 0;
>> while ($i < count($array)) {
>> echo $array[$i];
>> }
>>
>> or
>>
>> foreach ($array as $element) {
>> echo $element;
>> }
>>
>> Additionally, the foreach loop is most likely faster.
>>
>> [1] <http://www.php.net/manual/en/language.types.array.php>
>
> As you and others so kindly keep repeating, RTFM!
>
> $array[0][0]="data"
>
> Is 100% valid!
>
> In the arrays manual, it shows the use of bracketed arrays several times.
> So why don't you tell them they are wrong?
>
Then maybe you should read it again, specifically "Creating/modifying
with square bracket syntax". That tells you why it isn't working.
However:
$year = "60";
$alist = "art".$year;
$$alist = array(0 => array(0 => "00", 1 => "01"), 1 => array(0 => "10",
1 => "11"));
....is a small example of what you are trying to do.
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
|
|
|
Re: foreach problem part two [message #184292 is a reply to message #184285] |
Fri, 20 December 2013 07:14   |
Norman Peelman
Messages: 126 Registered: September 2010
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On 12/19/2013 11:37 PM, Richard Damon wrote:
> On 12/19/13, 11:05 PM, richard wrote:
>> On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker wrote:
>>
>>> richard wrote:
>>>
>>>> There is a flaw in the works that is not discussed.
>>>> That being, it won't work with brackted arrays.
>>>> Works fine with standard arrays.
>>>
>>> Nonsense. PHP has only one array type.[1]
>>>
>>>> While (){}. however works and could care less which is used.
>>>
>>> Assume, you have an array:
>>>
>>> $array = array('one', 'two', 'three');
>>>
>>> Now you want to echo its elements in order. What is simpler and more
>>> readable?
>>>
>>> $i = 0;
>>> while ($i < count($array)) {
>>> echo $array[$i];
>>> }
>>>
>>> or
>>>
>>> foreach ($array as $element) {
>>> echo $element;
>>> }
>>>
>>> Additionally, the foreach loop is most likely faster.
>>>
>>> [1] <http://www.php.net/manual/en/language.types.array.php>
>>
>> As you and others so kindly keep repeating, RTFM!
>>
>> $array[0][0]="data"
>>
>> Is 100% valid!
>>
>> In the arrays manual, it shows the use of bracketed arrays several times.
>> So why don't you tell them they are wrong?
>>
>
> As far as I can tell, the PHP Manual NEVER calls this a "bracketed
> array", and an array created this way is indistinguishable (after
> creation) from the array created with the array() operator.
>
> Creating a previously non-existent member by this member in a previously
> created array does have some advantages.
>
> I will also note, that you original program had no line like this (at
> least that you showed us).
>
This is not 100% legal syntax because the second [0] acts as a string
offset instead of an index:
$alist[0][0] = "data";
Fatal error: Cannot use string offset as an array... you must use
*array()* to set these up.
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
|
|
|
|
Re: foreach problem part two [message #184295 is a reply to message #184292] |
Fri, 20 December 2013 10:35   |
|
Norman Peelman wrote:
> This is not 100% legal syntax because the second [0] acts as a string
> offset instead of an index:
>
> $alist[0][0] = "data";
> Fatal error: Cannot use string offset as an array... you must use
> *array()* to set these up.
Unsurprisingly, utter nonsense from you again.
$ php -r '$a[0] = 42; var_dump($a["0"]);'
int(42)
In fact,
$alist[0][0] = "data";
is equivalent to
$alist = array(array("data"));
or
$alist = array(0 => array("data"));
or
$alist = array(array(0 => "data"));
or
$alist = array(0 => array(0 => "data"));
or variations of that where the key is the string '0' or "0", if $alist did
not exist before.
It is equivalent to
$alist[0] = array("data");
and variations of that, including those where the keys are '0', if $alist
existed before but did not have an element with key 0.
And it is equivalent to itself if $alist[0] referred to an array before
(i.e., it modifies the element with index 0 of the array that is the element
with index 0 of the outer array).
That you can create „multi-dimensional“ arrays, which can even be
associative, with just one line, is the beauty of PHP.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
|
|
|
Re: foreach problem part two [message #184296 is a reply to message #184284] |
Fri, 20 December 2013 12:14   |
Evan Platt
Messages: 124 Registered: November 2010
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On Thu, 19 Dec 2013 23:05:59 -0500, richard <noreply(at)example(dot)com>
wrote:
> As you and others so kindly keep repeating, RTFM!
>
> $array[0][0]="data"
>
> Is 100% valid!
>
> In the arrays manual, it shows the use of bracketed arrays several times.
> So why don't you tell them they are wrong?
Why don't you submit it as a bug if you're so confident you're right,
bullis?
Then show us the bug link.
Please.
This should be good.
--
To reply via e-mail, remove The Obvious and .invalid from my e-mail address.
|
|
|
Re: foreach problem part two [message #184298 is a reply to message #184279] |
Fri, 20 December 2013 13:37   |
|
On Thu, 19 Dec 2013 18:39:28 -0500, richard wrote:
> On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
>
>> richard <noreply(at)example(dot)com> wrote in
>> news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
>> 40tude.net:
>>
>>> <?php
>>> foreach ($aname as $item){
>>> echo $aname[$item][1];
>>> echo " (".$aname[$item][2].")";
>>> }
>>> ?>
>>>
>>> I decided to create a second array that holds only the artist and
>>> number of records.
>>> So why am I getting "invalid argument" with this?
>>
>> Because you don't understand how foreach() works. RTFM.
>
> I did.
> There is a flaw in the works that is not discussed.
> That being, it won't work with brackted arrays.
> Works fine with standard arrays.
Foreach works with all arrays. At the foreach level, all arrays are one-
dimensional, in that foreach works with the topmost dimension of the
array being processed.
The only flaw is your lack of understanding of how foreach works.
If you read and comprehended the manual entry for foreach, you would
realise this.
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
|
Re: foreach problem part two [message #184300 is a reply to message #184284] |
Fri, 20 December 2013 13:52   |
|
On Thu, 19 Dec 2013 23:05:59 -0500, richard wrote:
> On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker wrote:
>
>> richard wrote:
>>
>>> There is a flaw in the works that is not discussed.
>>> That being, it won't work with brackted arrays.
>>> Works fine with standard arrays.
>>
>> Nonsense. PHP has only one array type.[1]
> As you and others so kindly keep repeating, RTFM!
>
> $array[0][0]="data"
>
> Is 100% valid!
Yes, it's an array of arrays. But it's still just an array of things,
even if each thing is an array.
PHP has a single array type. An array is a collection of things. The
things can be arrays, ints, strings, classes, etc, but the containing
array is just an array of things.
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
Re: foreach problem part two [message #184536 is a reply to message #184281] |
Mon, 06 January 2014 23:13   |
John Smith
Messages: 7 Registered: January 2014
Karma: 0
|
Junior Member |
add to buddy list ignore all messages by this user
|
|
On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker
<cmbecker69(at)arcor(dot)de> wrote:
> richard wrote:
>
>> There is a flaw in the works that is not discussed.
>> That being, it won't work with brackted arrays.
>> Works fine with standard arrays.
>
> Nonsense. PHP has only one array type.[1]
>
>> While (){}. however works and could care less which is used.
>
> Assume, you have an array:
>
> $array = array('one', 'two', 'three');
>
> Now you want to echo its elements in order. What is simpler and more
> readable?
>
> $i = 0;
> while ($i < count($array)) {
> echo $array[$i];
> }
>
> or
>
> foreach ($array as $element) {
> echo $element;
> }
>
> Additionally, the foreach loop is most likely faster.
>
> [1] <http://www.php.net/manual/en/language.types.array.php>
At the first example you don't have to increment the $i?
John
|
|
|
|