php array_unique with exceptions - php

I want to remove duplicate values in an array except 1 value.
Eg:
$array = array ("apple", "orange", "orange", "banana", "grapes","grapes", "apple");
How can I remove all duplicate values and keep all duplicate values that equal "apple"
$array = array ("apple", "orange", "banana", "grapes", "apple");
There are about 400 values

$seen = array()
foreach ($array as $value)
if ($value == 'apple' || !in_array($value, $seen))
$seen[] = $value;
$seen will now have only the unique values, plus the apple.

$numbers = array_count_values($array);
$array = array_unique($array);
$array = array_merge($array, array_fill(1, $numbers['apple'], 'apple'));

$array = array ("apple", "orange", "orange", "banana", "grapes","grapes", "apple");
$counts = array_count_values($array);
$new_array = array_fill(0, $counts['apple']-2, 'apple'); // -2 to handle there already being an apple from the array_unique count below.
$new_array = array_merge(array_unique($array), $new_array);

Related

Combine array and add key for each array

I just can't find the right question for this
pretty much merging array From this
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
to this
$combined = [["name" => "temp1", "number" => 5], ["name" => "temp2", "number" => 7], ["name" => "temp3", "number" => 2]];
any idea to do it in most efficient way other than foreach?
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
foreach($arr1 as $key => $value)
{
$r[$key]['name'] = $value;
$r[$key]['number'] = $arr2[$key];
}
print_r($r);
Built-in function array_map actually can work with multiple arrays:
$result = array_map(function($value1, $value2) {
return ["name" => $value1, "number" => $value2];
}, $arr1, $arr2);
Here are some benchmarks comparing with simple foreach
You can use a foreach loop,
$result = [];
foreach($arr1 as $key => $value){
$resutl[] = array("name"=>$value,"number"=>$arr2[$key]);
}
You can do the following code to get your result
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
$count = count($arr1);
$combined = array();
for($i=0;$i<$count;$i++){
$combined[$i]['name'] = $arr1[$i];
$combined[$i]['number'] = $arr2[$i];
}

How to merge arrays in PHP with a same value

