How to cast array elements to strings in PHP?
A one-liner:
$a = array_map('strval', $a);
// strval is a callback function
See PHP DOCS:
array_map
strval
Convert the first element of an array to a string in PHP
Is there any other way to convert that array into string ?
You don't want to convert the array to a string, you want to get the value of the array's sole element, if I read it correctly.
<?php
$foo = array( 18 => 'Something' );
$value = array_shift( $foo );
echo $value; // 'Something'.
?>
Using array_shift you don't have to worry about the index.
EDIT: Mind you, array_shift is not the only function that will return a single value. array_pop( ), current( ), end( ), reset( ), they will all return that one single element. All of the posted solutions work. Using array shift though, you can be sure that you'll only ever get the first value of the array, even when there are multiple.
Best way to convert array into string with PHP keeping Keys and Values
Put all the key (value)
strings in an array, then call implode()
to combine them.
$a = [];
foreach ($array as $key => $value) {
$a[] = "$key ($value)";
}
$result = implode(' + ', $a);
How to convert array to string in laravel?
print_r($request->education); //It is an array print
$str_json = json_encode($request->education); //array to json string conversion
echo $str_json; // printing json string
print_r(json_decode($str_json)); //printing array after convert json string to array
exit; // exiting further execution to check resutls
Convert array keys and values to a string in PHP
If we want to keep your code for grouping:
function group_by($key, $data)
{
$result = [];
foreach ($data as $val) {
if (array_key_exists($key, $val)) {
$result[$val[$key]][] = $val;
} else {
$result[""][] = $val;
}
}
return $result;
}
$byGroup = group_by("gender", $data);
echo "#1
gender:male
name:
";
foreach ($byGroup['Male'] as $person) {
echo $person['name'],"\n";
}
echo "-----------
#2
gender:female
name:
";
foreach ($byGroup['Female'] as $person) {
echo $person['name'],"\n";
}
Else, I'd simply do:
foreach ($data as $person) {
if ($person['gender']==='Male'){
$m .= $person['name'] . "\n";
} else {
$f .= $person['name'] . "\n";
}
}
echo "#1
gender:male
name:
$m
-----------
#2
gender:female
name:
$f";
convert array into array to string is possible..?
You can use array_column() and implode() function to do this.
Here is how you can do it,
$values=array_column($array,"country_id");
$country_string=implode($values);
array_column() returns the values from a single column of the input,
identified by the column_key(country_id).implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between
each element.
implode() can have two arguments, first as glue by which you want to join the elements and second as the array of elements. If first argument is not given then default glue is ",".
Convert flat array to a delimited string to be saved in the database
Use implode
implode("|",$type);
Convert array objects to string and separate the values
You are missing the fact that you are dealing with an array of objects.
Looks like you can achieve that by doing:
$output = array_map(function ($object) { return $object->name; }, $input);
echo implode(', ', $output);
How to convert array of values into strings in laravel?
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$bookgetterPrices = json_decode($this->bookgetter2);
$totalPrice = 0;
foreach ($bookgetterPrices as $p) {
$totalPrice += $p->price;
}
$bookgetter1 = implode(',', array_map(function($x) { return $x->name; }, json_decode($this->bookgetter1)));
$bookgetter2 = implode(',', array_map(function($x) { return $x->price; }, $bookgetterPrices));
$bookgetter3 = implode(',', array_map(function($x) { return $x->author; }, json_decode($this->bookgetter3)));
return (new MailMessage)
->line("You'r order has been placed successfully.. ")
->line('This is the order-id keep it furthur!')
->with($this->orderNumber)
->with($totalPrice)
->with($bookgetter1)
->with($bookgetter2)
->with($bookgetter3)
}
Related Topics
Laravel 4 Custom Named Password Column
PHP Array Find Duplicates, Sum Them Up & Delete Duplicates
Post to a Facebook Page Without "Manage_Pages" Permission Using PHP
How Best to Get Someone's 'Rank' from a Scores Table with PHP and MySQL Without Looping
What Is Null Coalescing Assignment = Operator in PHP 7.4
Overriding Upload_Max_Filesize
How to Create a Base64Encoded String from Image Resource
Using Variables in MySQL Update (Php/Mysql)
Filling Gaps in Dates Returned from Database - Pure SQL Solution Possible
Performance in Pdo/Php/Mysql: Transaction Versus Direct Execution
Access Joomla 2.5 from External Script to Get Article by Id
Looping Through Query Results Multiple Times with MySQLi_Fetch_Array
PHP SQL, Query Returns Only One Row of Data
Protection Against Xss Exploits