Some examples.
Last active
July 2, 2019 06:29
-
-
Save davilera/c125b7370fc5fe16332e22ae35b7f741 to your computer and use it in GitHub Desktop.
PHP Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$list = array( 'orange', 'apple', 'pear' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$list = [ 'orange', 'apple', 'pear' ]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ages_in_nelio = array( | |
'toni' => 34, | |
'david' => 33, | |
'ruth' => 0, // unknown value | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'body_class', 'multisite_body_classes' ); | |
function multisite_body_classes( $classes ) { | |
$id = get_current_blog_id(); | |
$slug = strtolower( str_replace( ' ', '-', trim( get_bloginfo( 'name' ) ) ) ); | |
$classes[] = $slug; | |
$classes[] = 'site-id-' . $id; | |
return $classes; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'body_class', function( $classes ) { | |
$id = get_current_blog_id(); | |
$slug = strtolower( str_replace( ' ', '-', trim( get_bloginfo( 'name' ) ) ) ); | |
$classes[] = $slug; | |
$classes[] = 'site-id-' . $id; | |
return $classes; | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
remove_filter( 'body_class', 'multisite_body_classes' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function filter_expensive_products( $products, $min_price ) { | |
$result = array(); | |
foreach ( $products as $product ) { | |
if ( $min_price < $product['price'] ) { | |
array_push( $reuslt, $product ); | |
} | |
} | |
return $result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function filter_expensive_products( $products, $min_price ) { | |
return array_values( array_filter( | |
$products, | |
function( $product ) use ( $min_price ) { | |
return $min_price < $product['price']; | |
} | |
) ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$people = array( | |
'john' => 29, | |
'peter' => 38, | |
'stephany' => 34, | |
'mark' => 28, | |
); | |
$people_in_their_twenties = array_filter( $people, function( $age ) { | |
return 20 <= $age && $age < 30; | |
} ); | |
// Returns: | |
// array( | |
// 'john' => 29, | |
// 'mark' => 28, | |
// ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$products = array( | |
[ 'name' => 'X', 'price' => 20 ], | |
[ 'name' => 'Y', 'price' => 50 ], | |
[ 'name' => 'Z', 'price' => 12 ], | |
[ 'name' => 'W', 'price' => 31 ], | |
); | |
$cheap_products = array_filter( $products, function( $product ) { | |
return $product['price'] <= 20; | |
} ); | |
// Returns: | |
// array( | |
// 0 => [ 'name' => 'X', 'price' => 20 ], | |
// 2 => [ 'name' => 'Z', 'price' => 12 ], | |
// ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$products = array( | |
[ 'name' => 'X', 'price' => 20 ], | |
[ 'name' => 'Y', 'price' => 50 ], | |
[ 'name' => 'Z', 'price' => 12 ], | |
[ 'name' => 'W', 'price' => 31 ], | |
); | |
$cheap_products = array_values( | |
array_filter( $products, function( $product ) { | |
return $product['price'] <= 20; | |
} ) | |
); | |
// Returns: | |
// array( | |
// 0 => [ 'name' => 'X', 'price' => 20 ], | |
// 1 => [ 'name' => 'Z', 'price' => 12 ], | |
// ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function search_product( $products, $name ) { | |
$item = false; | |
foreach ( $products as $product ) { | |
if ( ! $item && $product['name'] === $name ) { | |
$item = $product; | |
}//end if | |
} | |
return $item; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function search_product( $products, $name ) { | |
$matching_items = array_values( array_filter( | |
$products, | |
function( $product ) use ( $name ) { | |
return $product['name'] === $name; | |
} | |
) ); | |
return $matching_items[0]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function apply_vat( $products ) { | |
$result = array(); | |
foreach ( $prices as $price ) { | |
array_push( $result, $price * 1.21 ); | |
} | |
return $result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function apply_vat( $prices ) { | |
return array_map( function( $price ) { | |
return $price * 1.21; | |
}, $prices ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function compute_total_price( $products ) { | |
$total_price = 0; | |
foreach ( $products as $product ) { | |
$total_price += $product['price'] | |
} | |
return $total_price; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function compute_total_price( $products ) { | |
return array_reduce( | |
$products, | |
function( $total_price, $product ) { | |
return $total_price + $product['price']; | |
}, | |
0 | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$fruits = [ 'orange', 'pear', 'pineapple', 'apple', 'strawberry' ]; | |
$fruits_i_like = [ 'orange', 'apple' ]; | |
$fruits_i_dont_like = array_diff( $fruits, $fruits_i_like ); | |
// Result: | |
// [ 'pear', 'pineapple', 'strawberry' ]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$items = [ 'apple', 'orange', 'pear', 'pear', 'apple', 'apple' ]; | |
$items = array_unique( $items ); | |
// Result: | |
// [ 'apple', 'orange', 'pear' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$fruits = [ 'orange', 'pineapple', 'apple', 'strawberry' ]; | |
$has_apple = in_array( 'apple', $fruits ); | |
// Result: true | |
$has_pear = in_array( 'pear', $fruits ); | |
// Result: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment