<aaaaa
http://agratitudesign.blogspot.com/2013/09/styling-script-code-snippets-syntax.html
https://www.youtube.com/watch?v=iFhA3kok6sY#t=118.971951
https://code.google.com/archive/p/syntaxhighlighter/downloads
aaaaa
http://agratitudesign.blogspot.com/2013/09/styling-script-code-snippets-syntax.html
https://www.youtube.com/watch?v=iFhA3kok6sY#t=118.971951
https://code.google.com/archive/p/syntaxhighlighter/downloads
კიდევ >>
You are going to run into PHP Arrays and PHP array functions with amazing regularity during the course of your web design and web development. It makes sense, because arrays are one of the most useful data types we can use. So it got us to thinking, what would you get if you combined the worlds most popular PHP CMS applications and dumped all of their source code into one bucket, then analyzed their most used array functions? Well you would get this awesome list of course! This is a great way to keep your skills sharp or simply refreshed when working with Arrays in PHP. Let’s check it out.
1. array()
This handy little guy creates an array for you. Here we’ll create an associative array with two keys, and then place an an associative array inside one and an indexed array inside the other.
<?php function (); $array = array ( 'websites' => array ( 'Search' => 'Google', 'Social' => 'Facebook', 'News' => 'NY Times' ), 'friends' => array ( 'Chris', 'Jim', 'Lynn', 'Jeff', 'Joanna' ) ); print_r ( $array ); ?>And it goes a little something like this, HIT IT:
Array
(
[websites] => Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
)
Learn more about array() at http://us1.php.net/manual/en/function.array.php
2. is_array()
Checks whether the variable is an array. Returns TRUE if the variable is an array, and FALSE otherwise. Used like so:
<?php
echo is_array($array); //1 or TRUE
?>
Learn more about is_array() at http://us2.php.net/manual/en/function.is-array.php
3. in_array()
We may often want to check if a certain value is in one of our arrays. Recall from our example above that we assigned an indexed array of friends to the ‘friends’ key of our example array. Let’s see if ‘Jeff’ was included.
$result = in_array('Jeff', $array['friends']);
print_r($result); // 1 or TRUE
Ah ha, turns out he was. With in_array(), the first argument is what you are looking for, and the second argument is the array you will check in. Learn more about in_array() at http://us3.php.net/manual/en/function.in-array.php
4. array_merge
Remember we had some websites and friends in the array from the prior example. In fact we stored an array of values in those arrays. Well I’m feeling lazy and I don’t want to work with two arrays. I just want to work with one, so let’s merge those two arrays together!
<?php
$array = array (
'websites' => array (
'Search' => 'Google',
'Social' => 'Facebook',
'News' => 'NY Times'
),
'friends' => array (
'Chris',
'Jim',
'Lynn',
'Jeff',
'Joanna'
)
);
$merged = array_merge ( $array ['websites'], $array ['friends'] );
print_r ( $merged );
?>
And bingo bango, we now have one array created out of two!
Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
Learn more about array_merge() at http://us3.php.net/manual/en/function.array-merge.php
5. array_keys
Let’s now extract all the keys from our $merged array by adding these lines:
<?php
$keys = array_keys ( $merged );
print_r ( $keys );
?>
And now you can see the keys of our $merged array become the values of the array returned from array_keys()!
Array
(
[0] => Search
[1] => Social
[2] => News
[3] => 0
[4] => 1
[5] => 2
[6] => 3
[7] => 4
)
Learn more about array_keys() at http://us3.php.net/manual/en/function.array-keys.php
6. array_key_exists()
With array_key_exists() we can perform a validation similar to the way we would with isset(). You pass a key to search for and an array to search in, and the function will return TRUE if the key exists in the array provided. Building on our prior example, let’s see if the 7th key is set:
<?php
$keys = array_keys ( $merged );
$exists = array_key_exists('7', $keys);
print_r ( $exists ); // 1 or TRUE
echo $keys['7']; //The key of 7 has value of 4
?>
Learn more about array_key_exists() athttp://us3.php.net/manual/en/function.array-key-exists.php
7. array_shift()
Let’s remember our original $array which had two keys in it, ‘websites’ and ‘friends’. I want each key to have it’s own variable name in the program, how can I do that? Well, let’s apply the array_shift() function to $array, return the result to a variable $shifted, and examine both variables.
<?php
$array = array (
'websites' => array (
'Search' => 'Google',
'Social' => 'Facebook',
'News' => 'NY Times'
),
'friends' => array (
'Chris',
'Jim',
'Lynn',
'Jeff',
'Joanna'
)
);
$shifted = array_shift ( $array );
print_r ( $array );
print_r ( $shifted );
?>
You can see the ‘websites’ key, which is the first in the array, was shifted out of the array, while the ‘friends’ key was left intact. Awesome!
// print_r ( $array ); 'friends' is still in the original
Array
(
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
)
// print_r ( $shifted ); 'websites' was shifted out
Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
Learn more about array_shift() at http://us3.php.net/manual/en/function.array-shift.php
8. array_push
Our newly created $shifted variable is mad. It wants back into the original$array variable to undo the shifting we have caused. Well, let’s try to apply thearray_push() function to the original $array and push $shifted back onto the end of the array:
<?php
$array = array (
'websites' => array (
'Search' => 'Google',
'Social' => 'Facebook',
'News' => 'NY Times'
),
'friends' => array (
'Chris',
'Jim',
'Lynn',
'Jeff',
'Joanna'
)
);
$shifted = array_shift ( $array );
array_push ( $array, $shifted );
print_r ( $array );
?>
You can see friends is now in the first position and the $shifted array is added back to the original array. Note that array_push() does not keep key => value pairs intact! We have lost our ‘websites’ key(it is now 0).
Array
(
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
[0] => Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
)
Learn more about array_push() at http://us3.php.net/manual/en/function.array-push.php
9. array_pop
Fickle mister $shifted is mad about losing his ‘websites’ key. I’m not going to be in this $array if I can’t have my original key. Ok fine then, we’ll array_pop() you right off the end of this $array one more time.
<?php
$array = array (
'websites' => array (
'Search' => 'Google',
'Social' => 'Facebook',
'News' => 'NY Times'
),
'friends' => array (
'Chris',
'Jim',
'Lynn',
'Jeff',
'Joanna'
)
);
$shifted = array_shift ( $array );
array_push ( $array, $shifted );
$shifted = array_pop ( $array ); //POP!
print_r ( $array );
print_r ( $shifted );
?>
And there you go mister $shifted – popped off and placed back into your own array.
Array
(
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
)
Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
Learn more about array_pop() at http://us3.php.net/manual/en/function.array-pop.php
10. array_values
Remember when we created the $merged array earlier and we got a nifty little array looking like this?
Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
Well, I’m not crazy about the fact that some of those keys are named and others are indexed. I’d like to grab just the values of the $merged array listed only with numeric indexes. Let use the array_values() function to do just that.
<?php
$array = array (
'websites' => array (
'Search' => 'Google',
'Social' => 'Facebook',
'News' => 'NY Times'
),
'friends' => array (
'Chris',
'Jim',
'Lynn',
'Jeff',
'Joanna'
)
);
$merged = array_merge ( $array ['websites'], $array ['friends'] );
$merged = array_values( $merged ); //get the values
print_r ( $merged );
?>
Ah yes, there she is – my nicely formatted merged array with neat indexes 0 – 7 courtesy of array_values().
Array
(
[0] => Google
[1] => Facebook
[2] => NY Times
[3] => Chris
[4] => Jim
[5] => Lynn
[6] => Jeff
[7] => Joanna
)
Learn more about array_values() athttp://us3.php.net/manual/en/function.array-values.php
11. array_map
Ok all this array business has me needing a break. Let’s go shopping and buy some cool new gadgets, maybe a new iPad Air. You know, these days they tax you on everything, so when we go shopping, let’s not forget that we have to pay a 5% sales tax. We’ll use our array_map() function to help us there.
<?php
function salestax($price) {
return number_format ( ($price * 1.05), 2, '.', '' );
}
$items = array (
100,
50,
250,
70,
500
);
$finalcost = array_map ( 'salestax', $items );
print_r ( $finalcost );
?>
Ok check it out. We had our array of $items to shop for, but we needed to quickly calculate the sales tax for each item at a 5% rate. Well with array_map() we can apply a function to every single array element, and return an array with the newly processed results, in this case our $finalcost – neat!
Array
(
[0] => 105.00
[1] => 52.50
[2] => 262.50
[3] => 73.50
[4] => 525.00
)
Learn more about array_map() at http://us3.php.net/manual/en/function.array-map.php
12. array_unique
You know there is a lot of blogging going on out there, and the search engines want to make sure they only index unique articles. Our little internet spider came across some articles below and put them into the $index. We need to make sure they are unique however, maybe we can use array_unique() to help us out!
<?php
$index = array (
'How to Eat Apples',
'Surfing Safely in a Wave Pool',
'The Best Foods For Breakfast',
'How to Eat Apples',
'25 tips to blogging nirvana',
'The Best Egg Nogg Recipe for the Holidays'
);
$unique = array_unique ( $index );
print_r ( $unique )
?>
Awesome! Working as designed! Our array_unique() got rid of that duplicate value in the array and returned us a new array with only unique titles. Did you spot the duplicate title in the original array?
Array
(
[0] => How to Eat Apples
[1] => Surfing Safely in a Wave Pool
[2] => The Best Foods For Breakfast
[4] => 25 tips to blogging nirvana
[5] => The Best Egg Nogg Recipe for the Holidays
)
Learn more about array_unique() athttp://us3.php.net/manual/en/function.array-unique.php
13. array_slice
Come to think of it, rather than use that array_unique() on our $index, let’s go ahead and perform some surgery on that array and extract only the inner elements, leaving the first and last entry to their own devices.
<?php
$index = array (
'How to Eat Apples',
'Surfing Safely in a Wave Pool',
'The Best Foods For Breakfast',
'How to Eat Apples',
'25 tips to blogging nirvana',
'The Best Egg Nogg Recipe for the Holidays'
);
$surgerized = array_slice ( $index, 1, 4 ); //slice it
print_r ( $surgerized );
?>
And there you have it, by passing the array to work on, the starting offset, and the length of our extraction, we have successfully removed the middle 4 entries of our 6 element array. Bravo!
Array
(
[0] => Surfing Safely in a Wave Pool
[1] => The Best Foods For Breakfast
[2] => How to Eat Apples
[3] => 25 tips to blogging nirvana
)
14. array_diff
Ok friends, our internet spider has been busy and it has created an updated index after some time. We’d like to examine our first index in comparison to the second and see what’s new. Let’s do it with our array_diff() function!
<?php
$index = array (
'How to Eat Apples',
'Surfing Safely in a Wave Pool',
'The Best Foods For Breakfast',
'How to Eat Apples',
'25 tips to blogging nirvana',
'The Best Egg Nogg Recipe for the Holidays'
);
$index2 = array (
'How to Eat Apples',
'Surfing Safely in a Wave Pool',
'The Best Foods For Breakfast',
'How to Eat Apples',
'25 tips to blogging nirvana',
'The Best Egg Nogg Recipe for the Holidays',
'Arrays with Style',
'PHP in the Enterprise',
'Douglas Crockford declares PHP his favorite language'
);
$diff = array_diff ( $index2, $index );
print_r ( $diff );
?>
Awesome. Our internet spider has found 3 new articles on the internet titled, ‘Arrays with Style’, ‘PHP in the Enterprise’, and ‘Douglas Crockford declares PHP his favorite language’. By passing our updated index and original index to thearray_diff() function, it returned only the new results to us.
Array
(
[6] => Arrays with Style
[7] => PHP in the Enterprise
[8] => Douglas Crockford declares PHP his favorite language
)
Learn more about array_diff() at http://us3.php.net/manual/en/function.array-diff.php
15. array_search
My buddy was telling me about a cool article named ‘PHP in the Enterprise’, I wonder if our updated index was able to capture it yet? Let’s find out witharray_search();
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$index2 = array (
'How to Eat Apples',
'Surfing Safely in a Wave Pool',
'The Best Foods For Breakfast',
'How to Eat Apples',
'25 tips to blogging nirvana',
'The Best Egg Nogg Recipe for the Holidays',
'Arrays with Style',
'PHP in the Enterprise',
'Douglas Crockford declares PHP his favorite language'
);
$found = array_search ( 'PHP in the Enterprise', $index2 ); // (needle,haystack)
print_r ( $found ); // 7
?>
Well isn’t that cool? We see that it is in fact in the index at position 7, as returned from array_search()
Learn more about array_search() athttp://us3.php.net/manual/en/function.array-search.php
16. array_reverse
When you want to turn an array upside down or rightside up, you can use thearray_reverse() function. I have added some elements to our original $array so we can get a better feel for how array_reverse() treats the array with both associative and indexed properties.
<?php
$array = array (
'websites' => array (
'Search' => 'Google',
'Social' => 'Facebook',
'News' => 'NY Times'
),
'friends' => array (
'Chris',
'Jim',
'Lynn',
'Jeff',
'Joanna'
),
16,
99,
13,
'newfriend' => 'Ken'
);
print_r ( $array );
$array = array_reverse ( $array );
print_r ( $array );
?>
And here we see the results of applying the array_reverse() to our $array
// before reverse
Array
(
[websites] => Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
[0] => 16
[1] => 99
[2] => 13
[newfriend] => Ken
)
// after reverse
Array
(
[newfriend] => Ken
[0] => 13
[1] => 99
[2] => 16
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
[websites] => Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
)
Learn more about array_reverse() athttp://us3.php.net/manual/en/function.array-reverse.php
17. array_unshift
We have seen how easy it is to add items to the end of an array, but what if we need to prepend something to our array? Well that is where our nifty littlearray_unshift() comes in. Let’s see how it works by adding an interest of ‘music’ to our array:
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$array = array (
'websites' => array (
'Search' => 'Google',
'Social' => 'Facebook',
'News' => 'NY Times'
),
'friends' => array (
'Chris',
'Jim',
'Lynn',
'Jeff',
'Joanna'
),
16,
99,
13,
'newfriend' => 'Ken'
);
array_unshift( $array, 'music');
print_r ( $array );
?>
Alrighty then! Check out that interest of music at the beginning of our array!
Array
(
[0] => music
[websites] => Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
[1] => 16
[2] => 99
[3] => 13
[newfriend] => Ken
)
Maybe you’d rather prepend an associative keyed value rather than a numeric, in that case you can simply add your original array to a key value pair like so:
<?php
$array = array ( 'interests' => 'music' ) + $array;
print_r ( $array );
?>
Array
(
[interests] => music
[websites] => Array
(
[Search] => Google
[Social] => Facebook
[News] => NY Times
)
[friends] => Array
(
[0] => Chris
[1] => Jim
[2] => Lynn
[3] => Jeff
[4] => Joanna
)
[0] => 16
[1] => 99
[2] => 13
[newfriend] => Ken
)
Well there you have it friends, an epic journey through the most widely used PHP array functions!
Thank you for reading PHP Array Functions – If you found this post helpful,
No comments:
Post a Comment