Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从数组中提取数值,然后将其与另一个字符串组合?_Php_Arrays - Fatal编程技术网

Php 如何从数组中提取数值,然后将其与另一个字符串组合?

Php 如何从数组中提取数值,然后将其与另一个字符串组合?,php,arrays,Php,Arrays,我有一个数组$qwe2,需要从中创建两个单独的数组。在本例中,一个不包含数值的数组mom、sister和一个数值为11dad 13brother的数组 $qwe = " mom 11dad sister 13brother "; $qwe0 = ucwords(strtolower($qwe)); $qwe1 = preg_replace('/\s+/', ' ',$qwe); $qwe7 = trim($qwe1); $qwe2 = explode(' ',$qwe7); var_d

我有一个数组$qwe2,需要从中创建两个单独的数组。在本例中,一个不包含数值的数组mom、sister和一个数值为11dad 13brother的数组

$qwe = " mom   11dad  sister  13brother ";
$qwe0 = ucwords(strtolower($qwe));
$qwe1 = preg_replace('/\s+/', ' ',$qwe);
$qwe7 = trim($qwe1);
$qwe2 = explode(' ',$qwe7);

var_dump($qwe2);
这就是它看起来的样子:

array (size=4)
  0 => string 'mom' (length=3)
  1 => string '11dad' (length=5)
  2 => string 'sister' (length=6)
  3 => string '13brother' (length=9)
所有这些都是需要的,但我很容易做到。我不明白下面的部分

所需结果:
$asd=数组(“妈妈,妹妹”)
$zxc=array(“11dad,13brother”)

另外,我还有一个字符串
$doyou=“你喜欢吗?”
,我需要将它与新数组
$asd
组合,这将导致:
你喜欢妈妈吗?你喜欢妹妹吗?


提前谢谢

将PHP的
array\u filter()
与一些检查字符串中数字的自定义函数一起使用:

$asd = array_filter($qwe2, 'hasNumbers');
$zxc = array_filter($qwe2, 'hasNoNumbers');

function hasNumbers($string)
{
    return strcspn($string, '0123456789') != strlen($string);
}
function hasNoNumbers($string)
{
    return strcspn($string, '0123456789') == strlen($string);
}
然后
array\u map()
可以帮助您执行字符串替换:

echo implode(', ', array_map('myStringReplace', $asd));

function myStringReplace($string)
{
    return str_replace('?', $string, 'Do you like ?');
}

到目前为止你试过什么?在第一个数组中选择“妈妈”和“妹妹”的规则是什么?第二个数组中的其他值呢?我尝试了array_slice,并尝试按位置访问数组的每个元素,但效果不太好。所以我在一个数组中需要数值(11dad,13 brother),在第二个数组中不需要数值(mom,sister),我们知道你们需要什么;但是你需要表现出一些努力来解决你自己的问题,而不仅仅是让别人帮你做作业。这很有效。我只需要想想如何内爆$doyou和$asd。谢谢。@Kotidrmakavez我刚刚添加了一个解决方案。对不起,第一次错过了。太棒了。再次感谢!