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);
Related
I have the following (nested) array:
array(3) { [16]=> array(3) { [0]=> int(159) [1]=> int(160) [2]=> int(158) }
[21]=> array(2) { [0]=> int(160) [1]=> int(158) }
[19]=> array(2) { [0]=> int(158) [1]=> int(159) } }
As you can see it contains 3 child array's. The child array's all contain the integer '158' as an value but also '159'. I want to somehow loop trough the child array's and do a check if all child array's contain that value. Then I want to return an array with only these values.
I know i could use array_intersect for this however the nested array's are generated dynamically so i'm not sure how to deal with this using array intersect:
<?php
$arr1 = array('158','250','342');
$arr2 = array('158','142','352');
$diff1 = array_intersect($arr1, $arr2);
print_r( $diff1 );
//RETURNS Array ( [0] => 158 )
?>
You can use the splat operator(...) to pass all the subarrays into array_intersect() in one go...
$arr1 = [['158','250','342'],['158','142','352'],['1421','158','3521']];
$diff1 = array_intersect(...$arr1);
print_r( $diff1 );
//RETURNS Array ( [0] => 158 )
After a bit searching around I found the following:
$result = call_user_func_array('array_intersect', $productStoreArray);
As seen on: php dynamic array_intersect
This solves my problem because it returns to me:
//RETURNS Array ( [0] => 158, 1 => 159 )
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
In the main array, I have:
$array=array('index1'=>'value1', 'index2'=>'value2' , ....);
In another array, I keep the key=>index relationship:
$map=array('key1'=>'index1', 'key2'=>'index2' , ...);
Is there any simple way to combile these two array into one to get:
$combine=array('key1'=>'value1', 'key2'=>'value2', ...);
that is values from main array and keys comes from the map array.
<?php
$array1=array('index1'=>'value1', 'index2'=>'value2');
$array2=array('key1'=>'index1', 'key2'=>'index2');
foreach($array2 as $key => $val){
if(array_key_exists($val,$array1))
$combined[$key] = $array1[$val];
}
// Output
print_r($combined);
?>
Output
Array
(
[key1] => value1
[key2] => value2
)
Loop through the $map array and extract the value for $array accordingly and put them to the $combine array. You can try this -
$array=array('index1'=>'value1', 'index2'=>'value2');
$map=array('key1'=>'index1', 'key2'=>'index2');
$combine = array();
foreach($map as $key => $val) {
$combine[$key] = $array[$val];
}
var_dump($combine);
Output
array(2) {
["key1"]=>
string(6) "value1"
["key2"]=>
string(6) "value2"
}
A possible solution here, if the keys and values already are in the expected order, and you are certain all arrays have the same length, use the array_combine function together with array_keys and array_values
<?php
$array=array('index1'=>'value1', 'index2'=>'value2');
$map=array('key1'=>'index1', 'key2'=>'index2');
$combine = array_combine( array_keys($map), array_values($array) );
print_r($combine);
?>
Output
Array
(
[key1] => value1
[key2] => value2
)
Variant with no own code, only functions. It takes only keys presented in both arrays:
$array=array('index1'=>'value1', 'index2'=>'value2' , 'index3'=>'value3');
$map=array('key1'=>'index1', 'key2'=>'index2');
// next two lines if arrays may be of different length
$newmap = array_intersect_key(array_flip($map), $array);
$newarray = array_intersect_key($array, $newmap);
$new = array_combine($newmap, $newarray);
var_dump ($new);
output:
array(2) { ["key1"]=> string(6) "value1" ["key2"]=> string(6) "value2" }
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 ) )
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);