Change an array value if duplicate - php

I have an array which holds some articles of a website. The problem is: when the same article is in 2 different categories it only shows 1. In this example the categories are 'Service' and 'Sales'.
If the article is duplicate and the category is 'Sales' I want 1 of them to change to 'Service' and vice versa.
The array I got now(3 & 4 as duplicates and 7 & 8):
Array
(
[0] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/38BbjLmVJXk
[3] => Park assist
)
[1] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/3lGfTZdVK1s
[3] => Multi Collision braking system
)
[2] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/6mgDraWpGvE
[3] => Area view
)
[3] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[4] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[5] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/N0fa4dUBkvE
[3] => Trailer assist
)
[6] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/NCNDyW6Yr1g
[3] => Ruitenwissers
)
[7] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
[8] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
)
The array I want to accomplish (No duplicates anymore and the values changed):
Array
(
[0] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/38BbjLmVJXk
[3] => Park assist
)
[1] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/3lGfTZdVK1s
[3] => Multi Collision braking system
)
[2] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/6mgDraWpGvE
[3] => Area view
)
[3] => Array
(
[0] => Service
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[4] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[5] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/N0fa4dUBkvE
[3] => Trailer assist
)
[6] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/NCNDyW6Yr1g
[3] => Ruitenwissers
)
[7] => Array
(
[0] => Sales
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
[8] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
)
What I tried:
$length = count ($sorted);
for ($c = 0; $c < $length; $c++) {
$check_array = $sorted[$c];
for ($x = 0; $x < $length; $x++) {
$compare_array = $sorted[$x];
if ($x != $i){
if($check_array[2] == $compare_array[2] && $check_array[0] == $compare_array[0]){
//print_r ($check_array);
//print_r ($compare_array);
if($check_array[0] == 'Sales'){
$compare_array[0] = 'Service';
}
if($check_array[0] == 'Service'){
$compare_array[0] = 'Sales';
}
}
}
}
}
Any help will be much appreciated.

Put your data in $array variable and try it:
$hash = array_map( function ( $value ) { return md5( implode( $value ) ); } , $array);
$keys = array_keys( array_diff_assoc( $hash, array_unique( $hash ) ) );
foreach( $keys as $key )
$array[ $key ][0] = $array[ $key ][0] == 'Service' ? 'Sales' : 'Service';

If the order is not important for you, you can use array_multisort, array_column and array_walk:
$replacements = [
'Sales' => 'Service',
'Service' => 'Sales'
];
// Sort array by youtube url.
array_multisort($array, array_column($array, 2));
array_walk($array, function (&$curr, $_, &$prev) use ($replacements) {
if (
$prev !== null
&& $prev[2] === $curr[2]
) {
$curr[0] = $replacements[$curr[0]];
}
$prev = $curr;
}, null);
Here is working demo.

Here I have a written some code, you can try it.
<?php
$array = [
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/38BbjLmVJXk',
3 => 'Park assist',
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/3lGfTZdVK1s',
3 => 'Multi Collision braking system'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/6mgDraWpGvE',
3 => 'Area view'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/II68oVm4zro',
3 => 'Lane Assist'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/II68oVm4zro',
3 => 'Lane Assist'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/N0fa4dUBkvE',
3 => 'Trailer assist'
],
[
0 => 'Service',
1 => 'veilig-op-de-weg',
2 => 'www.youtube.com/video/NCNDyW6Yr1g',
3 => 'Ruitenwissers'
],
[
0 => 'Service',
1 => 'veilig-op-de-weg',
2 => 'www.youtube.com/video/PJEC-yqUwzE',
3 => 'Bandenafdichtset'
],
[
0 => 'Service',
1 => 'veilig-op-de-weg',
2 => 'www.youtube.com/video/PJEC-yqUwzE',
3 => 'Bandenafdichtset'
]
];
/**
* #param $firstIndexValue
* #param $thirdIndexValue
* #param $searchArray
* #return bool
*/
function isExistValue($firstIndexValue, $thirdIndexValue, $searchArray)
{
foreach ($searchArray as $element) {
if ($firstIndexValue == $element[0] && $thirdIndexValue == $element[3]) {
return true;
}
}
return false;
}
$newArray = [];
foreach ($array as $arr) {
$isExist = isExistValue($arr[0], $arr[3], $newArray);
if ($isExist) {
if ($arr[0] == 'Sales') {
$arr[0] = 'Service';
} elseif ($arr[0] == 'Service') {
$arr[0] = 'Sales';
}
}
$newArray[] = $arr;
}
var_dump($newArray);

Related

compare values from multidimensional array and add key to array

Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)

Php multidimensional array

