Array diff implementation in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Does anybody know of an array diff implementation in PHP? I need to use this to develop a feature similar to the way stackexchange diffs tags.

Like the documentation says:
Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.
For example:
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
Would end up with $result containing only the value blue, because it is only in one of the arrays.
Complete documentation here: http://php.net/manual/en/function.array-diff.php

There is array_diff which will compare the values of 2 arrays and return an array with values of the differences.
$arrayone = array("bacon" => "tasty", "lettuce", "carrot");
$arraytwo = array("ham" => "tasty", "carrot");
$differences = array_diff($arrayone, $arraytwo);
var_dump($differences);
$differences = array_diff($arraytwo, $arrayone);
var_dump($differences);
This will give:
array (size=1)
0 => string 'lettuce' (length=7)
array (size=0)
empty
One important thing is it only goes one way the first array is compared to the other ones passed.
http://php.net/manual/en/function.array-diff.php

Related

I have two array in php. Need to combing in one at following way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have two arrays:
$array1 = array('104', '104', '104', '51', '228', '228');
$array2 = array('12121', '12120', '12119', '11821', '11788', '11787');
I need to create an array with two dimensions consisting of the elements of these two arrays in a specific way:
$array3 = array('104'=>array('12121', '12120', '12119'),'51'=>array('11821'),'228'=>array('11788', '11787'));
Array1 and Array2 always have the same number of elements.
How can I do this?
You can do it this way:
<?php
$array1 = ['104', '104', '104', '51', '228', '228'];
$array2 = ['12121', '12120', '12119', '11821', '11788', '11787'];
$result = [];
$index = 0;
foreach( $array1 as $key => $value ){
$result[$value][] = $array2[$index];
$index++;
}
Result:

Convert one dimensional key-value array into two dimensional kay-value array every nth key with new key names [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an array that looks like this:
And I need to translate it into this:
Some things to know about the $startArray is that it is dynamic based on the amount of people submitted on a form. Each person always has just those three fields though (custom_12, custom_13, custom_14 aka fName, lName, email). So if there were 5 members, the 5th members fName would be the key custom_12-5 in the start array.
I've been looking around on stackoverflow for a question just like this and I was not able to find one. What would be the steps taken and possible array functions or anything else for creating the $endArray ?
There's actually a builtin function for this: https://www.php.net/manual/en/function.array-chunk.php
For example, array_chunk($startArray, 3) will give you the base for your new array, you'll just need to then iterate through it and rename the keys.
Alternatively, just iterate through the array yourself and add the values to a new array depending on the index of the current iteration.
Thanks to Charlie's advice, I came up with this.
$startArray = array(
'custom_12' => 'john',
'custom_13' => 'johny',
'custom_14' => 'john#johny.com',
'custom_12-2' => 'bob',
'custom_13-2' => 'bobby',
'custom_14-2' => 'bob#bobby.com',
'custom_12-3' => 'don',
'custom_13-3' => 'donny',
'custom_14-3' => 'don#donny.com'
);
$middleArray = array_chunk($startArray, 3);
$endArray = array_map(function($val) {
return array(
'fName' => $val[0],
'lName' => $val[1],
'email' => $val[2]
);
}, $middleArray);
echo "<pre>";
print_r($endArray);
echo "</pre>";
And the output is exactly what I wanted:

Using array_map vs array_column and array_combine in php [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Let's say I have an associative array like below:
$myAssocArray = [
[ 'Name' => 'AWS_Bucket-Name1', 'Value' => 'Bucket data',...],
[ 'Name' => 'AWS_Bucket-Name2', 'Value' => 'Bucket data',...],
[ 'Name' => 'AWS_Bucket-Name3', 'Value' => 'Bucket data',...],
[ 'Name' => 'AWS_Bucket-Name4', 'Value' => 'Bucket data',...],
];
From this I want to create a simple associative array like below:
[ ['AWS_Bucket-Name1' => 'Bucket data'], ['AWS_Bucket-Name2' => 'Bucket data'], ...]
So I can take two approaches to resolve this:
(1) Use array_map function in php:
$destinationArray = array_map('myMapFunction', $myAssocArray);
function myMapFunction($entity): array {
if (is_array($entity) && isset($entity['Name'], $entity['Value'])) {
return [$entity['Name'] => $entity['Value']];
}
}
OR
(2) Using array_column and array_combine functions:
$keys = array_column($myAssocArray, 'Name');
$values = array_column($myAssocArray, 'Value');
$destinationArray = array_combine($keys, $values);
What will be more recommended approach and which will be more performant and efficient? OR is there even better solution available that does not need foreach loop?
I wouldn't look at the performance here because these operations are very small. I would focus more on the clean code side.
array_map() solution is harder to understand at the first look.
You don't need to use array_combine() and double invocation of array_column(). Instead you can pass third argument to array_column() which is
"index key"
The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.
The following code
array_column($myAssocArray, 'Value', 'Name')
prints the same output as desired.
Have in mind that PHP internally can notice and optimize a lot of things to improve performance. Obviously, it's better when developer does so, but sometimes you gain 1 μs of performance with the cost of clean code and headache of a developer maintaining that code.
Working fiddle
More info on php.net/array_column

How build nested associative array based of a one-dimensional array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have one-dimensional array, e.g.
$arr = ['foo', 'bar', 'baz'];
I want convert it as follow(var_dump output):
array (size=1)
'foo' =>
array (size=1)
'bar' =>
array (size=1)
'baz' => string '' (length=0)
I can use only loop(for and/or foreach). Recursive functions is not allowed. PHP as primary programming language.
Please, help me write code for this transformation.
$r = array('a', 'b', 'c');
$res = array();
foreach (array_reverse($r) as $i) {
$tmp = $res;
$res = array();
$res[$i] = $tmp;
}
echo '<pre>', print_r($res);

PHP slice associative array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is my array
$array = array(
"13111" => "2014-06-21 19:51:00.0000000",
"23111" => "2014-06-20 19:51:00.0000000",
"12111" => "2014-06-21 19:51:00.0000000",
"23311" => "2014-06-22 19:51:00.0000000",
"13114" => "2014-06-21 19:51:00.0000000",
"23711" => "2014-06-20 19:51:00.0000000",
);
How can i get first 3 elements of my array and how can i sort by datetime? thanks
What you want is:
sort($array);
$array = array_slice($array, 0, 3);
first, the sort function will sort them lexicographically (which in this case coincides with the date) and then you slice it to get the elements you want.
EDIT
If you want to preserve the keys just use
asort($array); // "asort" instead of simple "sort"
$array = array_slice($array, 0, 3, true); // note the final "true" parameter!

Categories