合并 2 个具有相同长度的数组项
Merge 2 array items with same length
我有一个数组,其中包含一些单词及其长度,类似于:
Array
(
[0] => Array
(
[word] => test
[length] => 4
)
[1] => Array
(
[word] => sets
[length] => 4
)
[2] => Array
(
[word] => foo
[length] => 3
)
)
我需要合并具有相同字长的数组项,例如第一项有 4 个字符的单词测试,第二项有也有 4 个字符长的单词集,所以它们应该像这样合并:
Array
(
[0] => Array
(
[word] => test, sets
[length] => 4
)
[1] => Array
(
[word] => foo
[length] => 3
)
)
我查看了堆栈溢出问题,但找不到解决方案。
如果有人有解决方案,这是我的代码,我真的很感激:
<?php
$words = array();
$length = array();
$words[] = array("word" => "test", "length" => '4');
$words[] = array("word" => "sets", "length" => '4');
$words[] = array("word" => "foo", "length" => '3');
echo '<pre>';
print_r($words);
echo '</pre>';
foreach($words as $key => $test){
$length[$key] = $test['length'];
if($test['length']==$length){
echo 'hello';
}
}
试试吧。
$words = array();
$length = array();
$words[] = array("word" => "test", "length" => '4');
$words[] = array("word" => "sets", "length" => '4');
$words[] = array("word" => "foo", "length" => '3');
foreach($words as $subword){
$is_exist = false;
foreach($length as &$sublength){
if($subword["length"] == $sublength["length"]){
array_push($sublength["word"],$subword["word"]);
$is_exist = true;
}
}
if($is_exist == false){
$new_arr=array("word" => array($subword["word"]), "length" => $subword["length"]);
array_push($length,$new_arr);
}
}
//print $length
foreach($length as $sublength){
foreach($sublength["word"] as $word){
echo $word."-";
}
echo ",".$sublength["length"];echo "<br>";
}
和
结果:
test-sets-,4
foo-,3
好的,我自己找到了解决方案。这是任何有兴趣寻找解决方案的人的代码
$words = array();
$words[] = array("word" => "test", "length" => '4');
$words[] = array("word" => "sets", "length" => '4');
$words[] = array("word" => "foo", "length" => '3');
$new_array = array();
foreach($words as $key => $test){
$length = $test['length'];
if(isset($new_array[$length]['word'])){
$new_array[$length]['word'] = $new_array[$length]['word'].', '.$test['word'];
}else{
$new_array[$length]['word'] = $test['word'];
$new_array[$length]['length'] = $test['length'];
}
}
print_r(array_values($new_array));
它输出:
Array
(
[0] => Array
(
[word] => test, sets
[length] => 4
)
[1] => Array
(
[word] => foo
[length] => 3
)
)
我有一个数组,其中包含一些单词及其长度,类似于:
Array
(
[0] => Array
(
[word] => test
[length] => 4
)
[1] => Array
(
[word] => sets
[length] => 4
)
[2] => Array
(
[word] => foo
[length] => 3
)
)
我需要合并具有相同字长的数组项,例如第一项有 4 个字符的单词测试,第二项有也有 4 个字符长的单词集,所以它们应该像这样合并:
Array
(
[0] => Array
(
[word] => test, sets
[length] => 4
)
[1] => Array
(
[word] => foo
[length] => 3
)
)
我查看了堆栈溢出问题,但找不到解决方案。 如果有人有解决方案,这是我的代码,我真的很感激:
<?php
$words = array();
$length = array();
$words[] = array("word" => "test", "length" => '4');
$words[] = array("word" => "sets", "length" => '4');
$words[] = array("word" => "foo", "length" => '3');
echo '<pre>';
print_r($words);
echo '</pre>';
foreach($words as $key => $test){
$length[$key] = $test['length'];
if($test['length']==$length){
echo 'hello';
}
}
试试吧。
$words = array();
$length = array();
$words[] = array("word" => "test", "length" => '4');
$words[] = array("word" => "sets", "length" => '4');
$words[] = array("word" => "foo", "length" => '3');
foreach($words as $subword){
$is_exist = false;
foreach($length as &$sublength){
if($subword["length"] == $sublength["length"]){
array_push($sublength["word"],$subword["word"]);
$is_exist = true;
}
}
if($is_exist == false){
$new_arr=array("word" => array($subword["word"]), "length" => $subword["length"]);
array_push($length,$new_arr);
}
}
//print $length
foreach($length as $sublength){
foreach($sublength["word"] as $word){
echo $word."-";
}
echo ",".$sublength["length"];echo "<br>";
}
和
结果:
test-sets-,4
foo-,3
好的,我自己找到了解决方案。这是任何有兴趣寻找解决方案的人的代码
$words = array();
$words[] = array("word" => "test", "length" => '4');
$words[] = array("word" => "sets", "length" => '4');
$words[] = array("word" => "foo", "length" => '3');
$new_array = array();
foreach($words as $key => $test){
$length = $test['length'];
if(isset($new_array[$length]['word'])){
$new_array[$length]['word'] = $new_array[$length]['word'].', '.$test['word'];
}else{
$new_array[$length]['word'] = $test['word'];
$new_array[$length]['length'] = $test['length'];
}
}
print_r(array_values($new_array));
它输出:
Array
(
[0] => Array
(
[word] => test, sets
[length] => 4
)
[1] => Array
(
[word] => foo
[length] => 3
)
)