I have build this array structure from dig query data.
[10] => Array
(
[id] => 150
[0] => 200.201.202.23
[1] => dns.name1.com
[2] => 200.201.202.24
[3] => dns.name2.com
[4] => 200.201.202.25
[5] => dns.name3.com
) `
I need something like:
[10] => Array
(
[0] => array ( [0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => array ( [0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => array ( [0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
) `
I'm not sure if this is possible?
Heres the code where i create the array:
At the first time from the dig i use array_push() to add content to it.
$temp = array();
$i = 0;
foreach ($digResult as $single){
if (preg_match('/(?:^|\s+)(\d+)(?:\s+|\n+|$)/', $single )){
$temp []["id"]= $single;
$i++;
}else {
$temp[$i][] = $single;
}
}
This will work for you :
<?php
$dataArray = array(10 => array
(
'id' => 150 ,
0 => '200.201.202.23' ,
1 => 'dns.name1.com',
2 => '200.201.202.24',
3 => 'dns.name2.com',
4 => '200.201.202.25',
5 => 'dns.name3.com',
)
);
$newArray = array();
$id = $dataArray[10]['id'];
for($i=0; $i< 6; $i++)
{
$newArray[10][] = array(0=>$dataArray[10][$i],1=>$dataArray[10][$i+1],'id'=> $id);
$i+=1;
}
print_r($newArray);
?>
This will output
Array
(
[10] => Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
)
LIVE EXAMPLE : CLICK HERE
Try this:-
<?php
$array = array(
'id' => '150',
'0' => '200.201.202.23',
'1' => 'dns.name1.com',
'2' => '200.201.202.24',
'3' => 'dns.name2.com',
'4' => '200.201.202.25',
'5' => 'dns.name3.com'
);
$i = 0;
$arrayLenght = (count($array)-2);
$newArray = array();
while ($i <= $arrayLenght) {
$newArray[] = array(
"0" => $array[$i++],
"1" => $array[$i++],
"id" => $array['id']
);
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
?>
Output:-
Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)

Create Array From Current Array using PHP

This is the result of array after i build it using array_push function from mssql result.
Array
(
[0] => Array
(
[STICKER] => FALCON
[MONTH] => 1
[JUM] => 65826210.00
)
[1] => Array
(
[STICKER] => FALCON
[MONTH] => 2
[JUM] => 68070573.00
)
[2] => Array
(
[STICKER] => FALCON
[MONTH] => 3
[JUM] => 99053067.60
)
[3] =>
[4] => Array
(
[STICKER] => HRD
[MONTH] => 2
[JUM] => 1521400.00
)
[5] => Array
(
[STICKER] => HRD
[MONTH] => 3
[JUM] => 2093200.00
)
)
I need to convert array above into this structure:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210.00
[2] => 68070573.00
[3] => 99053067.60
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400.00
[3] => 2093200.00
)
)
Note:
Array[0] values would be 1,2,3 (this is actualy month, i just input up to 3 in order to get the code not too long. but it will be up to 12 (Jan - Dec)).
If from original array, there is none value (example from array[3]), then it will be convert to new array[2]->[1] with value 0.
Any help would be very appreciated.
Thanks all!.
Try this,
If any month is not mentioned from stickers the jum considered as 0,
$array = array(
array('STICKER' => 'FALCON', 'MONTH' => 1, 'JUM' => 65826210.00),
array('STICKER' => 'FALCON', 'MONTH' => 2, 'JUM' => 68070573.00),
array('STICKER' => 'FALCON', 'MONTH' => 3, 'JUM' => 99053067.60),
array(),
array('STICKER' => 'HRD', 'MONTH' => 2, 'JUM' => 1521400.00),
array('STICKER' => 'HRD', 'MONTH' => 3, 'JUM' => 2093200.00),
);
$result[0][] = '';
foreach ($array as $key => $res) {
if (!empty($res)) {
$result[0][$res['MONTH']] = $res['MONTH'];
$result1[$res['STICKER']][$res['MONTH']] = $res['JUM'];
}
}
foreach ($result1 as $key => $res) {
$fin = array();
foreach ($res as $key1 => $re) {
foreach ($result[0] as $key2 => $month) {
if ($month != '') {
if (array_key_exists($month, $res)) {
if (!array_key_exists($month, $fin) && $month == $key1) {
$fin[$month] = $re;
}
} else {
$fin[$month] = 0;
}
}
}
}
$result[] = array_merge(array($key), $fin);
}
echo'<pre>';
print_r($result);
echo'<pre>';
Result:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210
[2] => 68070573
[3] => 99053067.6
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400
[3] => 2093200
)
)
I hope this is used to achieve your output(you mentioned in above question)!!
try something like this
not the issue with the empty array i could not understand how the program would know that it is STICKER HRD so it will be filled with zero.But you could later check if every month "isset" and if it is not act as it is zero
$arr1=array(
array('STICKER'=>'FALCON','MONTH'=>1,'JUM'=>65826210.00),
array('STICKER'=>'FALCON','MONTH'=>2,'JUM'=>68070573.00),
array('STICKER'=>'FALCON','MONTH'=>3,'JUM'=>99053067.60),
array(),
array('STICKER'=>'HRD','MONTH'=>2,'JUM'=>1521400.00),
array('STICKER'=>'HRD','MONTH'=>3,'JUM'=>2093200.00),
);
$arr2=array();
$arr3=array();
$arr2[0][0]="";
foreach($arr1 as $key => $val){
if(!empty($val)){
$arr2[0][$val['MONTH']]=$val['MONTH'];
//group each STICKER
$arr3[$val['STICKER']][]=$val['JUM'];
}
}
//transfer the grouped data to arr2
foreach($arr3 as $key => $val){
$tmp_arr=array($key);
$arr2[]=array_merge($tmp_arr,$val);
}
print_r($arr2);

