How to remove duplicate values compare two array - php

I have two array.
one
Array
(
[0] => Array
(
[driverId] => 3
[latitude] => 23.752182
[longitude] => 90.377730
[distance] => 0
[EstTime] => 0
)
[1] => Array
(
[driverId] => 6
[latitude] => 23.752782
[longitude] => 90.375730
[distance] => 0.2341134331552646
[EstTime] => 133
)
)
two
Array
(
[0] => Array
(
[driverId] => 3
)
[1] => Array
(
[driverId] => 61
)
)
first array store in $info and second array store in $infor
here first array item driverId is 3 and second array item driverId is 3.
so in my output i want to skip first array first item.

When looping through each array store the driverId in another array and also check that the current driverId is not in this array, if it is then we can skip it. For example:
$ids = array();
foreach($infor AS $arr2){
$ids[] = $arr2['driverId'];
}
foreach($info AS $i){
if(!in_array($i['driverId'],$ids)){
print_r($i);
}
}

Related

how to add array value at specfic index in php codenighter

I have array value like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[2] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
Now i want to add another array value in specific index .lets say i wants to add this array value at index 1
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
Now the result output should be like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[3] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
this is my foreach loop to serlize channel and id
foreach ($channel as $key => $ch) {
$user_hash['channel'] = json_encode($ch);
$user_hash['id'] = random_string('alnum', 30);
array_push($user_hash_array, $user_hash);
}
You need to split the array into 2, then insert your new value at the end of the first sub-array, then merge it with the second sub-array. EG: an array which looks like [1,3,4,5] and you want to insert "2" at position 2, then you split at position one to have [1] and [3,4,5]; then you append "2" at the end of first sub-array to form [1,2], then merge this new subarray with the other sub-array([3,4,5]) to form [1,2] + [3,4,5].
For your implementation, try this code:
$array = array() // the original array you want to modify
$insert = array() // the array you want to push into the original one above
$position = 1 // the position at which you want to insert the new item
$newArray = array_slice($array, 0, $position, TRUE) + $insert + array_slice($array, $position, NULL, TRUE);
you can use array_splice array method for add element in array at particular position
<?php
$original_array = array(
array("channel"=>15,"id"=>"sdfdfsf1"),
array("channel"=>16,"id"=>"sdfdfsf2"),
array("channel"=>17,"id"=>"sdfdfsf3")
);
echo "<pre>";print_r($original_array);
$inserted_element =
array(array("channel"=>20,"id"=>"xxxxxxxxxxewqeqwexxxxxxxewrewrw"));
$position=1;
array_splice( $original_array, $position, 0, $inserted_element );
echo "<pre>";print_r($original_array);
?>
Output will be as following
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[2] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[3] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)

merge two array's key value of same index

