• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

An array question

The Red Guy

Dork
NLC
Just asking:

When I use an array, is it adviceable to use 1=> inside it? Some books discourage it, while others encourage it.
 
You mean is it 'advicable' to use Associate arrays?

Well, I like them better becuase you have a more memorable key, so instead of 1, you could do like, name or something... But the only draw back is that you can't loop through them as easily as a for loop - but you still can.


-- OR --

Do you mean is it 'advicable' to create your arrays like that? It doesn't really matter. Personal preferance I guess. I define all of my arrays like this:

$myvar[name] = "blah";
$myvar = "blah";

etc. I find it easier to read that then:

$myvar("name"=>"blah"
"email"=>"blah");


But, is that your question?
 
Originally posted by Christopher
You mean is it 'advicable' to use Associate arrays?

Well, I like them better becuase you have a more memorable key, so instead of 1, you could do like, name or something... But the only draw back is that you can't loop through them as easily as a for loop - but you still can.


-- OR --

Do you mean is it 'advicable' to create your arrays like that? It doesn't really matter. Personal preferance I guess. I define all of my arrays like this:

$myvar[name] = "blah";
$myvar = "blah";

etc. I find it easier to read that then:

$myvar("name"=>"blah"
"email"=>"blah");


But, is that your question? [/B][/QUOTE]
The first. But I hear from a book that they discourage but they won't give reasons.
 
Mabe something to do with reources or speed... Or might be just like I said...

Instead of doing this to loop though normal array:

Code:
$array("1","2","3");
$num = count($array);

for($i=0; $i<$num; $i++)
{
  print $array[$i];
{

You have to use something less understandable (to a beginner):

Code:
$array("item1"=>1,
            "item2"=>2,
            "item3"=>3);

while($data = each($array))
{
  print $array["value"];
}

Although the two have the same output, the first is more easily read...
 
Originally posted by The Red Guy
Yup, understood. But what good is an array anyway? I dont seem to find any practical use for it.

Arrays are just a robust form of variables, so if you find variables useful (or necessary), then arrays can only be more so :)
 
Thanks for that, and can someone tell me what's the difference between -> and => when pointing to a function or something? I've never seen it covered in books before.
 
You meen with Classes? Thats the only thing I know of with functions that uses ->, my book only says that.
Code:
class A
{
  function funcA($text = "Hello World")
  {
    print $text;
  }
}

$a = new A();
$a->funcA("Yayayayayayayayayayayay");
For an example.

But I don't think thats your question...
 
Originally posted by The Red Guy
Yup, understood. But what good is an array anyway? I dont seem to find any practical use for it.

Well, if your in to mySQL yet, I find the mysql_fetch_array the best function in the world. That gets the data from the database and turns it into an array so you can print it or manipulate it.

So one practicle use for them. Also, lets say you had a huge site of links, instead of just having variables, you could categorize them:
Code:
$game[gamerz] = "http://site.com";
$game[ps2] = "http://site.com";

$webmaster[fws] = "http://freewebspace.net";
$webmaster[html] = "http://htmlgoodies.com";

// LOL
$warez[site1] = "http://site.com";
$warez[site2] = "http://pieceofcrap.com";
 
> But what good is an array anyway? I dont seem to find any practical use for it.

You don't think phpBB, iBF, vBulletin, etc are made without using arrays, do you?

Arrays are good for storing large amounts of data (usually related, in a single variable), instead of creating tons of variables that contain a single string/int/whatever.

PHP:
// For example, why do the following:

$blue_dog = 'is pretty';
$red_dog = 'is mean';
$orange_dog = 'is radiant';
$yellow_dog = 'is bright';

// ... and so on, when you can create an array that contains
// the same data?

$dog = array(
  'blue' => 'pretty',
  'red' => 'mean',
  'orange' => 'radiant',
  'yellow' => 'bright'
);
Also, you can use a loop function to do something to whatever data you have in the array. (This stuff was already pointed out, just giving more examples).

PHP:
// going to print out a simple sentence with the $dog array
// resulting sentences will look like "The blue dog is pretty".

foreach($dog as $key => $value)
{
  echo 'The ' . $key . ' dog is ' . $value;
}

// will print out "The [i]somecolor[/i] dog is [i]somedescription[/i]
// for each key and it's associated value from the array.

// if you are not familiar with the foreach() function, the following
// will do the same thing... but it is slower.

while( list($key, $value) = each($dog) )
{
  echo 'The ' . $key . ' dog is ' . $value;
}
As far as why people use "array($k => $v)" instead of "$var[key] = value" ... well, typically, it's a performance thing. The "array($k => $v)" method is faster.

Miscellaneous:
PHP:
// here's how you can use an array, applying it to something 
// people here ask often (how to make index.php?page=somepage links),
// while ensuring your code is safe.
// FYI, IBF uses code like the following in their scripts...

$pages = array(
  'index' => 'index.html',
  'links' => 'links.html',
  'downloads' => 'downloads.html',
  'aboutme' => 'about.html'
); // the pages a user may visit, and the associated file to include

// what the user has chosen (e.g. index.php?page=aboutme)
$choice = ( !empty($_GET['page']) ) ? $_GET['page'] : 'index';

// verify that the chosen "page" exists in the $pages array...
if( !isset($pages[$choice]) )
{
  $choice = 'index';
}

// include the file (e.g. about.html) associated with the chosen "page"
include('./' . $pages[$choice]);
 
Last edited:
Yes.

PHP:
$images("golf" => "golf.gif",
               "baseball" => "b-ball.gif";
               "soccer" => "soccer.jpg";
               "football" => "football.png");

print "<img src=\"".$images[soccer]."\">";
 
> Can I add a hyperlink to an array?

You can store many things in an array.

> Is is adviceable to use an array in an array (i dunno what
> name).

Yes, it's perfectly fine to store an array within an array. They're called multidimensional arrays - but the name is misleading. They're not multidimensional, but rather "flat" arrays stored in another "flat" array. I've come to think of a multidimesional array as a file directory (with subdirectories in it).

PHP:
$variable = array(
	'dog' => 'ugly',
	'farm' => array(
		'chicken' => 'KFC',
		'cow' => 'makes milk',
		'other' => 'blah'
		),
	'cats' => 'stupid',
);

// will print neatly formatted information about the array
//
print_r($variable);

What will the value of $variable['farm']['chicken'] be?
 
I think of a 2 dimensional array as a chart or spreadsheet.

array[row][column]

3 dimensional would be like a 3D chart, where each cell in the 2D chart has been expanded to be another row.

I couldn't see a use for much larger multi-dimensional arrays, but I guess the file directory idea makes sense. Each "dimension" adds another sub folder.
 
Back
Top