How to deal with the problem of PHP array

Source: Internet
Author: User
This article to share the content is about the PHP array problem processing method, has a certain reference value, the need for friends can refer to

There are two sets of data in the one array, the value of the specified key as the new index, and the value of the other key as the value of the new index.

public static function index (array $array, $name) {    $indexedArray = array ();        if (empty ($array)) {          return $indexedArray;      }        foreach ($array as $item) {          if (isset ($item [$name])) {$indexedArray [$item [                  $name]] = $item;                        Continue;                   }           }                  return $indexedArray;  }
For example: $array = array (' ID ' =>1,
           ' Name ' = ' Xiaogou '
          ),
          Array (' ID ' =>2,
           ' Name ' = ' Xiaomao '
        )
Direct return index ($array, ' id ');

The result is

Array (' 1 ' = ' Xiaogou ', ' 2 ' = ' Xiaomao ');
Gets the value collection of the array-specified key
public static function column (array $array, $columnName) {    if (empty ($array)) {          return array ();      }        $column = Array ();        foreach ($array as $item) {          if (isset ($item [$columnName])) {                  $column [] = $item [$columnName];              }     }          return $column; }

For example:

$array = array (' ID ' =>1, ' name ' = ' Xiaogou '), array (' ID ' =>2, ' name ' = ' Xiaomao ')); $array _ids = Column ($ Array, ' ID ');

$array _ids result is an array (0=>1,1=>2);

Third, get the value of the array specified key (unlike the above set, this is the direct fetch value)
public static function get (array $array, $key, $default) {    if (isset ($array [$key])) {          return $array [$key];      } else {          return $default;      }}
Iv. replacing values of the same key for a multidimensional array
public static function rename (array $array, array $map) {    $keys = Array_keys ($map);        foreach ($array as $key = + $value) {          if (In_array ($key, $keys)) {                  $array [$map [$key]] = $value;                       Unset ($array [$key]);               }           return $array;  }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.