A case study of key and Current,next in PHP _php techniques

Source: Internet
Author: User
Tags php introduction

This article illustrates the joint application of key and Current,next in PHP. Share to everyone for your reference, specific as follows:

Problem:

$ARRQ = Array ();
$ARRQ [' TUC '] = Array (' SSS ' => ' ewq ', ' shu ' => ' Sjkah ', ' saa ' => ' Dwjhaz ');
$ARRQ [' tucs '] = Array (' ssss ' => ' ewsq ', ' sqhu ' =>dwjhaz ', ' sa12a ' => ');
$ARRQ [' tuca '] = Array (' SSSD ' => ' ewqq ', ' shu2 ' => ' Sjkah ', ' saSa ' => ');

How do I get rid of duplicate values in a $ARRQ array without destroying the array structure?

idea : First the elements in the first two-dimensional array are output with the Array_unique function.
When you output an element from a second two-dimensional array, you first use Array_unique to process it, and then remove the element that already exists in the first two-dimensional array.
When you output the third, you first use the Array_unique process, and then remove the elements that already exist in the first and second two-dimensional combination set.
Analogy

In short, you first have to array_unqiue to weight, and then remove the elements of their own in the previous array elements in the same concentration.

The following is code:

<?php
$arrq = Array ();
$ARRQ [' TUC '] = Array (' SSS ' => ' ewq ', ' shu ' => ' Sjkah ', ' saa ' => ' Dwjhaz ');
$ARRQ [' tucs '] = Array (' ssss ' => ' ewsq ', ' sqhu ' => ' Dwjhaz ', ' sa12a ' => ');
$ARRQ [' tuca '] = Array (' SSSD ' => ' ewqq ', ' shu2 ' => ' Sjkah ', ' saSa ' => ');
$last =array (); Sets an empty array to compare while
($arr =current ($ARRQ)) {
  //echo key ($ARRQ);
  $tmp =array_unique ($arr); First clear each one-dimensional array itself the same value
  if (count ($last) ==0) {
    $last =array_merge ($last, $arr);//To merge the previous array that has gone heavy
  }
  Else {//Output
    //echo key ($ARRQ) for the remaining one-dimensional array;
    foreach ($arr as $ke => $va) {if
      in_array ($arr [$ke], $last))//If the existing element
      unset ($arrq [Key ($ARRQ)] [$ke]); /delete, where the key value of the array element is critical
    }
    $last =array_merge ($last, $arr);//change $last into a collection of the preceding array for the next comparison
  }
  next ($ ARRQ); The pointer is moved back so that the key function can be used to obtain a different $ARRQ subscript
}
//The following output processed array
foreach ($arrq as $arr) {
  foreach ($arr as $val) {
    echo $val. " ";
  }
  echo "<br>";
>

The results of the operation are:

More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", " PHP Date and Time usage summary, PHP Introduction to object-oriented Programming, PHP String (String) Usage summary, PHP+MYSQL database Operations Tutorial and PHP Common database operating Skills summary

I hope this article will help you with the PHP program design.

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.