Php array comparison value, how to solve

Source: Internet
Author: User
Php array comparison value {[0] = & gt; string (9) & quot; limit 29 & quot; [1] = & gt; string (8) & quot; 2_334 & quot; [2] = & gt; string (4) & quot; 1 & quot; [3] php array comparison values
{[0] => string (9) "29th 29" [1] => string (8) "2_334" [2] => string (4) "1" [3] => string (4) "2"
[4] => string (9) "limit 30" [5] => string (8) "5_334" [6] => string (4) "6" [7] => string (4) "10 "}

If the value in front of the separator '-' is the same as the value without '-' in the data, this data is not retrieved.
For example, 29th, because the data contains 1 and [2] => string (4) "1 ",
The final result is only '-' and there are no duplicates.
{[5] => string (8) "5_334 "}


------ Solution --------------------

$ A = array ("000029", "2_334", "1", "2", "000030", "5_334", "6", "10 ");
Var_dump ($ );

$ Keys = array ();
$ Result = array ();
Foreach ($ a as $ val ){
If (is_numeric ($ val ))
$ Keys [] = $ val;
}
Foreach ($ a as $ val ){
If (! Is_numeric ($ val )){
$ Tmp = explode ("_", $ val );
If (count ($ tmp) = 2 &&! In_array ($ tmp [0], $ keys ))
$ Result [] = $ val;
}
}
Var_dump ($ result );
?>

Output:

Array (8 ){
[0] =>
String (4) "29th 29"
[1] =>
String (5) "2_334"
[2] =>
String (1) "1"
[3] =>
String (1) "2"
[4] =>
String (4) "limit 30"
[5] =>
String (5) "5_334"
[6] =>
String (1) "6"
[7] =>
String (2) "10"
}
Array (1 ){
[0] =>
String (5) "5_334"
}

------ Solution --------------------
Very earthy code, but the result is what you need.
PHP code
$Array = array ('1_29', '2_334', '1', '2', '1_30', '5_334', '6', '10' );$ReferArray = array ();$FixArray = array ();$MyArray = array ();foreach ( $Array as $Value ) {    if (strpos ( $Value, '_' ) == false) {        $ReferArray [] = $Value;    } else {        $FixArray [] = substr ( $Value, 0, 1 );        $MyArray [] = $Value;    }}$FixArray = array_diff ( $FixArray, $ReferArray );foreach ( $FixArray as $Key => $Value ) {    echo $MyArray [$Key];}

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.