tags:

views:

50

answers:

3

I'm not 100% but this ($settings) would be called an array in php:

$setting;

$setting['host'] = "localhost";
$setting['name'] = "hello";

but what's the name for this that's different to the above:

$settings = array("localhost", "hello");

Also from the first example how can i remove the element called name?

(please also correct my terminology if I have made a mistake)

+3  A: 

I'm not 100% but this ($settings) would be called an array in php:

You should be 100% sure, they are :)

but what's the name for this that's different to the above:

This:

$setting['host'] = "localhost";
$setting['name'] = "hello";

And this are different ways of declaring a php array.

$settings = array("localhost", "hello");

In fact this is how later should be to match the first one with keys:

$settings = array("host" => "localhost", "name" => "hello");

Also from the first example how can i remove the element called name?

You can remove with unset:

unset($setting['name']);

Note that when declaring PHP array, do:

$setting = array();

Rather than:

$setting;

Note also that you can append info to arrays at the end by suffixing them with [], for example to add third element to the array, you could simply do:

$setting[] = 'Third Item';

More Information:

http://php.net/manual/en/language.types.array.php

Sarfraz
+2 if I could... great response.
Timothy
+2  A: 

As sAc said, they are both array. The correct way to declare an array is $settings = array(); (as opposed to just $settings; in your first line.)

The main difference between the first and second way is that the first allows you to use $settings['host'] and $settings['name'], whereas the latter can only be used with numeric indices ($settings[0] and $settings[1]). If you want to use the first way, you can also declare your array like this: $settings = array('host'=>'localhost', 'name'=>'hello');

More reading on PHP arrays

Lex
+1  A: 

Well this is indeed an array. You have different types of array's in php. The first example you mention is called an Associative Array. Simply an array with a string as a key.

An associative array can be declared in two ways:

1) (the way you declared it):

$sample = array();
$sample["name"] = "test";

2)

$sample = array("name" => "localhost");

Furthermore the first example can also be used to add existing items to an array. For example:

$sample["name"][] = "some_name";
$sample["name"][] = "some_other_name";

When you execute the above code with print_r($sample) you get something like:

Array ( [name] => Array ( [0] => some_name [1] => some_other_name ) )

Which is very usefull when adding multiple strings to an existing array.

Removing a value from an array is very simple,

Like mentioned above, use the unset function.

unset($sample["name"]) 

to unset the whole name value and values connected to it Or when you only want to unset a specific item within $sample["name"] :

unset($sample["name"][0]);

or, ofcourse any item you'd like.

So basicly.. the difference between your first example and the latter is that the first is an associative array, and the second is not.

For further reference on arrays, visit the PHP manual on arrays

Stephen