-3

i have an array with some words along with it's length which is something like that:

Array
(
    [0] => Array
        (
            [word] => test
            [length] => 4
        )

    [1] => Array
        (
            [word] => sets
            [length] => 4
        )

    [2] => Array
        (
            [word] => foo
            [length] => 3
        )

)

i need to merge array items that have same word length for example the first item has word test which is 4 chars and the second item has word sets which is also 4 chars long so they should be merge like this:

Array
(
        [0] => Array
            (
            [word] => test, sets
            [length] => 4
        )

    [1] => Array
        (
            [word] => foo
            [length] => 3
        )

)

i looked it around stack overflow but couldn't find a solution. if someone has solution here's my code, i really appreciate:

<?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';
    }
}
Muhammad Aqib
  • 21
  • 1
  • 7

2 Answers2

0

Try it.

$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>";
}

and

result:

test-sets-,4
foo-,3
Black
  • 1
  • 1
  • Thanks Black. It's great but is it possible to have both words in same array value? [0] => Array ( [word] => test, sets [length] => 4 ) – Muhammad Aqib Jul 24 '18 at 17:53
  • see var_dump result (they are in the same array): `array(2) { [0]=> &array(2) { ["word"]=> array(2) { [0]=> string(4) "test" [1]=> string(4) "sets" } ["length"]=> string(1) "4" } [1]=> array(2) { ["word"]=> array(1) { [0]=> string(3) "foo" } ["length"]=> string(1) "3" } }` – Black Jul 24 '18 at 18:47
  • Yes, they are in the same array but in the same value. They should look like this in array value: [word] => test, sets – Muhammad Aqib Jul 24 '18 at 19:57
0

Ok i just found a solution myself. here's the code for anyone interested finding the solution

$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));

and it outputs:

Array
(
    [0] => Array
        (
            [word] => test, sets
            [length] => 4
        )

    [1] => Array
        (
            [word] => foo
            [length] => 3
        )

)
Muhammad Aqib
  • 21
  • 1
  • 7