This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
How to remove duplicate values from an array in PHP
(27 answers)
Closed 9 years ago.
First i will like to say that, i have looked into other post but failed trying to accomplish my needs. I have used array_unique($array) but the duplicates don't get discarded. This is a view of my array using var_dump:
{
[0]=>
string(12) "44.94.192.40"
}
array(1) {
[0]=>
string(12) "44.94.192.41"
}
array(1) {
[0]=>
string(9) "44.94.1.1"
}
array(1) {
[0]=>
string(9) "44.94.1.1"
}
array(1) {
[0]=>
string(13) "44.96.253.100"
}
"44.94.1.1" is a duplicate which i hope to remove but i can't. Does this have to do with my array structure ?
Edited in response to your comment:
From the documentation on array_unique():
Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
So the fact that each of your values is an array is interfering.
Try creating an array containing just the string values:
$new_array = array();
foreach ($array as $value) {
$new_array[] = $value[0];
}
$new_array = array_unique($new_array);
You can flatten your array and do array unique or do array_map with a reference to a foreign array
<?php
$data = array(
array("44.94.192.40"),
array("44.94.192.41"),
array("44.94.1.1"),
array("44.94.1.1"),
array("44.96.253.100"),
);
$visited = array();
array_map(function($v) use ( &$visited ){
if(array_search( $v[0], $visited ) === false){
$visited[] = $v[0];
};
},$data);
var_dump($visited);
array(4) {
[0]=>
string(12) "44.94.192.40"
[1]=>
string(12) "44.94.192.41"
[2]=>
string(9) "44.94.1.1"
[3]=>
string(13) "44.96.253.100"
}
You can find a variety of ways to remove duplicates from a multidimensional array on the php doc page of array_unique in the user comments
http://us3.php.net/array_unique
Orel also mentioned a duplicate question that at a glance also contains a function for doing the same
How to remove duplicate values from an array in PHP
For simplicity I have picked one for you, but depending on exactly what you want, there are many different flavors on the php page I linked
foreach ($arrAddressList AS $key => $arrAddress) {
$arrAddressList[$key] = serialize($arrAddress);
}
$arrAddressList = array_unique($arrAdressList);
foreach ($arrAddressList AS $key => $strAddress) {
$arrAddressList[$key] = unserialize($strAddress);
}
You can do it with a foreach loop
$array = array(
array("44.94.192.40"),
array("44.94.192.41"),
array("44.94.1.1"),
array("44.94.1.1"),
array("44.96.253.100"),
);
$temp_arr=array();
foreach($array as $elementKey => $element)
{
foreach($element as $valueKey => $value)
{
if(in_array($value,$temp_arr)) unset($array[$elementKey]);
else $temp_arr[]=$value;
}
}
print_r($array);
Output would be:
Array ( [0] => Array ( [0] => 44.94.192.40 ) [1] => Array ( [0] => 44.94.192.41 ) [2] => Array ( [0] => 44.94.1.1 ) [4] => Array ( [0] => 44.96.253.100 ) )
Related
For example
[Nationality_1] string(8)=>Indian [Nationality_5] string(12)=>American [Nationality_12] string(17)=>Japanese
I got these array values by foreach loop but I want to put these value to single array by index on strings
Desired output
[Nationality] [0] string(8)=>Indian [1] string(12)=>American[2]string(17)=>Japanese
I have tried array_values but output Null
I tried this but creates duplicate array in loop for multiple orders Please help me on it . Thanks
$Nationality[] = $value;
One solution is below code:
$nationality = array(
'Nationality_1' => 'Indian',
'Nationality_2' => 'American',
'Nationality_3' => 'Japanese'
);
$temp = array();
foreach ($nationality as $val) {
$temp[] = $val;
}
$nationality = $temp;
print_r($nationality);
unset($temp);
// Array ( [0] => Indian [1] => American [2] => Japanese )
It sounds like you may want to use array_values to pull all the nationalities from your input list.
array_values
Return all the values of an array
http://php.net/array_values
What you do with that is then up to you but from your desired output it seems you want them under another array with a "Nationality" key?
Here is an example...
$input = array(
'Nationality_1' => 'Indian',
'Nationality_5' => 'American',
'Nationality_12' => 'Japanese'
);
$output = array('Nationality' => array_values($input));
var_dump($output);
/*
array(1) {
["Nationality"]=>
array(3) {
[0]=>
string(6) "Indian"
[1]=>
string(8) "American"
[2]=>
string(8) "Japanese"
}
}
*/
See the code in action: https://eval.in/869938
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 2 years ago.
I'm looking to find a way to merge all child arrays into one large array.
array (
[0] =
[0] = '0ARRAY',
[1] = '1ARRAY'
[1] =
[0] = '2ARRAY',
[1] = '3ARRAY'
)
into
array (
[0] = '0ARRAY', [1] = '1ARRAY', [2] = '2ARRAY', [3] = '3ARRAY'
)
Without using array_merge($array[0],$array[1]) because I don't know how many arrays there actually are. So I wouldn't be able to specify them.
Thanks
If I understood your question:
php 5.6+
$array = array(
array('first', 'second'),
array('next', 'more')
);
$newArray = array_merge(...$array);
Outputs:
array(4) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(4) "next" [3]=> string(4) "more" }
Example: http://3v4l.org/KA5J1#v560
php < 5.6
$newArray = call_user_func_array('array_merge', $array);
If it's only two levels of array, you can use
$result = call_user_func_array('array_merge', $array);
which should work as long as $array isn't completely empty
$new_array = array();
foreach($main_array as $ma){
if(!empty($ma)){
foreach($ma as $a){
array_push($new_array, $a);
}
}
}
You can try it by placing these values:
$main_array[0][0] = '1';
$main_array[0][1] = '2';
$main_array[1][0] = '3';
$main_array[1][1] = '4';
OUTPUT:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
This is the representation of my array from the view of var_dump and print_r :
I need to use array_unique but it doesn't work for this array so i was thinking that if i could flatten it to all fit one array then the unique will work.
var_dump :
{
["Source"]=>
string(12) "10.96.250.49"
}
array(1) {
["Source"]=>
string(12) "10.96.250.49"
}
array(1) {
["Source"]=>
string(12) "10.96.250.49"
}
print_r :
Array
(
[Source] => 10.96.250.49
)
Array
(
[Source] => 10.96.250.49
)
Array
(
[Source] => 10.96.250.49
)
If you're using PHP 5.5+, you can use array_column() to extract all the Source values:
$result = array_unique(array_column($array, 'Source'));
If you're using an older PHP version, simply loop through your array and create a flattened array, like so:
$new = array();
foreach ($array as $subarr) {
$new[] = $subarr['Source'];
}
$result = array_unique($new);
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
How can I sort arrays and data in PHP?
(14 answers)
Closed 9 years ago.
My array:
array(n) {
[...],
[100]=>
array(4) {
[0]=>
array(15) {
["TIME_PAIR"]=>
string(11) "16:35"
}
[1]=>
array(15) {
["TIME_PAIR"]=>
string(11) "11:25"
}
[2]=>
array(15) {
["TIME_PAIR"]=>
string(11) "13:25"
}
[3]=>
array(15) {
["TIME_PAIR"]=>
string(11) "15:00"
}
}
[...]
}
I need to sort arrays in array(n)[100] by "TIME_PAIR" value. Som array[100][0] should be at the end. How to do such sort? Ive tried to use array_multisort but it always doesn't work for me(
Hi you can use usort PHP function to sort your array below is the complete Code.
$arr = array(
"100"=>array(
array("TIME_PAIR"=>"16:35"),
array("TIME_PAIR"=>"11:25"),
array("TIME_PAIR"=>"13:25"),
array("TIME_PAIR"=>"15:00"),
)
);
echo "<pre>";
print_r($arr);
echo "</pre>";
function cmp($a, $b) {
return $a['TIME_PAIR'] - $b['TIME_PAIR'];
}
$res = usort($arr[100],"cmp");
echo "<pre>";
print_r($arr);
echo "</pre>";
You will have the result below..
Array
(
[100] => Array
(
[0] => Array
(
[TIME_PAIR] => 11:25
)
[1] => Array
(
[TIME_PAIR] => 13:25
)
[2] => Array
(
[TIME_PAIR] => 15:00
)
[3] => Array
(
[TIME_PAIR] => 16:35
)
)
)
Feel free to ask any questions... :)
I have an array that looks like this:
array(3) {
[0]=>
array(2) {
[0]=>
string(10) "2012-11-14"
[1]=>
string(5) "3238"
}
[1]=>
array(2) {
[0]=>
string(10) "2012-11-13"
[1]=>
string(5) "3231"
}
[2]=>
array(2) {
[0]=>
string(10) "2012-11-13"
[1]=>
string(5) "3231"
}
I would like to write a foreach loop that would turn this array into:
array(2) {
[0]=>
array(1) {
"2012-11-14" => "3238"
}
[1]=>
array(1) {
"2012-11-13" => "3231"
}
So, basically, I would like to use the array element formatted as Y-M-D date as key to the second element in the array.
Given the following array...
$array = array(
0 => array(0 => "2012-11-14", 1 => "3238"),
1 => array(0 => "2012-11-13", 1 => "3231"),
2 => array(0 => "2012-11-13", 1 => "3231"),
);
putting it into a new array like this:
$new_array = array();
foreach ($array as $key => $item)
{
$new_array[$key][$item[0]] = $item[1];
}
print_r($new_array);
produces this output:
Array
(
[0] => Array
(
[2012-11-14] => 3238
)
[1] => Array
(
[2012-11-13] => 3231
)
[2] => Array
(
[2012-11-13] => 3231
)
)
My answer doesn't get rid of the duplicates, but the added dimension as specified in the original question means that duplicate dates as keys aren't an issue.
<?php
$data = array(
array("2012-11-14", "3238"),
array("2012-11-13", "3231"),
array("2012-11-13", "3231") // warning! when there are two record with same date, the second's count will be display
);
$result = array();
foreach ($data as $value) {
$result[$value[0]] = $value[1];
}
echo '<pre>';
print_r($result);
<?php
$newArray = array();
for($i=0;$i<count($arrayVariable);$i++)
{
$newArray[$arrayVariable[$i][0]] = $arrayVariable[$i][1];
}
echo '<pre>';print_r($newArray);echo '</pre>';
?>
Didn't test it but something like this should work in concept. Of course change arrayVariable to your variable.. but that aside.
You can use this code to get what you want:
$dates = array(
array("2012-11-01", "3238"),
array("2012-11-03", "4321")
);
print_r($dates);
$result = array();
foreach($dates as $value) {
$result[][$value[0]] = $value[1];
}
print_r($result);
The output will look like the requested form:
Array
(
[0] => Array
(
[2012-11-01] => 3238
)
[1] => Array
(
[2012-11-03] => 4321
)
)
Codepad demo: http://codepad.org/XAmUEdYh
However, I would personally prefer Aykut's solution. You would of course have a problem when you've got two records with the same date, but the overall array layout is a bit nicer ;).
Here is what I came up with:
<?php
$original = array(
array(
"2012-11-14",
"3238"
),
array(
"2012-11-13",
"3231"
),
array(
"2012-11-13",
"3231"
)
);
$newArray = array();
foreach($original as $subArray){
$newArray[] = array($subArray[0] => $subArray[1]);
}
var_dump($newArray);