I want to remove duplicate data in my array.
That's my array :
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
My multidimensionnal array :
Array
(
[0] => Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
)
[1] => Array
(
[0] => fraise
[1] => pomme
[2] => framboise
)
[2] => Array
(
[0] => ananas
[1] => citron
[2] => raisin
)
[3] => Array
(
[0] => pêche
[1] => pruneau
[2] => pomme
)
)
and that's my code to try remove duplicate data:
$monTableau = array_map("unserialize", array_unique(array_map("serialize", $monTableau)));
Don't work unfortunately :(
Advance Thanks,
Your code does work properly, but there is no duplicates in the main array. If you want one single array as output that contains only unique elements of the whole list take a look at this code:
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
$merged = call_user_func_array('array_merge', $monTableau);
$unique = array_unique($merged);
Output of $unique:
Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
[3] => fraise
[5] => framboise
[6] => ananas
[7] => citron
[8] => raisin
[10] => pruneau
)
Duplicates of pomme have been removed
Related
I have an array coming as below which is dynamic. Now I want to edit a specific array for "BHD0000000002". I am getting the following array through ajax request. However I cannot edit the inner array element. For example, I am trying to edit the inner array value for the key "BHD0000000002" but unable to fix the issues. Can anyone help sir/madam?
This is the original array:
$budget_detail_array=Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 12212121
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
I want to edit the above array as :
Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 5000
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
So far I have tried with the following code but it is not working. Anyone can help?
foreach($budget_detail_array as $key=>$value){
foreach($value as $keyval=>$val){
if($keyval=='BHD0000000002'){
$val=5000;
}
}
/*if($value[$budget_id]){
$value[$budget_id]=100;
}else{
$value[$budget_id]=700;
}*/
}
print_r($budget_detail_array);exit;
In order to modify an associative array value you can do this:
$budget_detail_array[1]['BHD0000000002'] = 5000;
And then check the result again
print_r($budget_detail_array);
I have the following multi dimensional PHP array. What I'm trying to do is return only the value which occurs in all of the arrays.
So in the array below the only value which occurs in every array is "2018-02-22", so I want to create a new array with only this value.
I feel like it can't be too difficult, but I just can't get my head around how to do this. If any one can help it would be much appreciated!
Array
(
[0] => Array
(
[0] => 2018-02-22
)
[1] => Array
(
[0] => 2018-02-22
[1] => 2018-02-21
[2] => 2018-02-20
[3] => 2018-02-16
[4] => 2018-02-14
)
[2] => Array
(
[0] => 2018-02-20
[1] => 2018-02-19
[2] => 2018-02-21
[3] => 2018-02-22
[4] => 2018-02-14
)
[3] => Array
(
[0] => 2018-02-22
[1] => 2018-02-12
[2] => 2018-02-01
)
)
So to clarify the output I'm aiming for is:
Array
(
[0] => 2018-02-22
)
You can do it by array_intersect, to get common value 2018-02-22.
<?php
$dates = [
["2018-02-22","2018-02-23"],
["2018-02-22","2018-02-24"],
["2018-02-22","2018-02-25"],
];
$common = array_shift($dates);
foreach($dates as $key=>$date){
$common = array_intersect($common, $date);
}
print_r($common);
?>
Live Demo
Output is :
Array (
[0] => 2018-02-22
)
I have two+ Arrays which I set in a while loop.
Every array have a dependence to the other array by the index. And every array have duplications on the same index.
What I want to do is, to delete the duplications and after that to delete the empty indexes of each array, so that all arrays have the same positions like before but without duplications.
What I have tried is this:
$array1 = array_unique($array1);
$array2 = array_unique($array2);
print_r($array1);
echo "<br>";
print_r($array2);
echo "<br>";
sort($array1);
sort($array2);
print_r($artNrArray);
echo "<br>";
print_r($pnameArray);
echo "<br>";
This is the output:
Array (
[0] => 0100_64
[9] => 1999_13
[18] => 5999_12
[19] => 0204_22
[21] => 0241_75 )
Array (
[0] => intKab-4xAWG22-S-oE-oE-K3
[9] => Käbel
[18] => Kabel_test123
[19] => K-A21-V-IBIS-13-3-4-0-0-0m
[21] => K-CAN-17-2m )
Array (
[0] => 0100_64
[1] => 0204_22
[2] => 0241_75
[3] => 1999_13
[4] => 5999_12 )
Array (
[0] => K-A21-V-IBIS-13-3-4-0-0-0m
[1] => K-CAN-17-2m
[2] => Kabel_test123
[3] => Käbel
[4] => intKab-4xAWG22-S-oE-oE-K3 )
The problem if I sort the arrays is, that the dependence of the array contet is changed. What I want to have is this:
Array (
[0] => 0100_64
[1] => 1999_13
[2] => 5999_12
[3] => 0204_22
[4] => 0241_75 )
Array (
[0] => intKab-4xAWG22-S-oE-oE-K3
[1] => Käbel
[2] => Kabel_test123
[3] => K-A21-V-IBIS-13-3-4-0-0-0m
[4] => K-CAN-17-2m )
I have to use array_unique() to delete the duplications.
So how can I delete the array indexes with empty value and contraxt the array from 0 - 4?
Try the next:
$array1 = array_values(array_unique($array1));
$array2 = array_values(array_unique($array2));
I have two arrays that was converted from csv file.
The first csv line looks like this:
franchise_id,franchise_name,phone,website,email,region_codes;
1,"Abbott, Hackett and O`Conner",1-648-177-9510,auto-service.co/bw-319-x,Lupe-2485#auto-service.co,"36101,36055,36071";
The second csv line looks like this:
postal_code,region_code,city,state,region;
14410,36055,Adams Basin,NY,Monroe;
I converted these lines to arrays like this:
//Region Array
$region_lines = explode(PHP_EOL, $region_mappings_string);
$region_array = array();
foreach ($region_lines as $region_line) {
$region_array[] = str_getcsv($region_line);
}
//Franchise Array
$franchise_lines = explode(PHP_EOL, $franchises_string);
$franchise_array = array();
foreach ($franchise_lines as $franchise_line) {
$franchise_array[] = str_getcsv($franchise_line);
}
After that I got the result like this for Region:
Array
(
[0] => Array
(
[0] => postal_code
[1] => region_code
[2] => city
[3] => state
[4] => region;
)
[111] => Array
(
[0] => 14410
[1] => 36055
[2] => Adams Basin
[3] => NY
[4] => Monroe;
)
[112] => Array
(
[0] => 14617
[1] => 36055
[2] => Rochester
[3] => NY
[4] => Monroe;
)
And for franchise:
Array
(
[0] => Array
(
[0] => franchise_id
[1] => franchise_name
[2] => phone
[3] => website
[4] => email
[5] => region_codes;
)
[1] => Array
(
[0] => 1
[1] => Abbott, Hackett and O`Conner
[2] => 1-648-177-9510
[3] => auto-service.co/bw-319-x
[4] => Lupe-2485#auto-service.co
[5] => 36101,36055,36071;
)
What I need to do is to look for 14410 postal code from the first array, get the region_code and look for this region_code in second array and then output the results in PHP. How can this be done?
Will have to loop over franchises array and run simple comparison
function getDataByRegionCode($code, $franchiseArray) {
foreach ($franchiseArray as $key => $data) {
if(isset($data[5])){
$codes = explode(',',$data[5]);
if(in_array($code,$codes)) return $data;
}
}
return NULL;
}
print_r(getDataByRegionCode(14410,$franchiseArray));
If I have an array:
Array
(
[0] => Array
(
[0] => |174|September|2001|
[1] => |Pengantar=Hello!!!!
[2] => |Tema= Sami Mawon
[3] => |Tema_isi=meet you!!!
[4] => |Kutip=people
[5] => |Kutip_kitab=Efesus
[6] => |Kutip_pasal=4
[7] => |Kutip_ayat=28
[8] => |Tema_sumber=Kiriman dari Maurits albert (romind# )
[9] => [[Kategori:e-humor 2001]]
)
How can I get the value of Pengantar, Tema, Tema_isi etc?
You're going to have to loop over the array and use preg_match with a reference.
Regex something like this (off the top of my head) would probably work:
/\|(.*?)=(.*?)|?/
Just use preg_match('/\|(.*?)=(.*?)|?/', $subject[$x], $matches); and var_dump($matches); to see the results.
Don't forget that the $matches array passed into the preg_match function is a reference to an array which you should instantiate first and that it will be overwritten in each loop cycle.
just use array walk, a matching reg ex and a lambda function:
$array = array(
array(
'|174|September|2001|',
'Pengantar=Hello!!!!',
'Tema= Sami Mawon ',
'Kategori:e-humor 2001',
[...]
)
);
$values = array();
array_walk($array[0],function(&$item1, $key) use(&$values) {
if(preg_match('#[^=]=(.+)#',$item1,$match)){
$values[] = $match[1];
}
});
print_r($values);
The regular expression can be like this, using the named subpattern of "preg_match()":-
$a = array(
array(
'|174|September|2001|',
'|Pengantar=Hello!!!!',
'|Tema= Sami Mawon',
'|Tema_isi=meet you!!!',
'|Kutip=people',
'|Kutip_kitab=Efesus',
'|Kutip_pasal=4',
'|Kutip_ayat=28',
'|Tema_sumber=Kiriman dari Maurits albert (romind# )',
'[[Kategori:e-humor 2001]]',
)
);
$pattern = '/(?<first>\w+)[:=](?<rest>[\d|\w|\s]+)/';
$matches = array();
foreach ($a as $_arrEach) {
foreach ($_arrEach as $_each) {
$result = preg_match($pattern, $_each, $matches[]);
}
}
echo "<pre>";
print_r($matches);
echo "</pre>";
You will find that the array key "first" satisfies your requirement.
The above will output as:-
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => Pengantar=Hello
[first] => Pengantar
[1] => Pengantar
[rest] => Hello
[2] => Hello
)
[2] => Array
(
[0] => Tema= Sami Mawon
[first] => Tema
[1] => Tema
[rest] => Sami Mawon
[2] => Sami Mawon
)
[3] => Array
(
[0] => Tema_isi=meet you
[first] => Tema_isi
[1] => Tema_isi
[rest] => meet you
[2] => meet you
)
[4] => Array
(
[0] => Kutip=people
[first] => Kutip
[1] => Kutip
[rest] => people
[2] => people
)
[5] => Array
(
[0] => Kutip_kitab=Efesus
[first] => Kutip_kitab
[1] => Kutip_kitab
[rest] => Efesus
[2] => Efesus
)
[6] => Array
(
[0] => Kutip_pasal=4
[first] => Kutip_pasal
[1] => Kutip_pasal
[rest] => 4
[2] => 4
)
[7] => Array
(
[0] => Kutip_ayat=28
[first] => Kutip_ayat
[1] => Kutip_ayat
[rest] => 28
[2] => 28
)
[8] => Array
(
[0] => Tema_sumber=Kiriman dari Maurits albert
[first] => Tema_sumber
[1] => Tema_sumber
[rest] => Kiriman dari Maurits albert
[2] => Kiriman dari Maurits albert
)
[9] => Array
(
[0] => Kategori:e
[first] => Kategori
[1] => Kategori
[rest] => e
[2] => e
)
)
Hope it helps.
Apart from the question of "which pattern", I suggest you take a look into preg_replaceDocs which is able to operate on arrays directly.
$pattern = "...";
$matches = preg_replace($pattern, '\1', $array);
It is not clear from the question, but it looks like you want preg_filter.
This function can perform a regex match and replace on an array, returning a new array containing only the replaced values of the matched items.