Skip to content

Instantly share code, notes, and snippets.

@davilera
Last active July 2, 2019 06:29
Show Gist options
  • Save davilera/c125b7370fc5fe16332e22ae35b7f741 to your computer and use it in GitHub Desktop.
Save davilera/c125b7370fc5fe16332e22ae35b7f741 to your computer and use it in GitHub Desktop.
PHP Arrays

Some examples.

<?php
$list = array( 'orange', 'apple', 'pear' );
<?php
$list = [ 'orange', 'apple', 'pear' ];
<?php
$ages_in_nelio = array(
'toni' => 34,
'david' => 33,
'ruth' => 0, // unknown value
);
<?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;
}
<?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;
} );
<?php
remove_filter( 'body_class', 'multisite_body_classes' );
<?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;
}
<?php
function filter_expensive_products( $products, $min_price ) {
return array_values( array_filter(
$products,
function( $product ) use ( $min_price ) {
return $min_price < $product['price'];
}
) );
}
<?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,
// )
<?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 ],
// )
<?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 ],
// )
<?php
function search_product( $products, $name ) {
$item = false;
foreach ( $products as $product ) {
if ( ! $item && $product['name'] === $name ) {
$item = $product;
}//end if
}
return $item;
}
<?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];
}
<?php
function apply_vat( $products ) {
$result = array();
foreach ( $prices as $price ) {
array_push( $result, $price * 1.21 );
}
return $result;
}
<?php
function apply_vat( $prices ) {
return array_map( function( $price ) {
return $price * 1.21;
}, $prices );
}
<?php
function compute_total_price( $products ) {
$total_price = 0;
foreach ( $products as $product ) {
$total_price += $product['price']
}
return $total_price;
}
<?php
function compute_total_price( $products ) {
return array_reduce(
$products,
function( $total_price, $product ) {
return $total_price + $product['price'];
},
0
);
}
<?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' ];
<?php
$items = [ 'apple', 'orange', 'pear', 'pear', 'apple', 'apple' ];
$items = array_unique( $items );
// Result:
// [ 'apple', 'orange', 'pear' ]
<?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