Sorting an array by its keys by another array - php

I need to sort an array by its keys based on the order of the values in another array.
Simple example:
$sort_array = array( 'key1', 'key2' );
$array_that_needs_sorting = array( 'key2' => 'value2', 'key1' => 'value1' );
After sorting, the array should be:
array( 'key1' => 'value1', 'key2' => 'value2' );

If you know the $sort_array keys are all present in the array that needs to be sorted, you can do this:
$sorted = array_merge(array_flip($keys), $unsorted);
where $keys is $sort_array and $unsorted is $array_that_needs_sorting.

You might take a look at Sort an Array by keys based on another Array?. It should give you an idea on how to accomplish that.

array_merge(array_combine($sort_array, array_fill(0, count($sort_array), null))
, $array_that_needs_sorting);

Related

How can i do merge arrays by my specific situation

I have to arrays, these:
$array1 = [
'red' => 'pencil',
'blue' => 'eraser',
];
$array2 = [
'reding' => '',
'bluing' => '',
];
and i wanna get this output:
$output = [
'reding' => 'pencil',
'bluing' => 'eraser',
];
So, values come from $array1 and keys come from $array2 on the $output.
Note: elements in array count is variable.
How can i do this ? Thanks for your helps.
if arrays are always the same length
array_combine(array_keys($array2), $array1);
array array_combine ( array $keys , array $values )
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
More Details here

How to remove a pattern in naming of keys of an array and returning it in PHP

Sample array:
array( 'keyword-one' => 'value', 'keyword-two' => 'value' );
What can I do so I get returned an array with keyword- omitted from the key so:
array( 'one' => 'value', 'two' => 'value' );
Use str_replace() to replace in the keys from array_keys() and combine with the values using array_combine():
$array = array_combine(str_replace('keyword-', '', array_keys($array)), $array);
For more complex patterns use preg_replace().

PHP Multidimensional Array Merge based on keys

i have this two arrays:
$array1 = [
'1' => 285.52,
'2' => 427.76
];
$array2 = [
'1' => 123.44,
'2' => 48.32
];
The keys on each of them are the id for the client, the first one is the amount owed and the second one is the amount payed, i want to achieve the following:
$mergedArrays = [
'1' => [
'owed' => 285.52,
'payed' => 123.44
],
'2' => [
'owed' => 427.76,
'payed' => 48.32
]
];
I was wondering if there's a PHP function to do so, i tried with array_merge_recursive but it just makes an array with the four elements together.
Any help would be really appreciated.
you can loop in the first array and merge the second according to keys
foreach($array1 as $key => $val) {
$mergedArrays[$key] = array('owed' => $val, 'payed' => $array2[$key]);
}
sample
$array1 = [
'1' => 285.52,
'2' => 427.76
];
$array2 = [
'1' => 123.44,
'2' => 48.32
];
$final_arr = array_map(function($a1, $a2) {
return array(
'owed' => $a1,
'paid' => $a2
);
}, $array1, $array2);
$final_arr = array_combine(array_keys($array1), $final_arr);
var_dump($final_arr);
Based on the comment, it seems you're looking for built-in PHP functions to do the task for you rather than going for traditional looping. But the looping method provided by Fabio is the simplest one you could go for without any other complicated approaches. I've tried my best to provide you the solution using the core PHP functions. Hope you're happy with it!

Recursively iterate over an array to change specific key's value

I have an array like this:
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
as you can see there are inner arrays inside $a
Now, I have a list of keys, example:
key1
key4
key6
I need a script that search if those key exists, and if exists change their values.
I Need to change their values with base64_encode($value_of_the_key)
so Maybe a callback that get the current value and convert it using base64_encode() function.
COuld someone help me?
I'm tring to see the current php functions but it seems there is not ones that do this thing.
THanks
EDIT:
Using the follow code i can get the keys in the callback....but the problem is:
How can i modify the values directly in the array? I Mean.... ok ... i get key and value, but how to change the value in the original array? ($a)
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
function test($item, $key)
{
echo "$key. $item<br />\n";
}
array_walk_recursive($a, 'test');
array_walk_recursive() with callback supplied should help. More info here.

referring to sister element inside array in php

I want to do something like the following (key3 is a combination of 1+2):
$a = array(
'key1' => 5,
'key2' => 10,
'key3' => $a['key1']+$a['key2'] // want it to be 15
);
How can I do this? Do i need to refer from outside the array and then merge them? Because this doesn't work!
Thanks in advance,
Maurice
You won't be able to initialize it that way because PHP hasn't finished going through and initializing the entire array yet, so it can't use its other values yet.
You'll have to do it after initializing the first two elements:
$a = array(
'key1' => 5,
'key2' => 10
); // At this point, $a is ready for use
$a['key3'] = $a['key1'] + $a['key2']; // Or simply = array_sum($a);
The solution proposing to update the table afterward are good, but you can also use variables for your data :
$var1 = 5;
$var2 = 10;
$a = array(
'key1' => $var1,
'key2' => $var2,
'key3' => $var1 + $var2
);
Why not do:
$a = array(
'key1' => 5,
'key2' => 10,
'key3' => 0
);
$a['key3'] = $a['key1']+$a['key2'];
It would have the desired result. However, I have a feeling that you are trying to make the third member change when one of the first two change, and I'm afraid you won't be able to accomplish that easily.

Categories