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

Home » Imported messages » comp.lang.php » returning values from a 2d array
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
returning values from a 2d array [message #180633] Thu, 07 March 2013 15:49 Go to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
I have been searching the web for an answer but can't really find what I
want to know.

How do I retrieve the values from an array once the random number has been
generated?
In BASIC, one would simply say: show$=top$(1960,1).
In PHP though, it seems you have to go through a miriad of loops just to
find out if the value exists.

So how is this done properly?


<?php

anum=rand(1,40);
$num=anum
$number="0" . $num;
if ($num<10) {$number="0" . $number;}
if (Snum>=100) {$number=$num;}

$top[1960][0]= array("top 100 1960");
$top[1960][1]= array("Theme From A Summer Place "," Go-Go-Po-Go "," Percy
Faith "," Columbia 41490 "," Composer: Max Steiner"," Arranger: ","
Producer: "," Composer: Percy Faith "," Arranger: "," Producer: ");
$top[1960][2]= array("He'll Have To Go "," In A Mansion Stands My Love ","
Jim Reeves "," RCA Victor 7643 "," Composer: J. Allison, A. Allison ","
Arranger: "," Producer: Chet Atkins "," Composer: Johnny Russell ","
Arranger: "," Producer: Chet Atkins ");
$top[1960][3]= array("Cathy's Clown "," Always It's You "," Everly Brothers
"," Warner Bros. 5151 "," Composer: Don Everly, Phil Everly "," Arranger:
"," Producer: "," Composer: B. Bryant, F. Bryant "," Arranger: ","
Producer: ");
$top[1960][4]= array("Running Bear "," My Heart Knows "," Johnny Preston
"," Merucry 71474 "," Composer: J. P. Richardson "," Arranger: ","
Producer: "," Composer: J. P. Richardson, J. Preston "," Arranger: ","
Producer: ");
$top[1960][5]= array("Teen Angel "," Bye Now Baby "," Mark Dinning "," MGM
12845 "," Composer: J. Surrey, R. Surrey "," Arranger: "," Producer: ","
Composer: Bryant "," Arranger: "," Producer: ");
Re: returning values from a 2d array [message #180634 is a reply to message #180633] Thu, 07 March 2013 16:26 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/7/2013 10:49 AM, richard wrote:
> I have been searching the web for an answer but can't really find what I
> want to know.
>
> How do I retrieve the values from an array once the random number has been
> generated?
> In BASIC, one would simply say: show$=top$(1960,1).
> In PHP though, it seems you have to go through a miriad of loops just to
> find out if the value exists.
>
> So how is this done properly?
>
>
> <?php
>
> anum=rand(1,40);
> $num=anum
> $number="0" . $num;
> if ($num<10) {$number="0" . $number;}
> if (Snum>=100) {$number=$num;}
>
> $top[1960][0]= array("top 100 1960");
> $top[1960][1]= array("Theme From A Summer Place "," Go-Go-Po-Go "," Percy
> Faith "," Columbia 41490 "," Composer: Max Steiner"," Arranger: ","
> Producer: "," Composer: Percy Faith "," Arranger: "," Producer: ");
> $top[1960][2]= array("He'll Have To Go "," In A Mansion Stands My Love ","
> Jim Reeves "," RCA Victor 7643 "," Composer: J. Allison, A. Allison ","
> Arranger: "," Producer: Chet Atkins "," Composer: Johnny Russell ","
> Arranger: "," Producer: Chet Atkins ");
> $top[1960][3]= array("Cathy's Clown "," Always It's You "," Everly Brothers
> "," Warner Bros. 5151 "," Composer: Don Everly, Phil Everly "," Arranger:
> "," Producer: "," Composer: B. Bryant, F. Bryant "," Arranger: ","
> Producer: ");
> $top[1960][4]= array("Running Bear "," My Heart Knows "," Johnny Preston
> "," Merucry 71474 "," Composer: J. P. Richardson "," Arranger: ","
> Producer: "," Composer: J. P. Richardson, J. Preston "," Arranger: ","
> Producer: ");
> $top[1960][5]= array("Teen Angel "," Bye Now Baby "," Mark Dinning "," MGM
> 12845 "," Composer: J. Surrey, R. Surrey "," Arranger: "," Producer: ","
> Composer: Bryant "," Arranger: "," Producer: ");
>

$top is a three dimensional array and would be indexed as
$top[$x][$y][$z]. For instance, "Theme From a Summer Place" would be
$top[1960][1][0] and "Composer: J. P. Richardson" would be
$top[1960][4][4].

I think you're going about this the wrong way, but hey - it's your code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: returning values from a 2d array [message #180635 is a reply to message #180634] Thu, 07 March 2013 16:31 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
On Thu, 07 Mar 2013 11:26:18 -0500, Jerry Stuckle wrote:

> On 3/7/2013 10:49 AM, richard wrote:
>> I have been searching the web for an answer but can't really find what I
>> want to know.
>>
>> How do I retrieve the values from an array once the random number has been
>> generated?
>> In BASIC, one would simply say: show$=top$(1960,1).
>> In PHP though, it seems you have to go through a miriad of loops just to
>> find out if the value exists.
>>
>> So how is this done properly?
>>
>>
>> <?php
>>
>> anum=rand(1,40);
>> $num=anum
>> $number="0" . $num;
>> if ($num<10) {$number="0" . $number;}
>> if (Snum>=100) {$number=$num;}
>>
>> $top[1960][0]= array("top 100 1960");
>> $top[1960][1]= array("Theme From A Summer Place "," Go-Go-Po-Go "," Percy
>> Faith "," Columbia 41490 "," Composer: Max Steiner"," Arranger: ","
>> Producer: "," Composer: Percy Faith "," Arranger: "," Producer: ");
>> $top[1960][2]= array("He'll Have To Go "," In A Mansion Stands My Love ","
>> Jim Reeves "," RCA Victor 7643 "," Composer: J. Allison, A. Allison ","
>> Arranger: "," Producer: Chet Atkins "," Composer: Johnny Russell ","
>> Arranger: "," Producer: Chet Atkins ");
>> $top[1960][3]= array("Cathy's Clown "," Always It's You "," Everly Brothers
>> "," Warner Bros. 5151 "," Composer: Don Everly, Phil Everly "," Arranger:
>> "," Producer: "," Composer: B. Bryant, F. Bryant "," Arranger: ","
>> Producer: ");
>> $top[1960][4]= array("Running Bear "," My Heart Knows "," Johnny Preston
>> "," Merucry 71474 "," Composer: J. P. Richardson "," Arranger: ","
>> Producer: "," Composer: J. P. Richardson, J. Preston "," Arranger: ","
>> Producer: ");
>> $top[1960][5]= array("Teen Angel "," Bye Now Baby "," Mark Dinning "," MGM
>> 12845 "," Composer: J. Surrey, R. Surrey "," Arranger: "," Producer: ","
>> Composer: Bryant "," Arranger: "," Producer: ");
>>
>
> $top is a three dimensional array and would be indexed as
> $top[$x][$y][$z]. For instance, "Theme From a Summer Place" would be
> $top[1960][1][0] and "Composer: J. P. Richardson" would be
> $top[1960][4][4].
>
> I think you're going about this the wrong way, but hey - it's your code.

Not according to the PHP manual.
I have seen the format I'm using online and have tested it.
I tried using $a=$top[1960][anum] and the output was "Array".
Re: returning values from a 2d array [message #180636 is a reply to message #180633] Thu, 07 March 2013 16:48 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 07.03.2013 16:49, schrieb richard:
> I have been searching the web for an answer but can't really find what I
> want to know.
>
> How do I retrieve the values from an array once the random number has been
> generated?
> In BASIC, one would simply say: show$=top$(1960,1).
> In PHP though, it seems you have to go through a miriad of loops just to
> find out if the value exists.
>
> So how is this done properly?
>
>
> <?php
>
> anum=rand(1,40);
Error, missing $

> $num=anum
Error, missing ;
> $number="0" . $num;
Looks like nonsens.
> if ($num<10) {$number="0" . $number;}
> if (Snum>=100) {$number=$num;}
>
> $top[1960][0]= array("top 100 1960");
> $top[1960][1]= array("Theme From A Summer Place "," Go-Go-Po-Go "," Percy
> Faith "," Columbia 41490 "," Composer: Max Steiner"," Arranger: ","
> Producer: "," Composer: Percy Faith "," Arranger: "," Producer: ");

What are you trying to do? Pick one song randomly?

Then look at your array:

echo $top[1960][1][0] would give "Theme From A Summer Place"

Just put rand(0,9) as index of the third level, and the rest ... don't know what you
want.

And it is quite near to PHP array abuse, there must be a better representation.

/Str.
Re: returning values from a 2d array [message #180637 is a reply to message #180635] Thu, 07 March 2013 16:50 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 07.03.2013 17:31, schrieb richard:
> On Thu, 07 Mar 2013 11:26:18 -0500, Jerry Stuckle wrote:

>> I think you're going about this the wrong way, but hey - it's your code.
>
> Not according to the PHP manual.
> I have seen the format I'm using online and have tested it.
> I tried using $a=$top[1960][anum] and the output was "Array".

What is anum supposed to be? It is not an index. Turn on all error reporting options.

/Str.
Re: returning values from a 2d array [message #180638 is a reply to message #180635] Thu, 07 March 2013 17:02 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 07.03.2013 17:31, schrieb richard:
> On Thu, 07 Mar 2013 11:26:18 -0500, Jerry Stuckle wrote:
>
>> On 3/7/2013 10:49 AM, richard wrote:
>>> I have been searching the web for an answer but can't really find what I
>>> want to know.
>>>
>>> How do I retrieve the values from an array once the random number has been
>>> generated?
>>> In BASIC, one would simply say: show$=top$(1960,1).
>>> In PHP though, it seems you have to go through a miriad of loops just to
>>> find out if the value exists.
>>>
>>> So how is this done properly?
>>>
>>
>> $top is a three dimensional array and would be indexed as
>> $top[$x][$y][$z]. For instance, "Theme From a Summer Place" would be
>> $top[1960][1][0] and "Composer: J. P. Richardson" would be
>> $top[1960][4][4].
>>
>> I think you're going about this the wrong way, but hey - it's your code.
>
> Not according to the PHP manual.
> I have seen the format I'm using online and have tested it.
> I tried using $a=$top[1960][anum] and the output was "Array".
>

Listen to what people are telling you, or stop asking. Of course $top[1960][$anum] is
an array, you assigned it yourself:

$top[1960][2]= array("He'll H....

You have 3 index levels.

/Str.
Re: returning values from a 2d array [message #180639 is a reply to message #180636] Thu, 07 March 2013 17:04 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
On Thu, 07 Mar 2013 17:48:16 +0100, M. Strobel wrote:

> Am 07.03.2013 16:49, schrieb richard:
>> I have been searching the web for an answer but can't really find what I
>> want to know.
>>
>> How do I retrieve the values from an array once the random number has been
>> generated?
>> In BASIC, one would simply say: show$=top$(1960,1).
>> In PHP though, it seems you have to go through a miriad of loops just to
>> find out if the value exists.
>>
>> So how is this done properly?
>>
>>
>> <?php
>>
>> anum=rand(1,40);
> Error, missing $
>
>> $num=anum
> Error, missing ;
>> $number="0" . $num;
> Looks like nonsens.
>> if ($num<10) {$number="0" . $number;}
>> if (Snum>=100) {$number=$num;}
>>
>> $top[1960][0]= array("top 100 1960");
>> $top[1960][1]= array("Theme From A Summer Place "," Go-Go-Po-Go "," Percy
>> Faith "," Columbia 41490 "," Composer: Max Steiner"," Arranger: ","
>> Producer: "," Composer: Percy Faith "," Arranger: "," Producer: ");
>
> What are you trying to do? Pick one song randomly?
>
> Then look at your array:
>
> echo $top[1960][1][0] would give "Theme From A Summer Place"
>
> Just put rand(0,9) as index of the third level, and the rest ... don't know what you
> want.
>
> And it is quite near to PHP array abuse, there must be a better representation.
>
> /Str.

the randomizing feature works quite well.
www.mroldies.net/playertest.php
which randomly selects one of 40 songs each time the page is reloaded.

what I am wanting to do is, print the information for the song below the
player.
Re: returning values from a 2d array [message #180641 is a reply to message #180635] Thu, 07 March 2013 19:41 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/7/2013 11:31 AM, richard wrote:
> On Thu, 07 Mar 2013 11:26:18 -0500, Jerry Stuckle wrote:
>
>> On 3/7/2013 10:49 AM, richard wrote:
>>> I have been searching the web for an answer but can't really find what I
>>> want to know.
>>>
>>> How do I retrieve the values from an array once the random number has been
>>> generated?
>>> In BASIC, one would simply say: show$=top$(1960,1).
>>> In PHP though, it seems you have to go through a miriad of loops just to
>>> find out if the value exists.
>>>
>>> So how is this done properly?
>>>
>>>
>>> <?php
>>>
>>> anum=rand(1,40);
>>> $num=anum
>>> $number="0" . $num;
>>> if ($num<10) {$number="0" . $number;}
>>> if (Snum>=100) {$number=$num;}
>>>
>>> $top[1960][0]= array("top 100 1960");
>>> $top[1960][1]= array("Theme From A Summer Place "," Go-Go-Po-Go "," Percy
>>> Faith "," Columbia 41490 "," Composer: Max Steiner"," Arranger: ","
>>> Producer: "," Composer: Percy Faith "," Arranger: "," Producer: ");
>>> $top[1960][2]= array("He'll Have To Go "," In A Mansion Stands My Love ","
>>> Jim Reeves "," RCA Victor 7643 "," Composer: J. Allison, A. Allison ","
>>> Arranger: "," Producer: Chet Atkins "," Composer: Johnny Russell ","
>>> Arranger: "," Producer: Chet Atkins ");
>>> $top[1960][3]= array("Cathy's Clown "," Always It's You "," Everly Brothers
>>> "," Warner Bros. 5151 "," Composer: Don Everly, Phil Everly "," Arranger:
>>> "," Producer: "," Composer: B. Bryant, F. Bryant "," Arranger: ","
>>> Producer: ");
>>> $top[1960][4]= array("Running Bear "," My Heart Knows "," Johnny Preston
>>> "," Merucry 71474 "," Composer: J. P. Richardson "," Arranger: ","
>>> Producer: "," Composer: J. P. Richardson, J. Preston "," Arranger: ","
>>> Producer: ");
>>> $top[1960][5]= array("Teen Angel "," Bye Now Baby "," Mark Dinning "," MGM
>>> 12845 "," Composer: J. Surrey, R. Surrey "," Arranger: "," Producer: ","
>>> Composer: Bryant "," Arranger: "," Producer: ");
>>>
>>
>> $top is a three dimensional array and would be indexed as
>> $top[$x][$y][$z]. For instance, "Theme From a Summer Place" would be
>> $top[1960][1][0] and "Composer: J. P. Richardson" would be
>> $top[1960][4][4].
>>
>> I think you're going about this the wrong way, but hey - it's your code.
>
> Not according to the PHP manual.
> I have seen the format I'm using online and have tested it.
> I tried using $a=$top[1960][anum] and the output was "Array".
>

Your code does not match what's in the manual. That's why you get
"Array" as the output.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: returning values from a 2d array [message #180645 is a reply to message #180633] Thu, 07 March 2013 22:00 Go to previous message
Alastair Black is currently offline  Alastair Black
Messages: 3
Registered: March 2013
Karma: 0
Junior Member
On 03/07/2013 08:49 AM, richard wrote:
> I have been searching the web for an answer but can't really find what I
> want to know.
>
> How do I retrieve the values from an array once the random number has been
> generated?
> In BASIC, one would simply say: show$=top$(1960,1).
> In PHP though, it seems you have to go through a miriad of loops just to
> find out if the value exists.
>
> So how is this done properly?
>
>
> <?php
>
> anum=rand(1,40);
> $num=anum
> $number="0" . $num;
> if ($num<10) {$number="0" . $number;}

Check your typing for '$' designating variables. This is
also important when writing BASIC code. anum -> $anum

Check your typing for semi-colons. This is
also important when writing BASIC code. $num=anum -> $num=$anum;

Check 'type'-ing to see that you are not asking to concatenate numeric
variables onto character strings. Leading zero suppression is leading
zero suppression, regardless how many zeroes you put in front.

Alastair
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Need help with updating ereg_replace function -> to preg_replace
Next Topic: Bowling
Goto Forum:
  

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

Current Time: Fri Nov 29 23:51:58 GMT 2024

Total time taken to generate the page: 0.03419 seconds