Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help creating an array (newbie)
Old 09-10-2009, 01:59 AM Help creating an array (newbie)
Experienced Talker

Posts: 38
Name: jim
Trades: 0
I am trying to create a multi-dimensional array...

PHP Code:
<?php // Script 4.6 - bv_03.php

// $my_array = array;
// (doesn't work)

for ($i 1$i <= 10$i++) {
    
$time $i 0.5;
    
$other1 rand (7080);
    
$other2 rand (4050);    
    
$my_array[$i]['time'] = $time;
    
$my_array[$i]['otherdata1'] = $other1;
    
$my_array[$i]['otherdata2'] = $other2;

print 
"<br />  
<p>
$my_array[$i]['time'] and
    
$my_array[$i]['otherdata1'] and
    
$my_array[$i]['otherdata2'] 
</p>"
;

}
?>
Any help would be appreciated.
jwriter is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-10-2009, 02:47 AM Re: Help creating an array (newbie)
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You almost got it right. You just missed to initiate the array. Before the for loop, add this

PHP Code:
$my_array = array(); 
An just in case you didn't know, the "normal" behavior of an array is to start counting from index 0, not 1. So you might want to change the loop to

PHP Code:
for ($i 0$i 10$i++) {
   
$time = ($i 1) * 0.5
Of course your way works aswell, but it's more common to start from 0.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 09-10-2009, 04:16 AM Re: Help creating an array (newbie)
Experienced Talker

Posts: 38
Name: jim
Trades: 0
Thank you - I really appreciate the help. I initiated the array, but the loop is returning 10 lines of the following...

Array['time'] and Array['otherdata1'] and Array['otherdata2']

so I think there is something else wrong.
jwriter is offline
Reply With Quote
View Public Profile
 
Old 09-10-2009, 10:40 AM Re: Help creating an array (newbie)
Average Talker

Posts: 24
Location: Germany
Trades: 0
I think you already created the array you want. The output is the problem.
PHP Code:
<?php // Script 4.6 - bv_03.php

// $my_array = array;
// (doesn't work)

for ($i 0$i 10$i++) {
    
$time $i 0.5;
    
$other1 rand (7080);
    
$other2 rand (4050);    
    
$my_array[$i]['time'] = $time;
    
$my_array[$i]['otherdata1'] = $other1;
    
$my_array[$i]['otherdata2'] = $other2;

    print 
"<br /><p>".$my_array[$i]['time']." and ".$my_array[$i]['otherdata1']." and ".$my_array[$i]['otherdata2']." </p>";

}
?>
It should work with this code. (At least it did on my local webserver...)
__________________

Please login or register to view this content. Registration is FREE
- Register Now and Share Your Thoughts to Create Innovation and Wisdom


Please login or register to view this content. Registration is FREE
- Psychology, Philosophy and Self Development


Please login or register to view this content. Registration is FREE
- Read About All Those Things That Bug Me

Last edited by Trey Walter; 09-10-2009 at 10:43 AM.. Reason: PHP tags work better for PHP code than normal CODE tags^^
Trey Walter is offline
Reply With Quote
View Public Profile Visit Trey Walter's homepage!
 
Old 09-10-2009, 11:21 AM Re: Help creating an array (newbie)
Experienced Talker

Posts: 38
Name: jim
Trades: 0
Trey, the new print statement works well. Thanks!

I don't see any explanation in the manuals of the following construction...

Code:
".$my_array[$i]['time']."
Is this a way of nesting quotes within quotes?
jwriter is offline
Reply With Quote
View Public Profile
 
Old 09-10-2009, 11:40 AM Re: Help creating an array (newbie)
Average Talker

Posts: 24
Location: Germany
Trades: 0
Quote:
Originally Posted by jwriter View Post
Trey, the new print statement works well. Thanks!

I don't see any explanation in the manuals of the following construction...

Code:
".$my_array[$i]['time']."
Is this a way of nesting quotes within quotes?
You're welcome.

'.' is a string operator, here is a link to the manual: PHP String Operators
My construction is just a shorter way of writing something like this:
Code:
print "<br /><p>";
print $my_array[$i]['time'];
print " and ";
...
__________________

Please login or register to view this content. Registration is FREE
- Register Now and Share Your Thoughts to Create Innovation and Wisdom


Please login or register to view this content. Registration is FREE
- Psychology, Philosophy and Self Development


Please login or register to view this content. Registration is FREE
- Read About All Those Things That Bug Me
Trey Walter is offline
Reply With Quote
View Public Profile Visit Trey Walter's homepage!
 
Reply     « Reply to Help creating an array (newbie)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.58013 seconds with 12 queries