How to join items of multidimensional array using php? [duplicate] - php

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 8 months ago.
I have an array as shown bottom
array (
[det1] => 1,2,3,4
[det2] => 5,6
);
So i want to join items of this array and convert array to an string like bottom
$uru = 1,2,3,4,5,6
How can i do this work?

Did you try like this with implode()
<?php
$arr = array('det1'=>'1,2,3,4', 'det2'=>'5,6');
$uru = implode(',',$arr);
echo $uru;
?>
DEMO: https://3v4l.org/UBQrv

You say multidimensional array, but your array is single dimensional with strings?
I assume that is a typo and your array is multidimensional.
In that case loop the array and merge the new array with the subarray.
$arr = array (
"det1" => [1,2,3,4],
"det2" => [5,6]
);
$new= [];
foreach($arr as $sub){
$new = array_merge($new, $sub);
}
echo implode(",",$new); // 1,2,3,4,5,6
https://3v4l.org/NaDXN

This can be easily done by extracting into array_merge and then joining the resulting array.
$data = [
[1,2,3,4,5],
[6,7,8],
];
echo join(',', array_merge(... $data));
Output:
1,2,3,4,5,6,7,8

Related

Php reverse array is not working [duplicate]

This question already has answers here:
Reverse an associative array with preserving keys in PHP
(4 answers)
Closed 5 years ago.
I am developing a new website, and I have a quetion.
Input array:
Array ( [1319] => ####,[1316] => ###)
I have an array and I want to revese him, after the reverse the array would be like this:
Expected output:
Array ( [1316] => ###,[1319] => ####)
but when i'm using array_reverse function, it doesnt work for me, I got this array:
Array ( [0] => ###,[1] => ####)
Why it is happen?
For preserving keys you just pass second parameter to true in array_reverse.
Try this code snippet here
$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
you can try this:
$a = []; //your array
$keys = array_keys($arr);
$values = array_values($arr);
$rv = array_reverse($values);
$newArray = array_combine($keys, $rv);

Remove duplicate values from a multidimensional array in PHP [duplicate]

This question already has answers here:
Flatten a multidimensional array and remove duplicate values [duplicate]
(3 answers)
Closed 8 years ago.
How can I remove duplicate values from a multidimensional array in PHP like below.
I tried Remove duplicate value in multidimensional array. But did not solve my problem.
Array(
[0] => outdoor
[1] => indoor
)
Array(
[0] => indoor
)
Result should be a single array like below :
array(outdoor,indoor);
finally i found the result from Remove duplicate value in multidimensional array. I'll share them for other users.
$result = array_unique(call_user_func_array('array_merge',$result2));
Use array_unique to remove duplicates from a single array.
Use array_merge to combine arrays.
Try:
array_unique(array_merge($array1,$array2), SORT_REGULAR);
$array = array(array("outdoor","indoor"),array("indoor"));
$result = array_unique($array);
print_r($result[0]);
Demo
Try this:
<?php
$Arr = array(array('outdoor','indoor'),array('indoor'));
$result = array_unique($Arr);
$newArr = $result[0];
echo '<pre>';
print_r($newArr);
?>
The result will be
Array
(
[0] => outdoor
[1] => indoor
)
--
Thanks

How to add strings of two arrays [duplicate]

This question already has answers here:
PHP - Merge two arrays (same-length) into one associative?
(4 answers)
Closed 8 years ago.
I have two array like these.
array("a","b","c");
array("1","2","3");
I have to add those items like that way.
$c=array("a"=>"1","b"=>"2","c"=>"3");
how can i retrieve each inner array by this way.
echo $c[0];
Use array_combine()
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
<?php
$a = array("a","b","c");
$b = array("1","2","3");
$c = array_combine($a, $b);
print_r($c);
?>
The above example will output:
Array
(
[a] => 1
[b] => 2
[c] => 3
)
$new_array = $array;
array_unshift($new_array, $value1);
array_push($new_array, $value2);
Use array_merge method for combining two arrays . Array_merge($arr1'$arr2);

How to move the keys of array to the top in PHP [duplicate]

This question already has answers here:
PHP reindex array? [duplicate]
(4 answers)
Closed 9 years ago.
I have got an array
$array = array(
0=>'c'
1=>'a',
2=>'b',
3=>'d',
);
Now, i am going to unset the 1 and 2 keys.
unset($array[1]); unset($array[2]);
How to do if i want an array
array(0=>'c', 1=>'d');
Try This:
$array = array_values($array);
Try
unset($array[1]);
unset($array[2]);
$array = array_values($array);
This works because array_values returns the array you gave, but with increasing keys starting at 0.
Have you tired to use something like that ?
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
array_values
$myarray = array_values($myarray);
btw here is a good answer posted

How to Add two array values in php? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Merge 2 Arrays and Sum the Values (Numeric Keys)
I have two arrays both have same keys, i want to add key values,
Example
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
want to create new array which add these two array values
like
$arr3 = array("first"=>30,"second"=>50);
How can i do that in one line, i can do it using forach or other technique, but wondering if some one know a good way of doing?
Example for string keys (note that no error checking is done!):
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
$arr3 = $arr1;
array_walk($arr3, function (&$val, $key, $foo) { $val += $foo[$key]; }, $arr2);
print_r($arr3);
/*Array
(
[first] => 30
[second] => 50
)*/

Categories