Home>

Tips:

array_keys ($array) //Returns all key names
array_values ​​($array) //Returns all key values
$result=array_reverse ($input);//Reverse the array,Don't keep key names
$result_keyed=array_reverse ($input, true);//Reverse the array,Reserved key names
array_keys ($array, "blue");//Returns the keys whose value is blue

1. PHP two-dimensional array deduplication function

PHP array has a built-in function to remove duplicatesarray_unique (), but php's array_unique function only works for one-dimensional arrays,Not applicable to multidimensional arrays,The following provides an array_unique function for a two-dimensional array

function unique_arr ($array2d, $stkeep=false, $ndformat=true)
{
  //Determine whether to keep the first-level array keys (the first-level array keys can be non-numeric)
  if ($stkeep) $starr=array_keys ($array2d);
  //determine whether to retain secondary array keys (all secondary array keys must be the same)
  if ($ndformat) $ndarr=array_keys (end ($array2d));
  //Dimension reduction, you can also use implode to convert a one-dimensional array into a string connected by a comma
  foreach ($array2d as $v) {
    $v=join (",", $v);
    $temp []=$v;
  }
  //Remove duplicate strings,Which is a repeated one-dimensional array
  $temp=array_unique ($temp);
  //Reassemble the disassembled array
  foreach ($temp as $k =>$v)
  {
    if ($stkeep) $k=$starr [$k];
    if ($ndformat)
    {
      $temparr=explode (",", $v);
      foreach ($temparr as $ndkey =>$ndval) $output [$k] [$ndarr [$ndkey]]=$ndval;
    }
    else $output [$k]=explode (",", $v);
  }
  return $output;
}
</pre>
</div>
<div>
<pre>
$array2d=array ("first" =>array ("title" =>"1111", "date" =>"2222"), "second" =>array ("title" =>"1111 "," date "=>" 2222 ")," third "=>array (" title "=>" 2222 "," date "=>" 3333 "));
print_r ($array2d);
print_r (unique_arr ($array2d, true));

2. Sorting method of two-dimensional array

One-dimensional array sorting method:

Public functions:

function printr ($arr)
{
  echo "<pre>";
  print_r ($arr);
  echo "</pre><br>";
}

First group:sort and rsort, sort according to the order of asc and reverse order of php array key value,At the same time destroy the index relationship of the original array-in fact, delete the index and re-establish the numerical index starting from 0.

