Jump to content
MakeWebGames

[FAQ] Arrays Examples


Haunted Dawg

Recommended Posts

Ok i am not the greatest at explaining how array's work and such. I know what they do and capable of and that is all i need to know.

I made a small thing describing and showing array's. Some of you might find usefull.

Enjoy. Do not critizise if i am wrong in place's with naming the array's. Correct me rather than be a critic.

You can read through this php file & upload it to your game and go to the page to look at how the example's come out.

Another thing. I made a function called rprintr because normaly print_r without <pre> around it, is just not as readable with <pre> & </pre> around it. Some of you should know what i mean.

So this function already add's the <pre> & </pre> around the print_r function so now we do not have more line's lower down in the code.

Please keep negative feedback away from my thread and post it to pm. This is because i am not good at making FAQ's but i thought i would try and help those one's that are trying to learn out.

So here it is:

 

<?php
ob_start();
echo '<title>Example of array\'s</title>';

function rprintr($ray)
{
   echo '<pre>';
   print_r($ray);
   echo '</pre>';
}

/*

* Example of array's and multidimensional array's.

* Arrays:

   1.- You can add alot to an array and call all the information you need using for() function. REF => [url]http://www.php.net/for[/url]

   2.- An array is where you can store multiple information such as number's, letter's, character's.
   3.- If you are formal with mysql_fetch_assoc or mysql_fetch_array well those perfom as a array.

   4.- An example of an array:

       $raw_1 = array
       (
           1,2,3,4
       );

   4.1- Better array with meaning vars:

       $raw_2 = array
       (
           1 => "VAR",
           2 => "RAW",
           3 => "EG",
           4 => "TEST"
       );

       * Note *     here we are using a =>. when calling out which ever number for array( raw_2 ) you do not need to start at 0 to get the first variable.
               so we use  $raw_2[1] which will then display "VAR". if you are using letter's as your sequal it would be like this:

               $raw_2_b = array
               (
                   "row" = "VAR"
               );


* Multidimensional Array's:

   1.- It is the same as array's but this consist's of array's in array's for example:

   $raw_3 = array
   (
       1     =>     array
               (
                   "Name"     => "Kyle",
                   "Age"    => 16
               ),
       2    =>    array
               (
                   "Name"     => "Bot",
                   "Age"    => 913463214
               )
   );

* If need help just ask

*/




//Example of array raw_1:

echo '[b]Example or normal Arrays:[/b]';
$raw_1 = array
(
   1,2,3,4
);
rprintr ($raw_1);
$val = count($raw_1);
for ($i = 0; $i <= $val; $i++)
{
   echo $raw_1[$i].'
';
}

//Example of array with vars

echo '

[b]Example of arrays with vars:[/b]';
$raw_2 = array
(
   1 => "VAR",
   2 => "RAW",
   3 => "EG",
   4 => "TEST"
);
rprintr ($raw_2);
$val_n = count($raw_2);
for ($p = 1; $p <= $val_n; $p++)
{
   echo $raw_2[$p].'
';
}

//Example of multidimensional arrays

echo '

[b]Example of multidimensional arrays:[/b]';
$raw_3 = array
(
   1     =>     array
           (
               "Name"     => "Kyle",
               "Age"     => 16
           ),
   2    =>    array
           (
               "Name"     => "Bot",
               "Age"    => 923432
           )
);
rprintr ($raw_3);
$val_b = count($raw_3);
for ($r = 1; $r <= $val_b; $r++)
{
   echo 'Name: '.$raw_3[$r]['Name'].'
Age: '.$raw_3[$r]['Age'].'<hr>';
}
?>

 

Enjoy.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...