In PHP, key, current, and next are combined with instance analysis and next instance analysis.

Source: Internet
Author: User

In PHP, key, current, and next are combined with instance analysis and next instance analysis.

This article describes how to use key, current, and next in PHP. We will share this with you for your reference. The details are as follows:

Problem:

$arrq = array();$arrq['tuc'] = array('sss'=>'ewq','shu'=>'sjkah','saa'=>'dwjhaz');$arrq['tucs'] = array('ssss'=>'ewsq','sQhu'=>dwjhaz','sa12a'=>'dwjQhaz');$arrq['tuca'] = array('sssd'=>'ewqq','shu2'=>'sjkah','saSa'=>'dwjhhaz');

How can we remove the repeated values in the $ arrq array without damaging the array structure?

Ideas: First, output the elements in the first two-dimensional array using the array_unique function.
When outputting the elements in the second two-dimensional array, you first use array_unique to process the elements, and then drop them into the existing elements in the first two-dimensional array.
When outputting the third data, you first use array_unique for processing, and then drop the existing elements in the first and second two-dimensional data combination sets.
And so on.

In short, you must first remove the array_unqiue and then remove the elements existing in the array element collection in front of you.

The following code is used:

<? Php $ arrq = array (); $ arrq ['tuc'] = array ('ss' => 'ewq', 'shu' => 'sjkah ', 'saa '=> 'dwjhaz'); $ arrq ['ucs'] = array ('sss' => 'wsq', 'sqhu' => 'dwjhaz ', 'sa12a' => 'dwjqhaz'); $ arrq ['tuca'] = array ('sssd' => 'ewqq', 'shu2 '=> 'sjkah ', 'sasa '=> 'dwjhhaz'); $ last = array (); // set an empty array for comparison while ($ arr = current ($ arrq )) {// echo key ($ arrq); $ tmp = array_unique ($ arr); // first clear the same value of each one-dimensional array if (count ($ last) = 0) {$ last = array_merge ($ last, $ ar R); // merge the previously deduplicated arrays} else {// output the remaining one-dimensional arrays // echo key ($ arrq ); foreach ($ arr as $ ke => $ va) {if (in_array ($ arr [$ ke], $ last )) // if the existing element unset ($ arrq [key ($ arrq)] [$ ke]); // Delete, the KEY value of the array element is the KEY} $ last = array_merge ($ last, $ arr); // Changes $ last to the collection of the previous array, used for the next comparison} next ($ arrq); // move the pointer back, in this way, the key function can be used to obtain different $ arrq subscript} // The following output processed array foreach ($ arrq as $ arr) {foreach ($ arr as $ val) {echo $ val. "" ;}echo "<br>" ;}?>

The running result is:

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.