search
HomeBackend DevelopmentPHP Tutorial如何判断两组字符串是否有相同的字符?

$a=2344;//主字符串,固定值。

//下面为可变化的变量
如果$b=12;//判断$a字符串中是否包含1和2,判断后结果为假
如果$b=22;//判断$a字符串中是否包含两个2,判断后结果只有一个2,假
如果$b=234;//判断$a字符串中是否包含2,3,4.判断后结果$a中全部包含,真
如果$b=324;//判断$a字符串中是否包含3,2,4.判断后结果$a中全部包含,真
如果$b=4432;//判断$a字符串中是否包含4,4,3,2.判断后结果$a中全部包含,真
如果$b=4444;//判断$a字符串中是否包含4个4,.判断后结果$a只有二个4,假
如果$b
如果$b
如果$b
......后面还会有许多

我需要的判断结果为如上。
判断语句怎么写?


回复讨论(解决方案)

$a = '2344';$d = array('12', '22', '234', '324', '4432', '4444');$a = count_chars($a, 1);foreach($d as $b) {  $t = count_chars($b, 1);  $f = 0;  if(array_keys(array_intersect_key($t, $a)) == array_keys($a)) $f = 1;  printf("b = %s %s\n", $b, $f ? '真' : '假');}
b = 12 假b = 22 假b = 234 真b = 324 真b = 4432 真b = 4444 假

如果$b=4444;//判断$a字符串中是否包含4个4,.判断后结果$a只有二个4,假
这条规则似乎不妥

把$a和$b按字符分别拆成数组,然后判断是否子集?

不知道你这样做的目的是什么?
要从业务逻辑入手,可能有更好的解决方案。

$a = '2344';$d = array('12', '22', '234', '324', '4432', '4444');$a = count_chars($a, 1);foreach($d as $b) {  $t = count_chars($b, 1);  $f = 0;  if(array_keys(array_intersect_key($t, $a)) == array_keys($a)) $f = 1;  printf("b = %s %s\n", $b, $f ? '真' : '假');}
b = 12 假b = 22 假b = 234 真b = 324 真b = 4432 真b = 4444 假

如果$b=4444;//判断$a字符串中是否包含4个4,.判断后结果$a只有二个4,假
这条规则似乎不妥



这样的判断还是有缺陷啊,
如果$a为4444呢。
我想要的结果是,只有$b=4444;或者$b=44,或者$b=444或者$b=4的时候,才能匹配成功,才能为真,其他都不符合,

我已经说了,你的规则有问题(或者说不完备)
我给的代码只能处理一种情况:$a 中的所有字符(去除重复的)都必须出现在 $b 中

等你制定好完备的规则后,再去扩展功能

先把字符串转换成数组,然后用array_count_values计算各字符出现的字数,如果$b中出现的次数小于等于$a中出现的字数就是真,否则就是假。

我已经说了,你的规则有问题(或者说不完备)
我给的代码只能处理一种情况:$a 中的所有字符(去除重复的)都必须出现在 $b 中

等你制定好完备的规则后,再去扩展功能



直接说我的业务需求吧,或许能明白一点,
这样子的。
七星彩你应该知道吧,
我们公司需要做一个这样的一个游戏活动。

如果这期开奖号码是5667 321
这里我们只截取前面四位数,也就是5667来作为是否中奖,后面三位省略掉。
而中奖规则为四等奖项。
一等奖为四个数同现,意思是,四个数字同时出现
二等奖为三个数同现,
三等奖为二个数同现,
四等奖为一个数同现。
那么, 开奖之前,会有许多人来投注。

如果a单投了一个5,则开奖号码中有一个5,则中奖了。但他中的是四等奖。
如果B投了567,则开奖号码中同时出现567,中了个2等奖。
如果C投了66,则开奖号码中,有两个6同时出现,也中了。(可能会问,为什么这样投呢,因为如果投重复码并中奖的话,奖励会翻倍。)
如果D投了5676,则开奖号码中全部匹配,中了一等奖。

大概是这样的一个规则。意思是,不过你投了1个数字或者4个数字,开奖号码中,必须全部匹配才能中奖。包括重复数

$a='5415';$a=str_split($a);$a=array_count_values($a);$b='514';$b=str_split($b);$b=array_count_values($b);$result=true;foreach($b as $k=>$v){    if(!isset($a[$k]) OR $a[$k]<$v){        $result=false;        break;    }}var_dump($result);

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool