$links = an array and you want to make it a string with a space?
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.
Maybe :
$links = implode(' ', array_values($links));
Access the array values and then do whatever you gotta do to the string.
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
@bashy Yes.
$links = Link::whereStatus(false)->get(['url'])->toArray();
foreach($links as $link)
{
echo $link . ' \n';
}
@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.
@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?
@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?
I really need some major help here. =)
@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.
Yes.. =) that was it..
$links = implode('\n', array_flatten($links));
// *should be*
$links = implode("\n", array_flatten($links));
Please or to participate in this conversation.