Array

  • Indexed Array:

$arr = array(“…”, “…”, “…”); / $arr = []; OR $arr[no] = “…”;

Append an Item: $arr[] = newValue;

To Loop through an indexed array:

For Loop: for($x = 0; $x < $arrlength; $x++) { echo $arr[$x]; }

Foreach Loop: foreach ($arr as $value) { echo “$value <br / >”; }

  • Associative Arrays:

$arr = array(“object1” => “…”, “object2” => “…”); / $arr[“object1”] = “…”;

Access an Item: $arr[“object1”];

Append an Item: $arr[“newObject”] = newValue;

To Loop through an associative array: 

foreach ($arr as $key => $value) { echo “$key: $value <br / >”; }

  • Multidimensional Arrays:

$arr = array(array(“…”, “…”), array(“…”, “…”), array(“…”, “…”)); OR

$arr = array(“object1” => array(“…”, “…”), “object2” => array(“…”, “…”));

Access Data: $arr[“object1”][indexNumber];

To Loop through a Multidimensional Arrays:

for($row=0; $row < 4; $row++) {

for($col=0; $col < 3; $col++){

echo $arr[$row][$col];

}

}

  • Display:

echo $arr[no]; – to output a single item

print_r($arr); – to output the array

Add on the end: $arr[ ]= “…”;

Remove an element: unset($arr[“objectno“]);

$key: position number                              $value: value of the object

count($arr) – returns the number of elements of an array

implode(‘ ’, $arr) – joins all elements into a string

shuffle($arr) – changes the array in place to a random order

  • Sort Functions for Arrays:

sort() – sort arrays in ascending order

rsort() – sort arrays in descending order

asort() – sort associative arrays in ascending order, according to the value

ksort() – sort associative arrays in ascending order, according to the key

arsort() – sort associative arrays in descending order, according to the value

krsort() – sort associative arrays in descending order, according to the key

Advertisements

About bernadetteengleman

Hello, I am Bernadette from New York City. I moved here from Chicago in 2013. I am originally from the Philippines. I started studying web programming in 2015. I love it because I like making fun websites and web apps. The projects that I built so far are posted below. I like to cook and I love to travel. I also like to volunteer and I am a team leader with the non-profit organization, NYCares.
This entry was posted in php. Bookmark the permalink.

3 Responses to Array

  1. Alex says:

    Wow, marvelous blog structure! How long have you been blogging for? you made blogging glance easy. The total glance of your site is great, let alone the content!

    Like

  2. Alex says:

    Wow, amazing weblog layout! How long have you been blogging for? you make blogging glance easy. The whole glance of your site is excellent, let alone the content!

    Like

  3. Alex says:

    Excellent weblog here! Also your web site lots up fast! What web host are you using? Can I get your associate link for your host? I desire my web site loaded up as quickly as yours lol

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s