$a=array ("a" =>1,2);
sort ($a);printr ($a);
rsort ($a);printr ($a);
/* array
(
  [0] =>1
  [1] =>2
)
array
(
  [0] =>2
  [1] =>1
)
* /

The second set of functions:asort and arsort,These two functions are more powerful,As long as they can retain the original index relationship of the array,Replace the sort and rsort in the above example with these two functions respectively

$a=array ("a" =>1,2);
asort ($a);printr ($a);
arsort ($a);printr ($a);
/*
array
(
  [a] =>1
  [0] =>2
)
array
(
  [0] =>2
  [a] =>1
)
* /

The third set of php array sorting functions:krsort and ksort,The two are different from the above two groups.These two functions sort the keys.

$a=array ("a" =>1,2);
 ksort ($a);printr ($a);
krsort ($a);printr ($a);
/*
array
(
  [0] =>2
  [a] =>1
)
array
(
  [a] =>1
  [0] =>2
)
* /

Sort php arrays through custom functions,There are three functions:

uasort sorts the keys of a php array through a custom function,And retain the original index relationship.

uksort sorts the keys of a php array by a custom function,And retain the original index relationship.

usort sorts the keys of a php array through a custom function,And delete the original index relationship, build a new index from scratch.

Here isTwo-dimensional sort:

/**
 * @package bugfree
 * @version $id:functionsmain.inc.php, v 1.32 2005/09/24 11:38:37 wwccss exp $
 *
 *
 * sort an two-dimension array by some level two items use array_multisort () function.
 *
 * sortarr ($array, "key1", "sort_asc", "sort_retular", "key2" ...)
 * @author chunsheng wang<[email protected]>
 * @param array $arraydata the array to sort.
 * @param string $keyname1 the first item to sort by.
 * @param string $sortorder1 the order to sort by ("sort_asc" | "sort_desc")
 * @param string $sorttype1 the sort type ("sort_regular" | "sort_numeric" | "sort_string")
 * @return array sorted array.
 * /
function sortarr ($arraydata, $keyname1, $sortorder1="sort_asc", $sorttype1="sort_regular")
{
  if (! is_array ($arraydata)) return $arraydata;
  //get args number.
  $argcount=func_num_args ();
  //get keys to sort by and put them to sortrule array.
  for ($i=1;$i<$argcount;$i ++)
  {
    $arg=func_get_arg ($i);
    if (! eregi ("sort", $arg))
    {
      $keynamelist []=$arg;
      $sortrule []="$". $arg;
    }
    else $sortrule []=$arg;
  }
  //get the values ​​according to the keys and put them to array.
  foreach ($arraydata as $key =>$info)
  {
    foreach ($keynamelist as $keyname) ${$keyname} [$key]=strtolower ($info [$keyname]);
  }
  //create the eval string and eval it.
  $evalstring="array_multisort (". join (",", $sortrule). ", $arraydata);";
  eval ($evalstring);
  return $arraydata;
}

Example:

//##################Example ##################
$arr=array (
 array (
   "name" =>"Learn",   "size" =>"1235",   "type" =>"jpe",   "time" =>"1921-11-13",   "class" =>"dd", ), array (
   "name" =>"Chinese Kung Fu",   "size" =>"153",   "type" =>"jpe",   "time" =>"2005-11-13",   "class" =>"jj", ), array (
   "name" =>"programming",   "size" =>"35",   "type" =>"gif",   "time" =>"1997-11-13",   "class" =>"dd", ), array (
   "name" =>"Chinese Kung Fu",   "size" =>"65",   "type" =>"jpe",   "time" =>"1925-02-13",   "class" =>"yy", ), array (
   "name" =>"Chinese Kung Fu",   "size" =>"5",   "type" =>"icon",   "time" =>"1967-12-13",   "class" =>"rr", ),);
echo "<pre>";
print_r ($arr);echo "<br>";
//Note:153 is smaller than 65 when sorted numerically
$temp=sortarr ($arr, "name", "sort_asc", "type", "sort_desc", "size", "sort_asc", "sort_string");
print_r ($temp);
echo "</pre>";

3. Multi-dimensional array to one-dimensional array

function rebuild_array ($arr) {//rebuild a array
 static $tmp=array ();
 for ($i=0;$i<count ($arr);$i ++) {
  if (is_array ($arr [$i])) {
    rebuild_array ($arr [$i]);
  } else {
    $tmp []=$arr [$i];
  }
 }
 return $tmp;
}

Example:

$arr=array ("123.html", "456.html", array ("dw.html", "fl.html", array ("ps.html", "fw.html")), "ab. html ");
//define a three-dimensional array,To test our function
echo "<pre>";
print_r (rebuild_array ($arr));
echo "</pre>";

4. Remove blank elements from the array

function array_remove_empty (&$arr, $trim=true)
{
  foreach ($arr as $key =>$value) {
    if (is_array ($value)) {
      array_remove_empty ($arr [$key]);
    } else {
      $value=trim ($value);
      if ($value == "") {
        unset ($arr [$key]);
      } elseif ($trim) {
        $arr [$key]=$value;
      }
    }
  }
}

Example:

$a=array (array (3), 2, "", array ("", 23), 0);
array_remove_empty ($a);
printr ($a);//printr function please find in this article

5. Get the value under a specific key in a multidimensional array,And generate a one-dimensional array

function getall_by_key (array $arr, $key) {
  if (! trim ($key)) return false;
  preg_match_all ("/\" $key \ ";\ w {1}:(?:\ d +:|) (. *?);/", serialize ($arr), $output);
  return $output [1];
}
$testarr=array ("111" =>100, "out" =>200, "333" =>array ("out" =>310, array (321, 322, "out" =>" this is a test ")));
printr (getall_by_key ($testarr, "out"));
php
  • Previous Android implements IOS camera slide control
  • Next Talking about the life cycle of cookies