Showing posts with label PHP ARRAY_FILTER. Show all posts
Showing posts with label PHP ARRAY_FILTER. Show all posts

Friday, 2 August 2019

How to check for empty values within a array using PHP

If you have an array called $array with the below values
Array
(
    [0] => 1299016800
    [1] => 
    [2] => 
)
As we can see in the above example we have 2 empty values within the array, now how do we determine this using PHP
First we get the size of the array
$mainSize = sizeof($array);
Now we get the size of the array without the empty values
$emptySize =sizeof(array_filter($array));
Now we compare the 2 different sizes, if they are equal to each other, then it means that there aren’t empty values within the array, else there are empty values within the array
$mainSize = sizeof($array);
$emptySize =sizeof(array_filter($array));
 
if($mainSize == $emptySize){
              echo 'Array not empty';
}else{
              echo 'Array is empty';
}

How to remove all 0’s (zeros) from an PHP array

If I have the following PHP array
$array = array(9,0,6,4,5,0,9,0,0,0);
and I need to have all 0’s removed from it.
Use the PHP array_filter function. If you don’t supply a callback function it will filter out all values that are equal to false
$new_array = array_filter($array);

Thursday, 30 August 2018

PHP array_filter with assoc array?

I am using array_filter to do something like this:

function endswithy($value) {
    return (substr($value, -1) == 'y');
}

$people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
$withy = array_filter($people, "endswithy");
var_dump($withy);

BUT with the more option in filter for example
$people = array(
             "Johnny"=>array("year"=>1989, "job"=>"prof"),
             "Timmy"=>array("year"=>1989,  "job"=>"std"),
             "Bobby"=>array("year"=>1988),
             "Sam"=>array("year"=>1983),
             "Tammy"=>array("year"=>1985),
             "Danny"=>array("year"=>1983),
             "Joe"=>array("year"=>1989,"job"=>"prof"));

OR
$people = array(
             array("name"=>"Johnny","year"=>1989, "job"=>"prof"),
             array("name"=>"Timmy","year"=>1989,  "job"=>"std"),
             array("name"=>"Bobby""year"=>1988),
             array("name"=>"Sam","year"=>1983),
             array("name"=>"Tammy","year"=>1985),
             array("name"="Danny","year"=>1983),
             array("name"="Joe","year"=>1989,"job"=>"prof"));

How Can I got the only this people (endwith y and year=1989 and job=prof) ,Can I use array_filter? or any build-in function to do this?
$people = array(
                 "Johnny"=>array("year"=>1989, "job"=>"prof")
  );

OR
$people = array(
                 array("name="Johnny","year"=>1989, "job"=>"prof")
  );


Either use foreach with your current array's structure:
$people = array(
    "Johnny" => array("year" => 1989, "job" => "prof"),
    "Timmy"  => array("year" => 1989, "job" => "std"),
    "Bobby"  => array("year" => 1988),
    "Sam"    => array("year" => 1983),
    "Tammy"  => array("year" => 1985),
    "Danny"  => array("year" => 1983),
    "Joe"    => array("year" => 1989, "job" => "prof"),
);

foreach ( $people as $name => $info ) {
    if ( substr($name, -1) !== 'y' || $info['year'] != 1989 ) {
        unset($people[$name]);
    }
}

print_r($people);

// output:
Array
(
    [Johnny] => Array
        (
            [year] => 1989
            [job] => prof
        )
    [Timmy] => Array
        (
            [year] => 1989
            [job] => std
        )
)

Or convert your array so that name is value of inner array:
$people = array(
    array('name' => 'Johnny', 'year' => 1989, 'job' => 'prof'),
    array('name' => 'Timmy' , 'year' => 1989, 'job' => 'std'),
    array('name' => 'Bobby' , 'year' => 1988),
    array('name' => 'Sam'   , 'year' => 1983),
    array('name' => 'Tammy' , 'year' => 1985),
    array('name' => 'Danny' , 'year' => 1983),
    array('name' => 'Joe'   , 'year' => 1989, 'job' => 'prof'),
);

function filter($item) {
    return substr($item['name'], -1) === 'y' && $item['year'] == 1989;
}

$filteredPeople = array_filter($people, 'filter');

print_r($filteredPeople);

// output:
Array
(
    [0] => Array
        (
            [name] => Johnny
            [year] => 1989
            [job] => prof
        )
    [1] => Array
        (
            [name] => Timmy
            [year] => 1989
            [job] => std
        )
)

PHP sort array comparing the value and pushing the smaller values ​​last


I have a value and comparing that value i want to change order of an array


for eg. here 3 is value so i need array order to be
5,6,1,2
so values less then 3 shifts last in array.
<?php

$value=3;

$array=array(6,2,5,1);

asort($array);

print_r($array);

?>


Please have a look on the below code, it may help you. You need to use array_filter with call back function to make 2 arrays then u can merge them.
  $value=3;
  $array=array(6,2,5,1);
  asort($array);
  $right = array_filter($array, function($elem) use($value){
      return $elem < $value;
  });
  $left = array_filter($array, function($elem) use($value){
     return $elem > $value;
  });
  //print_r($right);
  //print_r($left);
  $res = array_merge($left,$right);
  print_r($res);

Monday, 23 February 2015

PHP: Extract keywords from a webpage

The title said it all: A great code snippet to easily extract meta keywords from any webpage.
<?php
$meta = get_meta_tags('https://thiscode4u.blogspot.com/');
$keywords = $meta['keywords'];
// Split keywords
$keywords = explode(',', $keywords );
// Trim them
$keywords = array_map( 'trim', $keywords );
// Remove empty values
$keywords = array_filter( $keywords );

print_r( $keywords );
?>

Wednesday, 24 September 2014

array_filter in PHP

array_filter() is used to generate the subset of input array analyzing their values according to the function called.
PHP array_filter() function returned array contains only those values, which function returns true.

Syntax: array_filter(array,function)
Parameters Description:
array : Required. Defines an array.

function : Required. Name of the user-made function.

Example:

<?php
function myfunc($par1)
{
if($par1 === "Sunday")
{
return  true;
}
return false;
}
$days = array( 0=>"Sunday", 1=>"Monday", 2=>"Tuesday" );
print_r( array_filter($days,"myfunc") );
?>

O/P:


Array (
          [0] => Sunday
        )