How to extract unique values from this array.
I've tried another suggestion...
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
However because of the unix timestamp it wont work.
Im looking to extract only the second array index unique value and its array so should be left with..
// expected final
array(
2 => array(...),
3 => array(..)
)
$arr = array (
0 =>
array (
2 =>
array (
'date' => 1438173658,
'user' => 'admin',
),
),
1 =>
array (
2 =>
array (
'date' => 1438007944,
'user' => 'admin',
),
),
2 =>
array (
3 =>
array (
'date' => 1437746969,
'user' => 'supes',
),
)
)
Thanks.
Might be a simpler way, but here is one:
$result = array_intersect_key($arr,
array_unique(array_map(function($v) {
return current($v)['user'];
},
$arr)));
Related
I have this multidimensional PHP array:
array (
0 =>
array (
'name_lower' => 'apples',
'name' => 'Apples',
),
1 =>
array (
'name_lower' => 'pears',
'name' => 'Pears',
),
2 =>
array (
'name_lower' => 'avocados',
'name' => 'Avocados',
),
3 =>
array (
'name_lower' => 'bananas',
'name' => 'Bananas',
),
)
What I'm trying to manually reorder the arrays inside the multidimensional array and list them in exact the following order:
array (
0 =>
array (
'name_lower' => 'bananas',
'name' => 'Bananas',
),
1 =>
array (
'name_lower' => 'avocados',
'name' => 'Avocados',
),
2 =>
array (
'name_lower' => 'pears',
'name' => 'Pears',
),
3 =>
array (
'name_lower' => 'apples',
'name' => 'Apples',
),
)
It does not follow a pattern to automatically sort the arrays. It needs to be rearranged manually by name. Any ideas?
If you index the array on something unique and set an array with the sort order with those unique values, then you can map the sort order array and extract from the main array:
$sort = array('bananas', 'avocados', 'pears', 'apples');
$array = array_column($array, null, 'name_lower');
$array = array_map(function($v) use($array) { return $array[$v]; }, $sort);
I have an Array as seen below.
Array
(
[0] =>
[key-1] => Array
(
[key-1-1] => 33
[key-1-2] => 22
)
[key-2] => -1
[key-3] => Array
(
[data] => Array
(
[other_data] => Array
(
[0] => data1
[1] => data2
)
)
)
[key-4] => data3
[key-5] => data4
)
I need to convert these in simpler form of end values as given below and save to an external php file using file_put_contents . I am trying for hours and I tried var_export, multiple foreach and got some degree of success, but not exactly what I want.
$value['key-1-1'] = '33';
$value['key-1-2'] = '22';
$value['other_data'] = array('data1', 'data2');
$value['key-4'] = 'data3';
$value['key-5'] = 'data4';
Can someone help with achieving it ?
You can get somewhat near to your desired output with array_walk_recursive. The troublesome keys are the numeric ones.
If you only have one other_data sub arrays, you could slightly adjust the output below. You'll get overwritten values for like numeric keys. So this method really depends on your data.
<?php
$data =
array
(
'0' =>'',
'key-1' => array
(
'key-1-1' => 33,
'key-1-2' => 22
),
'key-2' => -1,
'key-3' => array
(
'data' => array
(
'other_data' => array
(
'0' => 'data1',
'1' => 'data2'
)
)
),
'key-4' => 'data3',
'key-5' => 'data4'
);
array_walk_recursive($data, function($v, $k) use (&$result) {
$result[$k] = $v;
});
var_export($result);
Output:
array (
0 => 'data1',
'key-1-1' => 33,
'key-1-2' => 22,
'key-2' => -1,
1 => 'data2',
'key-4' => 'data3',
'key-5' => 'data4',
)
I have several arrays that I want to check for a repeated value and if the value is found to be repeated in one of the other arrays, then combine both of those arrays together.
I give an example below of 2 arrays that have repeated values.
example: the value in purchase_order_number is the same in both arrays below. They are not unique values. But the values in tracking_number are unique.
I want to check if the value in purchase_order_number is repeated in another array. If the same value is found in another array, then combine both of those arrays into 1 array.
I'm trying to get the value in tracking_number and service combined into a single array when the value in purchase_order_number is the same in 2 or more arrays.
Example arrays below.
not combined
array (
'data' =>
array (
15 =>
array (
'type' => 'Tracking',
'id' => 2830143,
'attributes' =>
array (
'tracking_number' => '1Z5270560360309870',
'service' => 'UPS',
'order_id' => 2606218,
'purchase_order_number' => '7249491',
'recipient_attempts' => 1,
),
),
16 =>
array (
'type' => 'Tracking',
'id' => 2830144,
'attributes' =>
array (
'tracking_number' => '1Z5270560361740866',
'service' => 'UPS',
'order_id' => 2606218,
'purchase_order_number' => '7249491',
'recipient_attempts' => 1,
),
),
),
)
Example given below of how I need to combine the two above arrays.
Combined
array (
'data' =>
array (
16 =>
array (
'type' => 'Tracking',
'id' => 2830144,
'attributes' =>
array (
'tracking' =>
array (
0 =>
array (
'tracking_number' => '1Z5270560360309870',
'service' => 'UPS',
),
1 =>
array (
'tracking_number' => '1Z5270560361740866',
'service' => 'UPS',
),
),
'order_id' => 2606218,
'purchase_order_number' => '7249491',
'recipient_attempts' => 1,
),
),
),
)
Here's a reduce based solution with caveats:
$arr['data'] = array_reduce($arr['data'], function ($out, $item) {
// keys we want to extract
static $tracking_keys = ['tracking_number' => '', 'service' => ''];
// yank tracking keys from attributes
$tracking = array_intersect_key($item['attributes'], $tracking_keys);
$item['attributes'] = array_diff_key($item['attributes'], $tracking_keys);
// insert to new array based on order number
$order_no = $item['attributes']['purchase_order_number'];
if (!isset($out[$order_no])) {
$item['attributes']['tracking'] = [$tracking];
$out[$order_no] = $item;
} else {
array_push($out[$order_no]['attributes']['tracking'], $tracking);
}
return $out;
}, []);
Keys in 'data' are not retained and 'id' is set by the first item.
I am having this array
array (
0 => array ( 'sno' => 'q3', 'result' => '15', ),
1 => array ( 'sno' => 'q1', 'result' => '5', ),
2 => array ( 'sno' => 'q2', 'result' => '10', ),
)
i want this resulting array
array (
'q3' => '15',
'q1' => '5',
'q2' =>'10'
)
if possible without using any loop?
if Yes Then How?
Here is your input array,
$arr= array (
0 => array ( 'sno' => 'q3', 'result' => '15', ),
1 => array ( 'sno' => 'q1', 'result' => '5', ),
2 => array ( 'sno' => 'q2', 'result' => '10', ),
);
Here is one liner code :
$result = array_combine(array_column($arr, 'sno'), array_column($arr, 'result'));
// $result = array_column($arr,'result','sno');
What I am doing is
array_column will fetch all values in array related to specific key, so I fetched two arrays values to specific keys
Then I used array_combine to create an array by using one array for keys and another for its values.
Here is output:
Array
(
[q3] => 15
[q1] => 5
[q2] => 10
)
Here is working code
click here
Using array_reduce() you can create new array contain custom key/value from an array.
$newArr = array_reduce($oldArr, function($carry, $item){
$carry[$item["sno"]] = $item["result"];
return $carry;
});
Check code result in demo
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
php multi-dimensional array remove duplicate
I have an array like this:
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
)
How can I remove the duplicate values so that I get this:
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
4 => array ( 'value' => 'Canada', ),
)
I tried using array_unique, but that doesn't work due to this array being multidimensional, I think.
Edit: I also need this array to be multi-dimensional and in this format, I can't flatten it.
array_unique is using string conversion before comparing the values to find the unique values:
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.
But an array will always convert to Array:
var_dump("Array" === (string) array());
You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique:
$unique = array_unique($a, SORT_REGULAR);
Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique to find the unique values:
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
Here :)
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach ($a as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
print_r ($tmp);
?>
Use SORT_REGULAR flag.
$unique_array = array_unique($a, SORT_REGULAR);
I'm not sure why it helps but it does. At least with php 5.3