473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Creating Arrays

Hi,

I new to php and only started learning it 3 days ago where I was given a 2 hour lecture on it. After the lecture I was asked to:

"create an multidimensional array containing 5 (or more) people, including their name, email address, website URL. They should be identified (i.e. the key of the array) by their nickname. Using this array, create a page that displays name and URL (not email) of every people that have an email containing a ".org""

I am not sure how to go about this and have tried loads of tutorials without success. Could anyone give me any help? Below is the code I have come up with so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php    
  3. $people = array
  4.  
  5. ("Jussy" => array
  6. (
  7. "name" => "Justine Woo",
  8. "email" => "justinewoo@gmail.com",
  9. "ULR" => "http://www.jussywoo.com"),
  10.  
  11. "Mossy" => array
  12. ("name" => "Maurice Fahy",
  13. "email" => "mauricefahy@yahoo.org",
  14. "URL" => "hhtp://www.mossyfahy.com"),
  15.  
  16. "Tom" => array
  17. ("name" => "Thomas Murphy",
  18. "email" => "thomasmurphy@renvue.org",
  19. "URL" => "http://www.tommurphy.ie"),
  20.  
  21. "Cat" => array
  22. ("name" => "Catherine Brennan",
  23. "email" => "catbrennan@students.nuigalway.com",
  24. "URL" => "http://www.catherinebrennan.net"),
  25.  
  26. "Jan" => array
  27. ("name" => "Janis Ennis",
  28. "email" => "janennis@students.dit.org",
  29. "URL" => "http://www.janisennis.ie"),
  30. );
  31.  
  32. foreach ($people as $ppl){
  33. foreach ($ppl as $key => $ppltwo){
  34.  
  35. if ($key == "email"){
  36. $chkemail= $ppltwo;
  37. echo "here is my email:, $chkemail <br/>";
  38.  
  39. }
  40. }
  41. echo $key, $ppltwo, $people;
  42. }
  43. ?>
  44.  
  45.  
I am not sure it is correct but I'm hoping someone can show me a way to create what I have been asked?

Hope you can help :)
Oct 14 '10 #1
8 1797
Markus
6,050 Expert 4TB
Hi there, Mary. Welcome to Bytes :)

Let me show you how I'd do this, and the explanation will be done via comments in the code.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // We'll create our array first. It's always good to 
  4. // use appropriate, descriptive names for variables.
  5. // Also, if we format our code with proper indentation,
  6. // variable-naming schemes, etc., it makes the code
  7. // much easier to read.
  8.  
  9. $people = array (
  10.     'Jussy' => array (
  11.         'name' => 'Justine Woo',
  12.         'email' => 'justinewoo@gmail.com',
  13.         'URL' => 'http://www.jussywoo.com'
  14.     ),
  15.  
  16.     'Mossy' => array (
  17.         'name' => 'Maurice Fahy',
  18.         'email' => 'mauricefahy@yahoo.org',
  19.         'URL' => 'hhtp://www.mossyfahy.com'
  20.     ),
  21.  
  22.     'Tom' => array (
  23.         'name' => 'Thomas Murphy',
  24.         'email' => 'thomasmurphy@renvue.org',
  25.         'URL' => 'http://www.tommurphy.ie'
  26.     ),
  27.  
  28.     'Cat' => array (
  29.         'name' => 'Catherine Brennan',
  30.         'email' => 'catbrennan@students.nuigalway.com',
  31.         'URL' => 'http://www.catherinebrennan.net'
  32.     ),
  33.  
  34.     'Jan' => array(
  35.         'name' => 'Janis Ennis',
  36.         'email' => 'janennis@students.dit.org',
  37.         'URL' => 'http://www.janisennis.ie'),
  38. );
  39.  
  40. // We'll loop over the outer array, i.e $people.
  41. // Remember - descriptive names
  42. foreach ($people as $person)
  43. {
  44.     // We don't need a loop here because we can access the $person's
  45.     // elements directly.
  46.  
  47.     // We'll use substr() to grab the last 4 characters (Top-Level-Domain)
  48.     // of the $person's URL element ($person['URL']).
  49.     $tld = substr($person['URL'], -4);
  50.  
  51.     // Now we can compare the $tld to the string we want to match.
  52.     if ($tld == '.org')
  53.     {
  54.         // We have a .org URL
  55.         echo "Name: {$person['name']}; URL: {$person['URL']}<br />", PHP_EOL;
  56.     }
  57. }
  58.  
