viernes, 10 de enero de 2025

Array functions grouped

 

1. Array Functions that Apply a Callback

These functions allow you to apply a callback function to each element of an array.

  • array_map() — Applies a callback to each element of an array (creates a new array).
  • array_walk() — Applies a callback to each element of an array (modifies the array in place).
  • array_walk_recursive() — Applies a callback to each element of an array recursively.
  • array_filter() — Filters elements of an array using a callback function.
  • array_reduce() — Reduces an array to a single value using a callback function.
  • array_flip() — Exchanges all keys and values in an array.
  • array_column() — Returns the values from a single column of the input array.

2. Array Functions for Checking Existence

These functions are used to check if a specific element or key exists in an array.

  • array_key_exists() — Checks if a specific key exists in the array.
  • array_key_first() — Returns the first key of an array.
  • array_key_last() — Returns the last key of an array.
  • array_search() — Searches for a value in an array and returns its key.
  • in_array() — Checks if a value exists in an array.
  • array_contains() (PHP 8.1 and later) — Checks if a value exists in an array.
  • array_flip() — Flips the array (keys become values and values become keys), so you can check if a value exists as a key.
  • array_search() — Searches for a value in an array and returns the corresponding key.
  • array_is_list() — Checks if the array is a list (numeric keys only).

3. Array Functions for Sorting and Ordering

These functions are used to sort or manipulate the order of elements in an array.

  • sort() — Sorts an array in ascending order.
  • rsort() — Sorts an array in descending order.
  • asort() — Sorts an array in ascending order, maintaining key-value relations.
  • arsort() — Sorts an array in descending order, maintaining key-value relations.
  • ksort() — Sorts an array by key in ascending order.
  • krsort() — Sorts an array by key in descending order.
  • natsort() — Sorts an array using a natural order algorithm (numeric order).
  • natcasesort() — Sorts an array using a natural order algorithm, ignoring case.
  • usort() — Sorts an array by values using a user-defined comparison function.
  • uksort() — Sorts an array by keys using a user-defined comparison function.
  • uasort() — Sorts an array by values using a user-defined comparison function while maintaining key-value relations.

4. Array Functions for Modifying or Manipulating Arrays

These functions are used to modify arrays in various ways.

  • array_merge() — Merges two or more arrays into one.
  • array_merge_recursive() — Merges arrays recursively.
  • array_slice() — Extracts a portion of an array.
  • array_splice() — Removes a portion of the array and optionally replaces it.
  • array_replace() — Replaces elements of an array with elements of another array.
  • array_diff() — Computes the difference of arrays.
  • array_diff_assoc() — Computes the difference of arrays including keys.
  • array_intersect() — Computes the intersection of arrays.
  • array_intersect_assoc() — Computes the intersection of arrays including keys.
  • array_chunk() — Splits an array into chunks.
  • array_pad() — Pads an array to a certain length with a value.
  • array_fill() — Fills an array with values.
  • array_flip() — Swaps all keys with values.
  • array_reverse() — Reverses an array.

5. Array Functions for Aggregation and Summarization

These functions help you summarize or aggregate data from arrays.

  • array_sum() — Returns the sum of all values in an array.
  • array_product() — Returns the product of all values in an array.
  • array_count_values() — Counts all values in an array.
  • array_unique() — Removes duplicate values from an array.
  • array_map() — Applies a callback function to each element in an array (returns a new array).
  • array_filter() — Filters elements of an array using a callback function.
  • array_reduce() — Reduces an array to a single value using a callback function.

6. Array Functions for Key and Value Manipulation

These functions are used for working with keys and values in arrays.

  • array_keys() — Returns all keys of an array.
  • array_values() — Returns all values of an array.
  • array_flip() — Exchanges keys and values.
  • array_rand() — Picks one or more random keys from an array.
  • array_key_first() — Returns the first key of an array.
  • array_key_last() — Returns the last key of an array.

7. Array Functions for Array Information and Properties

These functions are used for getting information about the array.

  • count() — Counts the number of elements in an array.
  • array_length() — Returns the length of an array (similar to count()).
  • array_is_list() — Checks if the array is a list (numerically indexed).
  • array_chunk() — Splits an array into chunks.
  • array_filter() — Filters elements in the array based on a callback.
  • array_flip() — Flips keys and values in an array.