I want to merge two arrays with the same value -> user_id to one array.
You can see I have two arrays with different objects. I want merge them by user_id to one array with all objects.
For example:
$array1 = [
$array[0]->leads = 5643;
$array[0]->user_id= 15;
$array[0]->sales = 1433;
$array[1]->leads = 3264;
$array[1]->user_id= 9;
$array[1]->sales = 1254;
];
$array2 = [
$array[0]->user_id= 15;
$array[0]->processing = 2300;
$array[0]->deleted = 203;
$array[1]->user_id= 9;
$array[1]->processing = 103;
$array[1]->deleted = 80;
];
The following array is the target.
$result = [
$array[0]->user_id= 15;
$array[0]->processing = 2300;
$array[0]->leads = 5643;
$array[0]->deleted = 203;
$array[0]->sales = 1433;
$array[1]->user_id= 9;
$array[1]->processing = 103;
$array[1]->leads = 3264;
$array[1]->deleted = 80;
$array[1]->sales = 1254;
];
Iterate your array and build the combined array of objects using user_id as the key.
foreach ($array as $entry) {
// if an entry for this user id hasn't been created in the result, add this object
if (!isset($result[$entry->user_id])) {
$result[$entry->user_id] = $entry;
// otherwise, iterate this object and add the values of its keys to the existing entry
} else {
foreach ($entry as $key => $value) {
$result[$entry->user_id]->$key = $value;
}
}
}
Given the additional info you just gave (two separate arrays) the solution is basically the same, just merge the two arrays together first.
foreach (array_merge($array1, $array2) as $entry) { ...
(Working example at https://3v4l.org/ccdQc)
How about array_merge,
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
or array_merge_recursive,
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
Example
$array = [
1 => [
'cost' => '25'
],
2 => [
'blah' => 'test'
]
];
$new_array = array_merge_recursive($array[1], $array[2]);
var_dump($new_array);
Output
array(2) {
["cost"]=>
string(2) "25"
["blah"]=>
string(4) "test"
}
Live Example
Repl
<?php
$array1= array("leads"=> 5643,"user_id"=>15,"sales" =>1433);
$array2= array("user_id"=> 15,"processing" => 2300,"deleted" => 203);
$array= (array_merge($array1,$array2));
print_r($array);
?>

Append an array to each entry of another array

I am trying to generate an array containing an array for each of the items
the code i tried:
$array = array(1,2,3,4,5,6,7,8,9,10 );
$array2 = array('how','are','you') ;
foreach( $array2 as $key => $value ){
$value = $array2;
}
result of this code
array(3) {
[0]=>
string(3) "how"
[1]=>
string(3) "are"
[2]=>
string(3) "you"
}
The desired result is an array where each of the words contains the following values:
how = 1,2,3,4,5,6,7,8,9,10
are = 1,2,3,4,5,6,7,8,9,10
you = 1,2,3,4,5,6,7,8,9,10
assign the array of numbers in each words.
$array = array(1,2,3,4,5,6,7,8,9,10 );
$array2 = array('how','are','you') ;
$newArray = [];
foreach( $array2 as $key => $value ){
$newArray[$value] = $array;
}
Using implode() to make it string
$array = array(1,2,3,4,5,6,7,8,9,10 );
$array2 = array('how','are','you') ;
$arr = [];
foreach( $array2 as $key => $value ){
$arr[$value] = implode(",",$array);// using implode
}
print_r($arr);
OUTPUT
Array
(
[how] => 1,2,3,4,5,6,7,8,9,10
[are] => 1,2,3,4,5,6,7,8,9,10
[you] => 1,2,3,4,5,6,7,8,9,10
)
This is an alternative approach for achieving desired output. Here we are using array_fill and array_combine to get the desired output. With this array_fill(0, count($array2), $array); we are creating an array of values with $array to the count of $array2, Then we are using array_combine to make $array2 as keys $values as values.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$array = array(1,2,3,4,5,6,7,8,9,10 );
$array2 = array('how','are','you') ;
$values=array_fill(0, count($array2), $array);//here we are creating an array with $array
print_r(array_combine($array2, $values));
You can use array_combine, check the live demo.
<?php
$array = array(1,2,3,4,5,6,7,8,9,10 );
$array2 = array('how','are','you') ;
print_r(array_combine($array2, array_fill(0, count($array2), implode(',', $array))));

Combine a value from an array with each values from other?

What i want is to combine every value from first array with every value from second array.
For example, let's take two arrays:
$array1 = ['green', 'red', 'blue'];
$array2 = ['s', 'm'];
The result array should be:
$result = [1 => 'green-s', 2 => 'green-m', 3 => 'red-s', 4 => 'red-m', 5 => 'blue-s' ...];
The result array can be different, but with that elements combined.
check this,
<?php
$array1 = array('green', 'red', 'blue');
$array2 = array('s', 'm');
$data = array();
foreach($array1 as $val){
foreach($array2 as $val2){
$data[] = $val."-".$val2;
}
}
print_r($data);
?>

Pushing multiple arrays to slice into one || PHP

So I have something like:
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array();
array_push($myArray, $array, $array2 );
$myArray = array_slice($myArray, 0, 2);
and I want $myArray to be ["red", "yellow"], and if $array was empty $myArray would be ["1", "2"]
Does that make any sense? Right now array_slice is counting the arrays being pushed into $myArray, not the content inside them. How would I go about doing this?
Where you use array_push you probably want to use array_merge:
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array_merge($array, $array2);
$myArray = array_slice($myArray, 0, 2);
Explanation:
array_push pushes the elements to the end of the existing array, so
$array = array('a');
array_push($array, 'b');
// results in $array = array('a', 'b');
Thus in your code, just after the array_push-call
$myArray = array(array('red', 'yellow', ...), array('1', '2', ...))
array_merge merges two or more arrays
$myArray = array_merge($array, $array2);
// results in $myArray = array('red', 'yellow', ..., '1', '2', ...)
Try using array_merge() instead of array_push()
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array_merge($array, $array2 );
$myArray = array_slice($myArray, 0, 2);
array_push() is adding each array as an element of $myArray instead of combining them with it.

Categories