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
Related
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
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);
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
This question already has answers here:
Get the first element of an array
(39 answers)
multidimensional array
(3 answers)
Closed 9 years ago.
I have this:
Array
(
[28] => Array
(
[name] => HTC Touch HD
)
)
There's only one array inside the main array and I only the value of name. Problem is that I don't know the index (28).
You could use array_values just in general to get rid of any weird keys:
$normal = array_values($arr);
$normal[0]['name']
Or in this particular case, end, which is only a little bit hacky:
end($normal)['name']
http://codepad.viper-7.com/cApBjK
(Yep, reset and first and such work too.)
You could also just use
$array = array_pop($array);
And then to get the name element:
$array['name']
You can try something like this:
reset($outerArray);
$innerArray = current($outerArray);
Now you should have access to the value you want.
Pretty self-explanatory :)
<?php
$array = array(
28 => array(
'name' => 'HTC Touch HD'
)
);
$key = current(array_keys($array));
echo '<pre>';
print_r($array[$key]);
echo '</pre>';
?>
If you don't know the structure of an array, you can use foreach construct.
This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I have array that i had to unset some indexes so now it looks like
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
as you can see [2] is missing all i need to do is reset indexes so they show [0]-[3].
Use array_values.
$myarray = array_values($myarray);
$myarray = array_values($myarray);
array_values
array_values does the job :
$myArray = array_values($myArray);
Also some other php function do not preserve the keys, i.e. reset the index.
This might not be the simplest answer as compared to using array_values().
Try this
$array = array( 0 => 'string1', 2 => 'string2', 4 => 'string3', 5 => 'string4');
$arrays =$array;
print_r($array);
$array=array();
$i=0;
foreach($arrays as $k => $item)
{
$array[$i]=$item;
unset($arrays[$k]);
$i++;
}
print_r($array);
Demo