i have a 2-dimensionl array such as this:
Array
(
[0] => Array
(
[0] => talk
[1] => amount
)
[1] => Array
(
[0] => base
[1] => amazing
)
[2] => Array
(
[0] => talk
[1] => filter
)
[3] => Array
(
[0] => label
[1] => any
)
[4] => Array
(
[0] => talk
[1] => amount
)
[5] => Array
(
[0] => tour
[1] => any
)
)
how remove duplicate value by first dimension and result such as this:
Array
(
[0] => Array
(
[0] => talk
[1] => amount
)
[1] => Array
(
[0] => base
[1] => amazing
)
[2] => Array
(
[0] => label
[1] => any
)
[3] => Array
(
[0] => tour
[1] => any
)
)
there is much help about remove duplicate values in the array, but I didn't see any sample about remove duplicates by specific dimension.
<?php
function uniqueArray($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = uniqueArray($value);
}
}
return $result;
}
?>
This will helps you
Note: not tested
Related
I apologize for not being word-perfect in English.
I have this result from a foreach loop in php.
my file is jason.
Merging more value into one array
Array
(
[777565] => Array
(
[0] => Array
(
[0] => 777565-1
[1] => 777565-2
)
[1] => Array
(
[0] => 777565-3
[1] => 777565-4
)
[2] => Array
(
[0] => 777565-5
[1] => 777565-6
)
)
[777566] => Array
(
[0] => Array
(
[0] => 777566-1
[1] => 777566-2
)
[1] => Array
(
[0] => 777566-3
[1] => 777566-4
)
[2] => Array
(
[0] => 777566-5
[1] => 777566-6
)
)
)
but, I want Something like this:
Array
(
[777565] => Array
(
[0] => 777565-1
[1] => 777565-2
[2] => 777565-3
[3] => 777565-4
[4] => 777565-5
[5] => 777565-6
)
[777566] => Array
(
[0] => 777566-1
[1] => 777566-2
[2] => 777566-3
[3] => 777566-4
[4] => 777566-5
[5] => 777566-6
)
)
I tried hard and searched the internet but I could not find any way.
Of course, I have the ability to move it to the database first and then to the array, but I think there should be a faster way. What do you think?
thanks for reply.
If you have no problem looping through it and flatten the array according to your desire then you can try this:
$parent =
Array
(
[777565] => Array
(
[0] => Array
(
[0] => 777565-1
[1] => 777565-2
)
[1] => Array
(
[0] => 777565-3
[1] => 777565-4
)
[2] => Array
(
[0] => 777565-5
[1] => 777565-6
)
)
[777566] => Array
(
[0] => Array
(
[0] => 777566-1
[1] => 777566-2
)
[1] => Array
(
[0] => 777566-3
[1] => 777566-4
)
[2] => Array
(
[0] => 777566-5
[1] => 777566-6
)
)
);
$length = count($parent);
$result=[];
for($i=0; $i<$length; $i++){
for($j=0; $j<3; $j++){
$l=0;
for($k=0; $k<2; $k++){
$result[777565+$i][$j][$l++] = $parent[777565+$i][$j][$k];
}
}
}
I have the following multidimensional array:
Array
(
[0] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1559881644000,500
)
)
[1] => Array
(
[name] => FPT
[data] => Array
(
[0] => 1559994465000,172
)
)
[2] => Array
(
[name] => MobiFone
[data] => Array
(
[0] => 1559997314000,11164
)
)
[3] => Array
(
[name] => Vietnamobile
[data] => Array
(
[0] => 1559993699000,1246
)
)
[4] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1560000096000,47886
)
)
[5] => Array
(
[name] => VinaPhone
[data] => Array
(
[0] => 1559997553000,11132
)
)
[6] => Array
(
[name] => VNPT
[data] => Array
(
[0] => 1559993066000,84
)
)
)
I'm currently using a foreach loop to extract the values from the array:
$result = [];
$nameData = [];
foreach($data as $key => $itemData)
{
}
I want array look like this:
Array
(
[0] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1559881644000,500
[1] => 1560000096000,47886
)
)
[1] => Array
(
[name] => FPT
[data] => Array
(
[0] => 1559994465000,172
)
)
[2] => Array
(
[name] => MobiFone
[data] => Array
(
[0] => 1559997314000,11164
)
)
[3] => Array
(
[name] => Vietnamobile
[data] => Array
(
[0] => 1559993699000,1246
)
)
[4] => Array
(
[name] => VinaPhone
[data] => Array
(
[0] => 1559997553000,11132
)
)
[5] => Array
(
[name] => VNPT
[data] => Array
(
[0] => 1559993066000,84
)
)
)
It seems you are looking for groupby the array by name.
You can use foreach with array_key_exists and array_push
$groupBy = [];
foreach($a as $v){
array_key_exists($v['name'], $groupBy) ?
array_push($groupBy[$v['name']]['data'], $v['data'][0])
:
($groupBy[$v['name']] = $v)
;
}
If you want to rearrange the keys of array, you can use array_values
print_r(array_values($groupBy));
Working DEMO :- https://3v4l.org/ASWDV
You can just loop over the input array, adding values to the output if they don't exist and merging that data values if they do:
$result = array();
foreach ($data as $itemData) {
if (($k = array_search($itemData['name'], array_column($result, 'name'))) !== false) {
$result[$k]['data'] = array_merge($result[$k]['data'], $itemData['data']);
}
else $result[] = $itemData;
}
print_r($result);
Output is as you desire (too long to reproduce here)
Demo on 3v4l.org
If you are adding a single value to the array you can just access to it with something like this:
$result[0]['data'][1] = 1560000096000,47886
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
I have an associative array as such:
Array
(
[0] => Array
(
[0] => Array
(
[0] => name=>Jose
[1] => email=>jo#example.com
)
[1] => Array
(
[0] => name=>Adriana
[1] => email=>add#example.com
)
)
[1] => Array
(
[0] => Array
(
[0] => name=>Jose
[1] => email=>juniper#example.com
)
[1] => Array
(
[0] => name=>Maria
[1] => email=>maria#example.com
)
)
)
I want to collect all email addresses of all members. Jose has 2 email addresses, Maria 1, and Adriana 1. How do I compare 2 arrays that co-exist in a multidimensional array?
I want something like:
Array
(
[0] => Array
(
[0] => name=>Jose
[1] => firstemail=>jo#example.com
[2] => secondemail=>juniper#example.com
)
[1] => Array
(
[0] => name=>Adriana
[1] => firstemail=>add#example.com
)
[2] => Array
(
[0] => name=>Maria
[1] => firstemail=>maria#example.com
)
)
thanks in advance!
Like this
$finalarray = array();
foreach ($inputarray as $subarray) {
foreach($subarray as $entry) {
if (!array_key_exists($entry["name"], $finalarray)) {
$finalarray[$entry["name"]] = array();
}
array_push($finalarray[$entry["name"]], $entry["email"]);
}
}
There has been a few other questions regarding replacing values in multidimensional array on here, but I didn't find anything regarding what I was trying to do, exactly, per se.
I have an array that I get from an API and I need to update a few values based on other values in the array tree before sending the API payload to the browser.
In the array when the sale[0] === true I am looking for some logic to then replace the [price][0] value with a corresponding new sale price.
Using the foreach, I can easily loop through the each of the nodes in the array, but I am unsure once I loop through the array, how I can update the original array with new price if/when sale node === true.
Array
(
[response] => Array
(
[0] => Array
(
[results] => Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[id] => Array
(
[0] => 846471605959
)
[title] => Array
(
[0] => Test Item 846471605959
)
[imageURL] => Array
(
[0] => https://foo/bar/images/846471605959.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/846471605959
)
[price] => Array
(
[0] => 799.00
)
[sale] => Array
(
[0] => true
)
)
[1] => Array
(
[id] => Array
(
[0] => 414953260545
)
[title] => Array
(
[0] => Test Item 414953260545
)
[imageURL] => Array
(
[0] => https://foo/bar/images/414953260545.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/414953260545
)
[price] => Array
(
[0] => 199.00
)
[sale] => Array
(
[0] => false
)
)
[2] => Array
(
[id] => Array
(
[0] => 684865199812
)
[title] => Array
(
[0] => Test Item 684865199812
)
[imageURL] => Array
(
[0] => https://foo/bar/images/684865199812.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/684865199812
)
[price] => Array
(
[0] => 699.00
)
[sale] => Array
(
[0] => false
)
)
[3] => Array
(
[id] => Array
(
[0] => 987800965761
)
[title] => Array
(
[0] => Test Item 987800965761
)
[imageURL] => Array
(
[0] => https://foo/bar/images/987800965761.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/987800965761
)
[price] => Array
(
[0] => 499.00
)
[sale] => Array
(
[0] => true
)
)
[4] => Array
(
[id] => Array
(
[0] => 005457536677
)
[title] => Array
(
[0] => Test Item 005457536677
)
[imageURL] => Array
(
[0] => https://foo/bar/images/005457536677.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/005457536677
)
[price] => Array
(
[0] => 99.00
)
[sale] => Array
(
[0] => false
)
)
)
)
)
}
)
)
Use a reference variable for the foreach iteration variable, then you can update the element in place.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
if ($item['sale'][0]) {
$item['price'][0] = $new_price;
}
}
If you also need to loop through all the elements in the sale array, add a nested loop.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
foreach ($item['sale'] as $i => $sale) {
if ($sale) {
$item['price'][$i] = $new_price;
}
}
}