Right now i am combining two arrays, but they are with different number of elements.
现在我正在合并两个数组,但是它们有不同数量的元素。
Here is the code:
这是代码:
preg_match_all('/\"SizeName\":\"([0-9.]+)\"/',$str,$matches);
preg_match_all('/\"SellPrice\":\"([0-9.]+)\"/',$str,$matches1);
echo '<pre>';
foreach (array_combine($matches[0], $matches1[0]) as $ht => $name) {
echo "$ht - $name<br>";
}
echo '</pre>';
I want to ask how i can limit the combine with the lowest number of elements array ?
我想问的是如何限制元素数组的最小数量?
I saw an advice for resolving this problem with a function like this:
我看到了一个解决这个问题的建议:
function array_combine2($arr1, $arr2) {
$count = min(count($arr1), count($arr2));
return array_combine(array_slice($arr1, 0, $count), array_slice($arr2, 0, $count));
}
But my question is, does this function will help me and how i can implement it in my code so it will not give me output error:
但我的问题是,这个函数是否会帮助我,以及我如何在我的代码中实现它,这样它就不会给我输出错误:
Warning: array_combine() [function.array-combine]: Both parameters should have an equal number of elements
警告:合二为一()函数。:两个参数都应该有相同数量的元素
Thanks in advance!
提前谢谢!
1 个解决方案
#1
4
"does this function will help me"? Yes
这个功能对我有帮助吗?是的
"how i can implement it in my code so it will not give me output error"?
“我如何在代码中实现它,使它不会给我输出错误”?
- Paste the function definition for
array_combine2
in your code. - 在代码中粘贴array_梳状2的函数定义。
- Change
array_combine
toarray_combine2
in yourforeach
. - 将array_combine更改为foreach中的array_梳状2。
#1
4
"does this function will help me"? Yes
这个功能对我有帮助吗?是的
"how i can implement it in my code so it will not give me output error"?
“我如何在代码中实现它,使它不会给我输出错误”?
- Paste the function definition for
array_combine2
in your code. - 在代码中粘贴array_梳状2的函数定义。
- Change
array_combine
toarray_combine2
in yourforeach
. - 将array_combine更改为foreach中的array_梳状2。