Concatenate values of n arrays in php
You can put all word arrays into one array and use a recursive function like this:
function concat(array $array) {
$current = array_shift($array);
if(count($array) > 0) {
$results = array();
$temp = concat($array);
foreach($current as $word) {
foreach($temp as $value) {
$results[] = $word . ' ' . $value;
}
}
return $results;
}
else {
return $current;
}
}
$a = array(array('dog', 'cat'), array('food', 'tooth'), array('car', 'bike'));
print_r(concat($a));
Which returns:
Array
(
[0] => dog food car
[1] => dog food bike
[2] => dog tooth car
[3] => dog tooth bike
[4] => cat food car
[5] => cat food bike
[6] => cat tooth car
[7] => cat tooth bike
)
But I guess this behaves badly for large arrays as the output array will be very big.
To get around this, you can output the combinations directly, using a similar approach:
function concat(array $array, $concat = '') {
$current = array_shift($array);
$current_strings = array();
foreach($current as $word) {
$current_strings[] = $concat . ' ' . $word;
}
if(count($array) > 0) {
foreach($current_strings as $string) {
concat($array, $string);
}
}
else {
foreach($current_strings as $string) {
echo $string . PHP_EOL;
}
}
}
concat(array(array('dog', 'cat'), array('food', 'tooth'), array('car', 'bike')));
Which gives:
dog food car
dog food bike
dog tooth car
dog tooth bike
cat food car
cat food bike
cat tooth car
cat tooth bike
With this approach it is also easy to get the "sub-concatinations". Just insert echo $string . PHP_EOL;
before concat($array, $string);
and the output is:
dog
dog food
dog food car
dog food bike
dog tooth
dog tooth car
dog tooth bike
cat
cat food
cat food car
cat food bike
cat tooth
cat tooth car
cat tooth bike
php concat array values into new array php
The idea is get the data by column, you're in luck, there's a built in function for that. Its array_column
.
So first, get the number of columns and simply use a for loop for that. Then just use implode
and assign it inside a new container:
$new = array(); // container
$count = count($arr[0]); // get the number of colums
for ($i = 0; $i < $count; $i++) {
// get the data by column number ($i), then implode and push inside
$new[] = implode(',', array_column($arr, $i));
}
Here's a sample output
Merge values from multiple arrays in PHP
Use array_merge()
to combine arrays, not array_push()
.
Remove the duplicates at the end after merging everything.
$json = $this->curl_get_marketplace_contents();
$data = json_decode($json, true);
$categories = array();
foreach ($data['themes'] as $theme) {
$array = explode(",", $theme['categories']);
$array = array_map('trim', $array);
$categories = array_merge($array, $categories);
};
return array_unique($categories);
Can't concatenate 2 arrays in PHP
Both will have a key of 0
, and that method of combining the arrays will collapse duplicates. Try using array_merge()
instead.
$arr1 = array('foo'); // Same as array(0 => 'foo')
$arr2 = array('bar'); // Same as array(0 => 'bar')
// Will contain array('foo', 'bar');
$combined = array_merge($arr1, $arr2);
If the elements in your array used different keys, the +
operator would be more appropriate.
$arr1 = array('one' => 'foo');
$arr2 = array('two' => 'bar');
// Will contain array('one' => 'foo', 'two' => 'bar');
$combined = $arr1 + $arr2;
Edit: Added a code snippet to clarify
Concatenate the values from one array to another in PHP
Presuming you want to use the array you created as the array to use to append to. I can't see why you'd need to work with specific characters other than that in the array (I may be wrong, but this can be easily adapted to cater for that).
/**
* @param array $array
* @param $length
* @param null $original
* @return array
*/
function generate_values(array $array, $length, $original = null) {
// If length is 1 or less just return the array
if ($length <= 1) {
return $array;
}
// The resulting values array
$result = [];
// Copy the array if original doesn't exist
if (!is_array($original)) {
$original = $array;
}
// Loop over each item and append the original values
foreach($array as $item) {
foreach($original as $character) {
$result[] = $item . $character;
};
}
// Recursively generate values until the length is 1
return generate_values($result, --$length, $original);
}
To use it you can use your generator.
$characterArray = generate_characters(true, false, false);
$results = generate_values($characterArray, 2);
PHP concatenate values into array
Try something like this. If this is what you need. Here in the below codes $mappingId
contains array of individual appDate
, appTime
, appDoctorId
in array format. If you don't want array just want concatenate those value with one element then also you can do that. Please let me know!
$mappingId = array(); // combined values
for ($i = 0; $i < mysqli_num_rows($resultAppointmentsBooked); $i++)
{
$row = mysqli_fetch_row($resultAppointmentsBooked);
$appId = $row[0];
$appTime = $row[3];
$appDate = $row[4];
$appDoctorId = $row[2];
$myArr = array();
//array_push($myArr, $appDate, $appTime, $appDoctorId);
// If you want in concatenate format
array_push($myArr, $appDate."".$appTime."".$appDoctorId);
array_push($mappingId, $myArr);
}
how do I concatenate the string values of two arrays pairwise with PHP?
You could do it with array_map
:
$combined = array_map(function($a, $b) { return $a . ' ' . $b; }, $a1, $a2));
Related Topics
Does the Order of Class Definition Matter in PHP
How to Use Array_Unique on an Array of Arrays
How to Handle Diacritics (Accents) When Rewriting 'Pretty Urls'
Output Text File with Line Breaks in PHP
PHP - Merge Two Arrays (Same-Length) into One Associative
How to Remove 'Index.Php' from Url in Codeigniter
Laravel 5 - Env() Always Returns Null
How to Get CSV File to Download on Ie? Works on Firefox
How to Call a PHP Function from Ajax
PHP - Returning the Last Line in a File
Do I Really Need to Do MySQL_Close()