0

I want to remove the whitespace between the arrays, but I've used different codes like trim but it does not remove. I think because trim is fro " " outside space and not between the words itself. I'm using PHP.

This is for R, but something like this: How to remove all whitespace from a string?

I've changed my code to this:

<?php

function combinations($arr, $n)
{
    $res = array();

    foreach ($arr[$n] as $item)
    {
        if ($n==count($arr)-1)
            $res[]=$item;
        else
        {
            $combs = combinations($arr,$n+1);

            foreach ($combs as $comb)
            {
                $res[] = "$item $comb";
            }
        }
    }
    return $res;
}

$words = array(array(
'PY7AD022031',
'AD022031',
'CB5A09XQXU',
),array(
'HELLO', 
'3040',
'3022031',
'07W11',
'4170B',
'0682',
'35570401',
'103448',
), array(
'HELLO', 
'3040',
'3022031',
'07W11',
'4170B',
'0682',
'35570401',
'103448',
));

$combos = combinations($words,0);  

$comma_separated = implode("<br />", $combos);
print("<pre>".print_r($comma_separated,true)."</pre>");
//var_dump($combos);
?>

It echo's

PY7AD022031 HELLO HELLO
PY7AD022031 HELLO 3040
PY7AD022031 HELLO 3022031
PY7AD022031 HELLO 07W11
PY7AD022031 HELLO 4170B

But I want

PY7AD022031HELLOHELLO
PY7AD022031HELLO3040
PY7AD022031HELLO3022031
PY7AD022031HELLO07W11
PY7AD022031HELLO4170B
Community
  • 1
  • 1
MOTIVECODEX
  • 2,624
  • 14
  • 43
  • 78
  • 1
    try [implode](http://php.net/implode) – hjpotter92 Jun 23 '12 at 17:08
  • [str_replace](http://www.php.net/str_replace) with a loop dude...or just edit the combinations() function – goat Jun 23 '12 at 17:09
  • @rambocoder I've added the combination function, any idea what to change? – MOTIVECODEX Jun 23 '12 at 17:17
  • @T-ShirtDude `$comma_separated = implode("
    ", $combos); print("
    ".print_r($comma_separated,true)."
    ");` Prints it like `PY7AD022031 HELLO HELLO` `PY7AD022031 HELLO 3040` `PY7AD022031 HELLO 3022031` `PY7AD022031 HELLO 07W11` `PY7AD022031 HELLO 4170B` But how to remove the spacing between the words?
    – MOTIVECODEX Jun 23 '12 at 17:23
  • Side-note: "ALWAYS call it with 0 as the last parameter"... why not just make the parameter optional? `function combinations($arr,$n=0) {...}` and then just call it with the array. – Niet the Dark Absol Jun 23 '12 at 17:29
  • @F4LLCON Try `implode("", $combos);` – hjpotter92 Jun 23 '12 at 17:43
  • @T-ShirtDude actually `implode("
    ", $combos);` is better. Without `
    ` it will do the same as with `
    ` but everything is on one line. both ways will not remove the whitespace between ONE combination. That's what I want
    – MOTIVECODEX Jun 23 '12 at 17:53
  • 1
    check the reply by mo3lyana. That's exactly what the problem is. – hjpotter92 Jun 23 '12 at 17:56

3 Answers3

3

it's simple just change your syntax

$res[] = "$item $comb";

into this syntax :

$res[] = "$item$comb";

remove whitespace in your $res.

have you try it?


http://nanamo3lyana.blogspot.com/

mo3lyana
  • 56
  • 2
0

Don't use print_r() because the function itself will add spaces, no matter what you're doing...

Just loop over the arrays and print all the elements.

foreach($words as $element) {

    if (is_array($element)) {
        foreach($element as $v) {
            echo trim($v);
        }
    }

    echo '<br />';
}
napolux
  • 15,574
  • 9
  • 51
  • 70
  • With the print_r it shows 191 combinations, but with your code it only shows `PY7AD022031AD022031CB5A09XQXU HELLO3040302203107W114170B068235570401103448 HELLO3040302203107W114170B068235570401103448` I need everything to show normally but I want to remove the whitespace between each word, but don't want to delete lines – MOTIVECODEX Jun 23 '12 at 17:14
0

I use print_r only for debuging, try this

foreach($words as $element) 
{
    if(is_array($element))
    {
      echo implode($element) . "<br />";
    }
    else
    {
      echo $element . "<br />";
    }
}
GrayB
  • 1,010
  • 8
  • 22
  • Also not working: it echo's `PY7AD022031AD022031CB5A09XQXU HELLO3040302203107W114170B068235570401103448 HELLO3040302203107W114170B068235570401103448`. And that's it, but i have a total of 191 combinations. – MOTIVECODEX Jun 23 '12 at 17:21
  • Do you have arrays in arrays? That looks like the data from the question. Are you using the right data? – GrayB Jun 23 '12 at 17:24
  • I've changed my question.. maybe it's better this way – MOTIVECODEX Jun 23 '12 at 17:27