8. Array Functions for Recursive Operations

These functions work with arrays that may be multidimensional (nested arrays).

  • array_walk_recursive() — Applies a callback function to each element of an array recursively.
  • array_map() — Can be used recursively if you apply it to multidimensional arrays (via callback).
  • array_filter() — Can be used with a recursive callback if the array is multidimensional.
  • array_merge_recursive() — Merges arrays recursively, handling multidimensional arrays.

9. Other Miscellaneous Array Functions

These functions don’t fit neatly into other categories but are useful for specific tasks.

  • array_diff_key() — Computes the difference of arrays using keys.
  • array_intersect_key() — Computes the intersection of arrays using keys.
  • array_multisort() — Sorts multiple arrays or multidimensional arrays.
  • array_walk() — Applies a callback to each element of an array.
  • array_rand() — Picks one or more random elements from an array.
  • array_flip() — Swaps keys and values.
  • array_replace() — Replaces elements of an array with values from another array.

array filter using call back function

 function test($var) {

  

 $r=($var>14)?1:0;

  

  return $r;

   

}



$array=[13,12,34];

print_r(array_filter($array, "test"));



Array ( [2] => 34 )

viernes, 27 de diciembre de 2024

select

 #!/usr/bin/php -q


<?php


// Include external configuration and helper files

require("/root/fm/db_con.php");     // Database connection details


// Establish a connection using MySQLi (object-oriented style)

$conn = new mysqli($host, $user, $password, $database);


// Check if the connection was successful

if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

}


// Define the query to fetch leads

$query = "SELECT lead_id, phone FROM leads";


// Execute the query and check for errors

if ($result = $conn->query($query)) {

    

    // Check if there are any rows returned

    if ($result->num_rows > 0) {

        // Loop through the result set and output each row

        while ($row = $result->fetch_assoc()) {

            // Safely output the values (HTML escaped for safety)

            echo htmlspecialchars($row['lead_id']) . ", " . htmlspecialchars($row['phone']) . "\n";

        }

    } else {

        // If no rows were found

        echo "No records found.\n";

    }


    // Free result set to free up memory

    $result->free();

} else {

    // If query fails, output an error message

    echo "Error executing query: " . $conn->error . "\n";

}


// Close the database connection

$conn->close();


?>


miércoles, 25 de diciembre de 2024

php tenary operator logical opertor

 echo $cmd=(45>13 && 211<100 ?"AMBIORIX":"RODRIGUEZ");

 as only  1  of the condition is true will return RODRIGUEZ



 echo $cmd=(45>13 || 211<100 ?"AMBIORIX":"RODRIGUEZ");

in tis case will return AMBIORIX as at least 1  is true

----------------------------------------


$argv=[1,3];


 echo $var=(count($argv)!=2)?exit():1;

miércoles, 18 de diciembre de 2024

accesing to a multidimensional array value

,<?php


   $array = array(

    "foo" => "bar",

    42    => 24,

    "multi" => 

  array(

         "dimensional" => array(

             "array" => "foo"

         )

     ));



echo $array['multi']['dimensional']['array'];  //foo


?>

Other example



$i=1; 

  $pos=array();

 


  

   $pos[$i]=["familyId"=>"12","familylocalName"=>"Ambiorix"];



  //$pos[1] is an array so you can  access direct to is key value $pos[1]['familyId']; 



echo  $pos[1]['familyId'];

///

jueves, 5 de diciembre de 2024

applying a call back function to all keys on an array.

 $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);


$array2 = array('green' => 3, 'blue1' => 1, 'yellow' => 7, 'cyan'   => 8);





echo "<br>";

print_r(array_map('strtoupper',array_keys($array1)));  

// will print Array ( [0] => BLUE [1] => RED [2] => GREEN [3] => PURPLE )


echo "<br>";





miércoles, 27 de noviembre de 2024

check if a value is not or is in an array and create other array with those values

 <?php



$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);

$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 2, 'cyan'   => 1);



$arr=array();



foreach($array1 as $value){

if (!in_array($value, $array2)) {

$arr[]=$value;

}


}


print_r($arr);



?>


Array ( [0] => 3 [1] => 4 )