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
Related
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:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 8 years ago.
I have array A :
Input:
A ={2,3,2,{1},3,2,{0},3,2,0,11,7,9,{2}}
I want output to be Array B
Output:
B={0,1,2,3,7,9,11}
How can i remove the duplicate values and sort them ascending with PHP?
if you have:
$a = array(2,3,2,1,3,2,0,3,2,0,11,7,9,2);
you can use array_unique() to remove duplicates:
$a = array_unique($a);
and then use asort() to sort the array values:
asort($a);
//Try this out...
$array = array(2,3,2,(1),3,2,(0),3,2,0,11,7,9,(2));
$array_u = array_unique($array);
sort($array_u);
print_r($array_u);
Sample output
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 7
[5] => 9
[6] => 11
)
First step: flattern
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}
then using asort and array_unique you can remove duplicates and sort ascendent.
$result = array_unique(flattern($array));
asort($result);
Sources:
How to Flatten a Multidimensional Array?
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
This question already has answers here:
PHP Change Array Keys
(12 answers)
Closed 8 years ago.
Hi I have an array like below.
$arr = Array ( [My_name] => Sam [My_location] => United_Kingdom [My_id] => 1 );
And im trying to change the keys from
My_name, My_Location, My_id
to
Your_name, Your_Location, Your_id
So the final array would look like
Array ( [Your_name] => Sam [Your_location] => United_Kingdom [Your_id] => 1 );
I was hoping something like str_replace would work
$arrnew = str_replace("My","Your",$arr);
But this is only replacing "My" to "Your" if "My" is a value, not a key.
So how would I change the keys?
Thanks for any help.
$arrnew = array_combine(str_replace("My","Your",array_keys($arr)), $arr);
you cannot change the keys in-place but you can do something like this:
foreach($arr as $key => $value) {
$arr[str_replace("My","Your",$key)] = $value;
unset($arr[$key]);
}
This will add a new element with the new keys unsetting the old element
Hope this helps
You could try this:
foreach($arr as $key => $val){
$newkey = str_replace("My","Your",$key);
unset($arr[$key]);
$arr[$newkey] = $val;
}
Demo: http://codepad.org/3vKkmAXx
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