1
implode(',', $a);

I want to attach the $q variable in front of the $a so like this

implode(',', $q.$a); 

But that doesn`t work. How can i put 2 variables in a implode function?

$a is an array with domain names like "com, org" and $q is the text (string) you type in before the domain names will appear.

I get the following error:

invalid argument passed in line..

Whole code:

$a = ['nl','net','com','co'];
$q = $_REQUEST["q"];

$domain = explode(".", $q);
$ext = @$domain[1] ?: ' ';


if (empty($ext)) {
    echo implode(',',$a);
} else if (in_array($ext, $a)) {
    echo $q;
} else {
    $r = [];
    foreach ($a as $x) {
        if (strstr($x, $ext)) {
            $r[] = $x;
        }
    }
    echo (count($r)) ? implode(',',$r) : implode(',',$a);
}
8
  • 2
    What are $a and $q ? And what error do you get? Commented Oct 24, 2018 at 12:51
  • $a is an array with domain names like "com, org" and $q is the text you type in before the domain names wil appear. And the error is "invalid argument passed in line.." Commented Oct 24, 2018 at 12:54
  • Can you Put $a , $q values and your expected result? Commented Oct 24, 2018 at 12:54
  • So $a is an array and $q is an other array? Commented Oct 24, 2018 at 12:55
  • How am I suppose to know what $_REQUEST["q"] is? Is it an array? Is it a string? What is the desired output because it's not clear? Please explain what you get (value of $a and $q) and what is the output you want. Commented Oct 24, 2018 at 12:58

4 Answers 4

2

If $a is an array and $q is the prefix string you can achieve that with 2 steps:

Add the prefix with:

$a = array("com", "co");
$q = "robot.";
foreach ($a as &$value)
    $value = $q.$value;

Second, use the implode:

echo implode(',',$a);

output is:

robot.com,robot.co

Edited

I think this will be more suitable for you:

$a = array("com", "co", "org");
$q = "robot.c";
$arr =  explode(".", $q);

$output = array();
foreach ($a as &$value) {
    if (substr($value, 0, strlen($arr[1])) === $arr[1])
        $output[]= $arr[0] . "." . $value;
}
echo implode(',',$output);

In this code you take the domain prefix and search for all domain name that may be fit for the prefix.

Notice that is this example we have domain org but he does not show up because your prefix is c

Sign up to request clarification or add additional context in comments.

6 Comments

This is almost what I want, what I get now if I type in robot.c. "robot.ccom, robot.cco" etc.
@KoenM I guess you can add this $q = substr($q, 0, strpos($q, ".") + 1);
Can you check out my whole code I edited it in and tell me which things I have to change. Because you are the one that knows what I want to have.
@KoenM edited my answer - I think now it will answer you
Thanks man. sorry but I am a beginner, how do I make it so it won`t show robot.c anymore I used $q = $_REQUEST["q"]; but it says undefind offset line 9 @David Winder
|
1

You need to add your $q before implode function. You can add your $q into your $a array using array_map function.

$array = array('com', 'org', 'net');
$q = 'test';
$array = array_map(function($value) { 
                    $q= "test"; // you $q value goes here.
                    return $q.".".$value; 
         }, $array);

echo implode(',',$array);

Comments

0

Assuming that your business logic guarantees that $a is never empty, then here is the most direct, efficient, and concise technique...

Code: (Demo)

$a = ['nl', 'net', 'com', 'co'];
$q = 'example';

echo "$q." . implode(",$q.", $a);

Output:

example.nl,example.net,example.com,example.co

See how there is no need to loop or call any additional functions?

This basic technique of expanding the delimiting glue can be seen in other usages like:

echo '<tr><td>' . implode('</td><td>', $array) . '</td></tr>';

The idea is that you prepend what you need before the first value, then you declare the glue to be the characters that you need to occur after each value AND before the next value.

Comments

0

Merge an array with a string variable in PHP with the help of implode function

$a = array("com", "co");
$q = "robot.c";
$temp = explode(".",$q);
foreach ($a as $value)
    echo $value = $temp[0].".".$value;

1 Comment

"Try this" answers are low value on Stack Overflow because they do very little to educate/empower thousands of future researchers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.