viernes, 7 de febrero de 2025

getting the key name for a value

<? $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; print_r(array_find_key($array,function($v){ return ($v=="goose");})); ?>

jueves, 6 de febrero de 2025

Seraching key and retuning value

<?php

 $search_array = array('first' => 1334, 'second' => 4);

echo (array_key_exists('first', $search_array)==true?$search_array['first']:0);  //1334


?>

array_filter( ) simplified

 $elements=[13,45,101,33,1,0];


$arr= array_filter($elements,function($value){ return($value>100);  });


print_r($arr);   //101

lunes, 3 de febrero de 2025

searching in string as array

 $text="Gobierno afirma que el país tiene la segunda canasta alimentaria más baja de la región";


echo in_array(strtolower('GoBierno'),$arr1=explode(' ',strtolower($text)));  //1


print_r($arr1);



sábado, 1 de febrero de 2025

custom array filter values

$arr=[12,31,14,101,9,220];



function test(array $arr,int $val){

  if(gettype($arr)!="array" || gettype($val)!="integer"){ return 0; exit();}

  $holder=array();

foreach($arr as $key=>$value){

  

 if($value>$val){ $holder[]=$value;}

}

 return $holder;                 

}

$holder=test($arr,20);

print_r($holder);

echo "<br>";


echo count($arr)==count($holder)?"all_values_match":(count($holder)>0?"any_match":-1);

sábado, 25 de enero de 2025

variable filter

 $parameter="bob@example.com";


echo filter_var($parameter, FILTER_VALIDATE_EMAIL)==false?-1:$parameter;


//bob@example.com


https://www.php.net/manual/en/function.filter-var.php

https://www.php.net/manual/en/filter.constants.php#constant.filter-validate-int

domingo, 12 de enero de 2025

Array filter

 <?php


$arr=["name"=>"Ambx","lastname"=>"Rodriguez","Placencio"];


$d=array_filter($arr,function($value){ return (strlen($value)>6);});




print_r($d);

?>