PHP - Sort multi-dimensional array of number arrays - php

I have an array:
$arr = [
[4,6],
[1,2,3],
[7,8,9]
];
I'd like to sort it so the result is
$arr = [
[1,2,3],
[4,6],
[7,8,9]
];
If I apply sort($arr), it first sorts by array length and then compares the values. So I get
$arr = [
[4,6],
[7,8,9],
[1,2,3],
];
which is wrong for my purposes.
I could use a sorting algorithm to compare the elements.
Or I could create another array with each element imploded and then sort it.
But was wondering if there was an inbuilt or quicker way of getting this?
Thanks

You can use rsort() to get the order you are expecting.

Related

Use array column to generate new array's keys and values

I have an array of associative arrays like this:
$array = [
["foobar" => "asd"],
["foobar" => "abvc"],
["foobar" => "test123"],
];
I would like to have the column values used as keys and values in the end result.
[
'asd' => 'asd',
'abvc' => 'abvc',
'test123' => 'test123',
];
I tried using array_flip() while looping over, but I did not manage to get the desired output.
Use array_column to get all the nested foobar values. Then use array_combine to make an associative array using that as both the keys and values.
$values = array_column($array, 'foobar');
$result = array_combine($values, $values);
There is no need to call array_combine(). Tell array_column() to assign foobar column values as values (2nd param) and keys (3rd param).
Code: (Demo)
var_export(
array_column($array, 'foobar', 'foobar')
);

Given array keys that correspond to another array, how can I merge them?

I have the following array that is supposed to only be keys:
$keys = ['mod_4_key'];
and the bigger array which contains a lot of information:
$big_array = [ 'mod_4_key' => ['old' => '', 'info' => ''], 'mod_5_key' => ..]
I would like to, based on what is inside $keys generate a new array with the information from $big_array, as such, if we are to compute the "non-difference" between the arrays, the output should be:
$final_array = [ 'mod_4_key' => ['old' => '', 'info' => '']]
I achieved this using a classic foreach but I was wondering if there was no in-built way to achieve this.
You may be better off with a simple foreach() loop, but there are probably several ways of achieving this.
This uses array_flip() on the $keys, so that you end up with another associative array, then use array_intersect_key() with the big array first.
$final_array = array_intersect_key($big_array, array_flip($keys));

How can you get unique values from multiple arrays using array_unique?

Is there any way to put two or more arrays into the array_unique() function?
If not, is there any other solution to get unique results from multiple arrays?
The answer to the first part of your question is NO.
The array_unique function definition in the PHP manual states that array_unique takes exactly two arguments, one array, and an optional integer that determines the sorting behavior of the function.
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
Rather than take the manual's word for it, here are some test arrays.
$one_array = ['thing', 'another_thing', 'same_thing', 'same_thing'];
$two_arrays = ['A', 'B', 'C', 'thing', 'same_thing'];
$or_more_arrays = ['same_thing', 1, 2, 3];
A couple of test show that the function does work as advertised:
$try_it = array_unique($one_array);
returns ['thing', 'another_thing', 'same_thing'];
$try_it = array_unique($one_array, $two_arrays);
gives you a warning
Warning: array_unique() expects parameter 2 to be integer, array given
and returns null.
$try_it = array_unique($one_array, $two_arrays, $or_more_arrays);
also gives you a warning
Warning: array_unique() expects at most 2 parameters, 3 given
and returns null.
The answer to the second part of your question is YES.
To get unique values using array_unique, you do have to have one array of values. You can do this, as u_mulder commented, by using array_merge to combine the various input arrays into one before using array_unique.
$unique = array_unique(array_merge($one_array, $two_arrays, $or_more_arrays));
returns
['thing', 'another_thing', 'same_thing', 'A', 'B', 'C', 1, 2, 3];
If instead of several individual array variables, you have an array of arrays like this:
$multi_array_example = [
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]
];
Then you can unpack the outer array into array merge to flatten it before using array_unique.
$unique = array_unique(array_merge(...$multi_array_example));
Or in older PHP versions (<5.6) before argument unpacking, you can use array_reduce with array_merge.
$unique = array_unique(array_reduce($multi_array_example, 'array_merge', []));
returns [1, 2, 3, 4, 5, 6]
By default, array_unique() takes two parameters, the array, of type array takes the input array and the second parameter, sorting of type int takes the sorting behavior. You can read more on it in the PHP manual for array_unique().
But as suggested by #u_mulder, you merge the array first, probably into a temporary array (you can learn how to do that here) and then apply the array_unique to that temporary array.

Function that checks for keys in array and return array with those keys

I remember that there were function that have such functionality. Unfortunately, I don't remember the name of it. Basically, it did something like...
$values = foo(array('x', 'y', 'z'), $_POST);
If there are such keys in the array, it does return new array (named $values) with only those keys... taken from $_POST. If one or more keys aren't in $_POST, it simply returns false.
Anyone remember something like that or I was just dreaming? Thanks in advice!
http://www.php.net/manual/en/function.array-intersect-key.php
I think the function you are looking for is array_intersect_key() As of PHP 5.1.0.
array array_intersect_key ( array $array1 , array $array2 [, array $ ... ] )
Parameters
array1 - The array with master keys to check.
array2 - An array to compare keys against.
array - A variable list of arrays to compare.
Returns an associative array containing all the entries of array1 which have keys that are present in all arguments.
check out this function array_key_exists
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array))
{
echo "The 'first' element is in the array";
}
?>
http://www.php.net/manual/en/function.array-key-exists.php

What's the best way to copy an array and keep the keys with empty values

In PHP, what's the best way to copy an array and keep the keys with empty values?
array1 = array("apple" => "green", "banana" => "yellow);
I want to copy array1 to array2 and keep only the keys...
array2 = array("apple" => "", "banana" => "");
return array_fill_keys(array_keys($array1), "");
(Example run: http://www.ideone.com/SuMt2)
What about array_keys(), it return an array that has the keys of another array as values.
You can then use array_flip() to change keys for values and voilá, you have your result.
In a nutshell:
$array2 = array_flip(array_keys($array1));
you can use foreach to produce a new array, or one of numerous array functions

Categories