i have 2 arrays, i have to merge array within same index.
Array
(
[0] => Array
(
[vendor_name] => MIRAGE PET PRODUCTS
[count_es] => 86
[outofstalk] => 19
[listing] => 64
[pricing] => 2
[discontinued] => 1
[others] => 0
)
[1] => Array
(
[vendor_name] => THE HOME DEPOT
[count_es] => 1
[outofstalk] => 0
[listing] => 1
[pricing] => 0
[discontinued] => 0
[others] => 0
)
)
this is first array and
Array
( [0] => Array
(
[incorrect_esc_count] => 1
)
[1] => Array
(
[incorrect_esc_count] => 0
)
)
this is second array .
I want that result
Array
(
[0] => Array
(
[vendor_name] => MIRAGE PET PRODUCTS
[count_es] => 86
[outofstalk] => 19
[listing] => 64
[pricing] => 2
[discontinued] => 1
[others] => 0
[incorrect_esc_count] => 1
)
[1] => Array
(
[vendor_name] => THE HOME DEPOT
[count_es] => 1
[outofstalk] => 0
[listing] => 1
[pricing] => 0
[discontinued] => 0
[others] => 0
[incorrect_esc_count] => 0
)
)
How can I achieve that? `The first array is getting from one variable and second array is getting from another variable. I want this sholud be in one array because for view i use angular.js and using for table
You are trying to merge two multidimensional associative arrays into one. This code should work for you, with some comments:
<?php
// declare the first array, I did not add all of the key-value pairs
$marr1 = array
(
array("vendor_name"=>"MIRAGE PET PRODUCTS","count_es"=>86,"outofstalk"=>19),
array("vendor_name"=>"THE HOME DEPOT","count_es"=>1,"outofstalk"=>0)
);
// declare second array
$marr2 = array
(
array("incorrect_esc_count"=>1),
array("incorrect_esc_count"=>0)
);
// declare third empty array to hold the result
$marr3 = array();
// loop through both arrays
// this will not work if your arrays have different lengths
for ($i = 0; $i < count($marr1); $i++) {
// merge the ith elements of both array and then push that array into the third array
array_push($marr3, array_merge($marr1[$i],$marr2[$i]));
}
// print out result
print_r($marr3);
?>

how to get the nested array count with key values seperated in php

This is my array
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each
Here,I need array count as follows
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
How should I get this..
Someone help me out of this...
Thank you..
It is very easy foreach your array and use count or sizeof function.
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
The output will be as your desired output
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
It's simple, and is better to create new array where you can save count of elements of main array items:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
Also you don't need to have extra array for the $counts array, what you get is:
array (
2 => 2,
1 => 1
)

Merge a 3D Array with 2D Array Based on Shared Values

I need to merge a three-dimensional array with a two-dimensional array based on a shared value for 'id.'
In the example below, "George Washington" has an 'id' of 1. I need to append his 'value', which is found in the second array where 'id' is also 1.
The 'id' of "John Adams" is 2, which is not found in the second array. As a result, his 'value' needs to be set to 0 (or NULL).
The final result is a three-dimension array where 'value' has been added to each item in the original array.
Array #1
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
)
)
Array #2
Array
(
[0] => Array
(
[id] => 1
[value] => 12
)
[1] => Array
(
[id] => 5
[value] => 18
)
)
Desired Result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
[value] => 12
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
[value] => 0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
[value] => 18
)
)
What I've tried so far:
I separated all of the 'id' values from the first array into a new array named $ids. Then while looping through the items in the second array, I check to see whether the 'id' from the second array is found in the $ids array.
But then I'm stuck because I don't know how to specify which item in the first array needs to receive the new 'value'. Also, this seems like a messy solution because of the overhead involved in creating the $ids array.
Didn't even realize you had tried to list the IDs and such before I was done writing this, but here you go anyway. Good luck!
$array1_size = count($array1);
$array2_size = count($array2);
// Creates a list containing all available IDs of Array1
for ($i = 0; $i < $array1_size; $i ++) {
$id_list[] = $array1[$i][0]['id'];
$array1[$i]['value'] = NULL; // Adds a NULL to all value fields
}
// Loops through Array2
for ($i = 0; $i < $array2_size; $i++) {
if (in_array($array2[$i]['id'], $id_list)) { // Checks if each ID exists in the ID list
$key = array_search($array2[$i]['id'], $id_list); // Gets the key of the matching ID
$array1[$key]['value'] = $array2[$i]['value']; // Adds the value
}
}
Edit: Values are now set to NULL by default and thus only gets changed later on if needed.

php unique/group associative array

I would like to remove duplicates for the following array. I want to group by the first value and then rebuild the index.
ie
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[1] => Array
(
[title] => California
[state_id] => 1
)
[2] => Array
(
[title] => New Mexico
[state_id] => 2
)
[3] => Array
(
[title] => Washington
[state_id] => 3
)
[4] => Array
(
[title] => Montana
[state_id] => 4
)
[5] => Array
(
[title] => Montana
[state_id] => 4
)
)
To
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[2] => Array
(
[title] => New Mexico
[state_id] => 2
)
[3] => Array
(
[title] => Washington
[state_id] => 3
)
[4] => Array
(
[title] => Montana
[state_id] => 4
)
)
and rebuild key
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[1] => Array
(
[title] => New Mexico
[state_id] => 2
)
[2] => Array
(
[title] => Washington
[state_id] => 3
)
[3] => Array
(
[title] => Montana
[state_id] => 4
)
)
$array = array_values(array_combine(array_map(function ($i) { return $i['title']; }, $array), $array));
I.e.
extract the title key (array_map)
use the extracted titles as keys for a new array and combine it with the old one (array_combine), which de-duplicates the array (keys have to be unique)
run the result through array_values, which discards the keys
Broken down:
$keys = array_map(function ($i) { return $i['title']; }, $array);
$deduped = array_combine($keys, $array);
$result = array_values($deduped);
For PHP 5.2- you'll need to write the anonymous callback function like this:
array_map(create_function('$i', 'return $i["title"];'), $array)
Sort your array by title (usort does the trick)
create a new empty array
Cycle original array and store the value you find in the new empty array
If next array value equals last then skip it
At the end of this you have the array you want.
There is another way:
Sort your array by title
Cycle original array
If this array value equals last then unset it
Sort the array again with PHP sort function to rebuild keys
$array = array_unique($array);
Yes, it works with multi-dimentional arrays too; it just checks to equality.

Categories