Order array multi-dimensional

I have an array that looks like this:
$cars = array (
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'peugeot',
'b' => '306'),
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'audi',
'b' => 'a5'),
array(
'a' => 'peugeot',
'b' => '106'),
array(
'a' => 'peugeot',
'b' => '106'),
);
I need to order arrays like this to (id is the same as name):
name => audi
id=> audi
data => a4 => 2
a5 => 1
name => peugeot
id=> peugeot
data => 306 => 1
106 => 2
So the car brands need to be grouped an the car types counted.
I already have this code; but that is only for the group part and the count part is missing.
function mergeAndOrder($data){
// set group arrays
$i = 0; $group1 = array();
// loop trough array
$array = array(); $array2 = array();
if($data != null){
foreach($data AS $row){
// search and order level1
$search = array_search($row->a,$group1);
// this object is not found
if(is_int($search) == false){
$group1[$i] = $row->a;
$array[$i]['id'] = $row->a;
$array[$i]['name'] = $row->a;
$array[$i]['data'] = array();
$i++;
}
}
}
return $array;
}
Does somebody know an solution for this case? Thanks!
--- INPUT (part of) ---
a = lease company in this case
Array
(
[0] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[1] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[2] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[3] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
--- OUTPUT (part of) ---
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
[3] => Array
(
[id] => LPD
[name] => LPD
[data] => Array
(
)
)
)
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
If I understand your question correctly, this should do what you want.
function mergeAndOrder ($data) {
$output = array();
foreach ($data as $item) {
$id = $item->a;
$value = $item->b;
if (!array_key_exists($id, $output)) {
$output[$id] = array('id' => $id, 'name' => $id, 'data' => array());
}
if (!array_key_exists($value, $output[$id]['data'])) {
$output[$id]['data'][$value] = 0;
}
$output[$id]['data'][$value]++;
}
// Order by name element
uasort($output, function ($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
return $output;
}
Output:
Array
(
[audi] => Array
(
[id] => audi
[name] => audi
[data] => Array
(
[a4] => 2
[a5] => 1
)
)
[peugeot] => Array
(
[id] => peugeot
[name] => peugeot
[data] => Array
(
[306] => 1
[106] => 2
)
)
)

Rearrange multi Arrays in PHP

I have the array about:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[product] => Array
(
[0] => t-shirt
[1] => earing
[2] => clock
)
[price] => Array
(
[0] => 100.00
[1] => 32.00
[2] => 898.00
)
)
I want to do this:
Array
(
[0] => Array
(
[0] => 1
[1] => t-shirt
[2] => 100.00
)
[1] => Array
(
[0] => 2
[1] => earing
[2] => 32.00
)
[2] => Array
(
[0] => 3
[1] => clock
[2] => 898.00
)
)
You can try with:
$input = array( /* your input array */ );
$output = array();
foreach ($input as $data) {
for ($i = 0; $i < count($data); $i++) {
if (!isset($output[$i])) {
$output[$i] = array();
}
$output[$i][] = $data[$i];
}
}
Well or like this.
$test = array(
'id' => array(
1,2,3
),
'product' => array(
'tshirt', 'ewew', 'shorts'
),
'price' => array(
'10.00', '20.00', '30.00'
)
);
$newarray = array();
foreach($test['id'] as $k => $v){
$newarray[$k] = array(
$v, $test['product'][$k], $test['price'][$k]
);
}
echo '<pre>';
print_r($newarray);
Example live

Categories