PHP中的字符串数组(检查2个或更多字符串是否具有相同的开头)(Array of Strings in PHP (check if 2 or more strings have the same beginning))
我的问题似乎有点傻,但我不知道为什么我似乎无法使代码工作。
我有一个字符串数组,并希望执行以下操作:
当我找到以相同的3个字母exp 09_开头的3或n元素时,我想把它exp 09_一个由***分隔的元素。 我试着这样做
for ($j=0;$j
{
$begin= substr($list1[$j], 0, 3).'
';
//echo $list1[$j].'
';
if( strpos( $list1[$j], $begin ) !== false)
{
$list1[$j]=$list1[$j].'***'.$list1[$j+1];
unset($list1[$j+1]);
$list1 = array_values($list1);
}
echo $list1[$j].'
';
}
我有这个数组:
('01_order','09_customer bla bla bla ','09_customer bla1 bla1 bla1','09_customer bla2 bla2 bla2')
我想让它看起来像这个数组
('01-order','09_customer bla bla bla * 09_customer bla1 bla1 bla1* 09_customer bla2 bla2 bla2')
请给我一些想法
谢谢
My question seems a little bit silly, but I don't know why I can't seem to make the code work.
I have an array of strings and want to do the following:
When I find 2, 3 or n elements starting with the same 3 letters exp 09_ I want to make it one element seperated by ***. i tried to do this
for ($j=0;$j
{
$begin= substr($list1[$j], 0, 3).'
';
//echo $list1[$j].'
';
if( strpos( $list1[$j], $begin ) !== false)
{
$list1[$j]=$list1[$j].'***'.$list1[$j+1];
unset($list1[$j+1]);
$list1 = array_values($list1);
}
echo $list1[$j].'
';
}
I have this array:
('01_order','09_customer bla bla bla ','09_customer bla1 bla1 bla1','09_customer bla2 bla2 bla2')
I want to make it look like this array
('01-order','09_customer bla bla bla * 09_customer bla1 bla1 bla1* 09_customer bla2 bla2 bla2')
Please give me some ideas
Thank you
原文:https://stackoverflow.com/questions/51748077
更新时间:2019-12-15 01:35
最满意答案
创建一个由子字符串索引的临时数组,并使新数组的项目内爆
$res = [];
foreach($list1 as $x) {
$res[substr($x, 0, 3)][] = $x;
}
foreach($res as &$x) {
$x = implode('***', (array) $x);
}
print_r($res);
Make a temporary array indexed by substrings and implode the new array's items
$res = [];
foreach($list1 as $x) {
$res[substr($x, 0, 3)][] = $x;
}
foreach($res as &$x) {
$x = implode('***', (array) $x);
}
print_r($res);
2018-08-08
相关问答
你可以这样做 $subj = '00 - 04:38:27 - Hi';
preg_match('/^(\?\?|\d\d) - (\?\?|10|0\d):(\?\?|[0-5]\d):(\?\?|[0-5]\d) - (.*)/', $subj, $matches);
然后您可以访问匹配中的字段: $matches[1] = 00
$matches[2] = 04
$matches[3] = 38
$matches[4] = 27
$matches[5] = Hi
You can do i
...
一种可能的方法是检查每个前缀是否可以使用输入字符串表示。 我们的想法是,如果我们可以轻松计算长度为i的前缀是否可以用输入字符串表示,如果我们已经有这些信息用于较短的前缀(这可以通过检查任何允许的字符串是否导致更短的表达来完成前缀) - 见下面的代码。 var str = "forthcarkeys";
var arr = ["for","car","keys","forth"];
// isPossible[i] indicates whether we can express the
...
创建一个由子字符串索引的临时数组,并使新数组的项目内爆 $res = [];
foreach($list1 as $x) {
$res[substr($x, 0, 3)][] = $x;
}
foreach($res as &$x) {
$x = implode('***', (array) $x);
}
print_r($res);
在eval上演示 Make a temporary array indexed by substrings and implode the
...
我并不完全相信它是编码。 PHP将以相同的格式在内部存储其所有字符串。 你能试试这段代码吗? 它将比较两个字符串中每个字符的ascii值,这可能会通过直观地比较字符串来揭示您未看到的内容。 $str1 = ...;
$str2 = ...;
if(strlen($str1) != strlen($str2)) {
echo "Lengths are different!";
} else {
for($i=0; $i < strlen($str1); $i++) {
if(ord
...
使用implode基本上将数组值连接为一个字符串,然后使用strpos检查字符串中的字符串。 传递给implode的第一个参数用于分隔数组中的每个值。 $array = array("Hello_backup","World!","Beautiful_backup","Day!");
$r = implode(" ", $array);
if (strpos($r, "backup") !== false) {
echo "found";
}
Use implode to basica
...
如果你只需要验证一个字符串(不清理或修改它),你可以使用正则表达式。 尝试以下操作: if (preg_match('/^[0-9\,\;\.]+$/', $string)) {
// ok
} else {
// not ok
}
If you only need to validate a string (not sanitize or modify it), you can use regex. Try the following: if (preg_match('/^[
...
您可以使用此函数来检查数组中的所有字符串是否也在ArrayList中。 如果你想添加额外的逻辑,比如doSomething(),每次找到一个匹配,你应该能够很容易地调整代码。 ArrayList myList; // let's assume its initialized and filled with Strings
String[] strArray; // let's assume its initialized and filled with Strings
//this functi
...
最好的方法我认为是使用正则表达式 Imports System.Text.RegularExpressions
Dim arrayitems As New Regex(arrayitem(0) & "|" & arrayitem(1) & "|" & arrayitem(2))
If arrayitems.IsMatch(largestring) Then
'Exists
'...
End If
另一种选择是使用IndexOf(理论上)比Contains略快 Dim str As
...
使用非捕获组: /(?:date|after|latest) rest of the regex(p)?/
Use non capture group: /(?:date|after|latest) rest of the regex(p)?/
你必须为每个游戏创建子数组,然后在里面设置字符串 // checking it sub array already not exits to not overwrite it
if( isset($array[$game]) ){
$array[$game] = array();
}
然后只在其中插入您的字符串 $array[$game][] = $string1;
$array[$game][] = $string2;
...
结果你会有 array('somegame' =>
...