Of course, you'll get no output because there are no .org domains in your array ;)
Oct 14 '10 #2
Thank you so much for that it makes sense now. I really appreciate the help :)

I was also asked to:

"Create a function that can convert any amount (in €) to an amount in either £ or $. Then, using this function, create a page listing, in a table, the conversion of any amount from 1€ to 100€ (integer only) to both £ and $."

I have attempted it and I think it is correct. I was hoping you could check it? I have emailed my lecturer asking for help but he has yet to reply. So we are kinda in the dark with php at the moment.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. echo "<table border=\"1\">";
  4. echo "<tr><th>Euro</th>";
  5. echo "<th>USD</th>";
  6. echo "<th>GBP</th></tr>";
  7.  
  8. function currencyconvertor ($a, $currency) {
  9. $a = 1;
  10.  
  11. $dollarrate = 1.39514;
  12. $poundrate = 0.88027;
  13.  
  14.     while ($a <= 100) {
  15.  
  16.     $currency = $dollarrate;
  17.     echo "<tr><td>";
  18.     echo "€";
  19.     echo $a;
  20.     echo "</td><td>";
  21.     echo "$";
  22.     echo $a * $currency;
  23.  
  24.     $currency = $poundrate;
  25.     echo "</td><td>";
  26.     echo "£";
  27.     echo $a * $currency;
  28.     echo "</td></tr>";
  29.     $a = $a + 1;
  30.  
  31. }
  32. }
  33.  
  34. currencyconvertor ($a, $currency);
  35. echo "</table>";
  36.  
  37. ?>
  38.  
Oct 14 '10 #3
oranoos3000
107 100+
hi mary
general syntax for function is as follow as
Expand|Select|Wrap|Line Numbers
  1. function nameOfFunction(parameter list) {
  2. /*
  3. in the part you must body of function that do functionality
  4. */
  5. }
you must obey this general syntax , in body of the function you must write code for logic of function
for more help use www.php.net (php formal site)

be successful
Oct 14 '10 #4
dlite922
1,584 Expert 1GB
I'm not sure what @oranoos3000 was saying but let me clarify the problem for you a little bit and explain how to go about coding it the best way.

functions usually do one or more "functions" or "job", in your case the job of your function should be to convert a Japanese Yen (?) amount to an amount in either English Pound or US Dollar.

So think about what your function needs in order to do this:

1. It needs the currency rate from Yen to Pounds
2. It needs the currency rate from Yen to Dollars
3. It needs to know which currency to convert to (if not both at the same time)
4. And finally, what amount to convert. (integer)

Your function will do the calculation and **Return** an integer.

That's it! that's all your function should do. In your code you have your function outputing html tags. This is a NO-NO! Another thing to point out that you should avoid is use the same variable twice. In your code you used $currency to be equal two separate values when you could have just replaced $currency with $dollarrate and $poundrate in both cases.



So you program should flow like this:

Write HTML header (and table headers) without using PHP. Don't use echo to output HTML. HTML can just stay outside of the <?php tags ?> unless they contain PHP code. In your code the first 4 lines are a waste for PHP.

Then you come to the PHP code, you're while loop, because you need to display multiple rows. Your program should read like this:

For every value between 1 and 100, print the value and get the converted amount to Pound and Dollar.

close PHP?> and continue finishing up your HTML. Here's what it looks like when all these suggestions are implemented:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5.     <table border="1">
  6.         <tr>
  7.             <th>Euro</th>
  8.             <th>USD</th>
  9.             <th>GBP</th>
  10.         </tr>
  11. <?php 
  12.  
  13.  
  14.  
  15. // Convert Function
  16. function currencyconvertor ($a, $currency) {
  17.  
  18.     // Know Static Values
  19.     $dollarrate = 1.39514;
  20.     $poundrate  = 0.88027;
  21.  
  22.     // initiall value
  23.     $value = -1; 
  24.  
  25.     // if currencyType is Pound
  26.     if($currency == 'P')
  27.     {
  28.         // use the pound rate
  29.         $value = $a * $poundrate; 
  30.     }
  31.     elseif($currency == 'D') // if currency type is Dollar
  32.     {
  33.         // calculate value with dollar rate
  34.         $value = $a * $dollarrate; 
  35.     }
  36.  
  37.     // return the value:
  38.     return $value; 
  39. }
  40.  
  41.  
  42.     $a = 1;
  43.     while ($a <= 100) {
  44.  
  45.         // create new row: 
  46.         echo '<tr>'; 
  47.  
  48.         // display YEN cell
  49.         echo "<td>€$a</td>";
  50.  
  51.         // display ollar cells:
  52.         echo '<td>$' . currencyconvertor($a,'D') . '</td>';
  53.  
  54.         // display Pound cell
  55.         echo '<td>£' . currencyconvertor($a,'P') . '</td>';
  56.  
  57.         // close the row
  58.         echo '</tr>'; 
  59.  
  60.         // Increment $a by 1
  61.         $a++;
  62.         // you can combine all of the above into one line. I broke them down for clarity. 
  63.     }
  64.  
  65. // You're done with PHP, so close it
  66. ?>
  67. </table>
  68. </body>
  69. </html>
  70.  
Hope that helped. I think I'd make a better PHP teacher.


Dan
Oct 14 '10 #5
dlite922
1,584 Expert 1GB
If your currency characters are not displaying correctly use the HTML codes:

http://webdesign.about.com/od/locali...lcodes-cur.htm

Replace Pound with &pound;
Replace Euro with &euro;

If you like numbers rounded to the nearest hundredth cents, use the PHP round() function. http://php.net/round and use precision 2.

Cheers,


Dan
Oct 14 '10 #6
Hi Dan,

Thanks for that. I tried out the code you suggested above and it isn't calling the amounts into the table. Is there any extra code that I need to include?

Again thanks for all the help :)

Mary
Oct 14 '10 #7
dlite922
1,584 Expert 1GB
No there's no extra code. I didn't run it yet, so I might have forgotten something. debug it if you need to.

But the idea was for you to understand the code, not copy and paste it as your own.

Dan
Oct 15 '10 #8
Thanks Dan,

I figured it out and got it working. Thanks for your help I really appreciate it
Oct 15 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
10
by: Liz - Newbie | last post by:
Does anyone know how to clear arrays? My C# books talk about creating arrays or talk about using Clear or RemoveAt but these methods don't appear to be available for my array. I have an array...
0
by: John Harvey | last post by:
I have been struggling with the following: I want to create a class that contains an array of some user-defined type. so I have the following vb code: Public Class Communications Public...
5
by: evanburen | last post by:
How would I put 2 strings like this into 2 separate arrays like my examples below? Thanks. Div10,Div11|Div2,Div3,Div8,Div4,Div12,Div1,Div5|Div3,Div5,Div9 France|Germany|Norway var...
1
by: iwl | last post by:
Hello, there is an builtin documented array module in phyton, but no documentation how such an array can be created in C-Extension functions. So I have to use Lists at time, but this is I think...
2
by: Dr Dav | last post by:
Hello all, I'm a physicist whose rewriting a numerical simulation, previously written in IDL, in C with the goal reducing runtime. As you may imagine, my C programming skills are quite poor but I...
35
by: Andrew Fabbro | last post by:
I have a need to create an array that is larger than size_t - 1, which I believe is the largest guaranteed array. I though I'd found salvation in FAQ 6.14, but it appears I am not understanding...
2
by: xxsodapopxx5 | last post by:
Hi everyone, I am new to C# and am having problems creating arrays like i did in VB.net. Right now i have a structure lets say called stuBox and it has a few items in it struct stuBox ...
10
by: fmsguy06 | last post by:
I have the numbers 1 through 50 in one line on a text file, in random order. I am trying to read that text file into an array, so as to copy the array and sort it. I can copy and sort arrays fine,...
1
by: MCaron | last post by:
Can I use the values in an access table or query to populate an array that I want to use as part of a module?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.