Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jasonb's avatar

Array to string conversion ??

I need to take the valules of an array and convert it to a string that looks like this:

$links = “http://google.com\nhttp://yahoo.com”;

Here is my code:

 $links = Link::whereStatus(false)->get(['url'])->toarray();

            $links = implode(' ', $links);
            dd($links);

First off I get this error: Array to string conversion

I am not sure what is going on.

0 likes
12 replies
bashy's avatar

$links = an array and you want to make it a string with a space?

1 like
bestmomo's avatar

Maybe :

$links = implode(' ', array_values($links));
Ozan's avatar

@jasonb You don't even need this:

$links = implode(' ', $links);

Delete that.

davorminchorov's avatar

Access the array values and then do whatever you gotta do to the string.

1 like
jasonb's avatar

I want to take the array and create a variable that has the values from the array VALUE\nVALUE\nVALUE\nVALUE

When I tried to take my array and create that variable I got that error.

$links = implode(' ', $links);

ErrorException in LinkController.php line 43: Array to string conversion

davorminchorov's avatar
$links = Link::whereStatus(false)->get(['url'])->toArray();
foreach($links as $link)
{
    echo $link . ' \n';
}
1 like
usman's avatar

@jasonb just reduce the collection that you get from the ->get(['url']) call:

$urlString = Link::whereStatus(false)->get(['url'])->reduce(function($previous, $value) { 
    if( ! $previous) return $value->url; 
    return $previous . '\n' . $value->url;
});

Usman.

jasonb's avatar

@usman That is perfect.. It gives me the result I need.

So get this, I am working with an API that says to: Links String[] Yes Uploaded to the POSTs Request Body. Every link must be on a new line. Minimum of 10 links without BatchID. Example:

$links = “http://google.com\nhttp://yahoo.com”;

If I submit that to the API with the above $links I get Response : {"LinksAdded":2}

If I create $ links with the code you supplied:

$links = Link::whereStatus(false)->get(['url'])->reduce(function($previous, $value) { 
    if( ! $previous) return $value->url; 
    return $previous . '\n' . $value->url;
});

I get this: Response : {"LinksAdded":1}

For some reason if I try to build the variable the API takes the string and is unable to parse it and all URLs appear as one string. If I build it out manually it works. What the heck if the difference?

jasonb's avatar

@usman That is perfect.. It gives me the result I need but there is a problem.

So get this, I am working with an API that says to: Links String[] Yes Uploaded to the POSTs Request Body. Every link must be on a new line. Minimum of 10 links without BatchID. Example:

$links = “http://google.com\nhttp://yahoo.com”;

If I submit that to the API with the above $links I get Response : {"LinksAdded":2}

If I create $ links with the code you supplied:

$links = Link::whereStatus(false)->get(['url'])->reduce(function($previous, $value) { 
    if( ! $previous) return $value->url; 
    return $previous . '\n' . $value->url;
});

I get this: Response : {"LinksAdded":1}

For some reason if I try to build the variable the API takes the string and is unable to parse it and all URLs appear as one string. If I build it out manually it works. What the heck if the difference?

jasonb's avatar

I really need some major help here. =)

usman's avatar

@jasonb hmm try using the double quotes around \n:

$links = Link::whereStatus(false)->get(['url'])->reduce(function($previous, $value) { 
    if( ! $previous) return $value->url; 
    return "$previous\n$value->url";
});

Usman.

jasonb's avatar

Yes.. =) that was it..


$links = implode('\n', array_flatten($links));

// *should be*

$links = implode("\n", array_flatten($links));
1 like

Please or to participate in this conversation.