Replace keys in an array based on another lookup/mapping array
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
How to change keys of a array in PHP?
array_column will be enough to help you with.
$result = array_column($yourarray, null, 'position');
column_key The column of values to return. This value may be an integer key of
the column you wish to retrieve, or it may be a string key name for an
associative array or property name. It may also be NULL to return
complete arrays or objects (this is useful together with index_key to
reindex the array).
Syntax
array_column ( array $input , mixed $column_key [, mixed $index_key = NULL ] ) : array
Working demo.
Change Array Key Names with PHP
$string = '[{ "unicode" : "1f910" }, { "unicode" : "1f5e3" }, { "unicode" : "1f4a9" }]';
$array = json_decode($string);
$count = 1;
$final = array();
foreach ($array as $item) {
$final['unicode_'.$count] = $item->unicode;
$count++;
}
print_r($final); die;
if you want json then
$final = json_encode($final);
How to rename sub-array keys in PHP?
You could use array_map()
to do it.
$tags = array_map(function($tag) {
return array(
'name' => $tag['name'],
'value' => $tag['url']
);
}, $tags);
PHP Convert Array Keys
You want the array_values function.
replace array keys with given respective keys
array_combine(array_merge($old, $keyReplaceInfoz), $old)
I think this looks easier than what you posed.
PHP change array keys with key values from another array
Probably something like this:
foreach ($data as $key => $val){
$choices[$key] = $val;
array_flip($choices[$key]);
}
with array_combine
and array_flip
will work
Edited, not 100% sure what you're trying to achieve but this may help
Change all keys in a array
If you don't want to use a foreach
, you can use a combination of array_keys
, array_map
, and array_combine
to achieve this.
$array = array(
'mc_gross' => 10.17,
'protection_eligibility' => 'Eligible',
'address_status' => 'unconfirmed',
'payer_id' => 'STTAC7UV2CVJ4'
);
//Get keys
$keys = array_keys($array);
//Format keys
function map($key){
return str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
}
//Map keys to format function
$keys = array_map('map', $keys);
//Use array_combine to map formatted keys to array values
$array = array_combine($keys,$array);
var_dump($array);
This should output something like:
array(4){
["McGross"]=>float(10.17)
["ProtectionEligibility"]=>string(8) "Eligible"
["AddressStatus"]=>string(11) "unconfirmed"
["PayerId"]=>string(13) "STTAC7UV2CVJ4"
}
Relative docs:
http://php.net/manual/en/function.array-keys.php
http://php.net/manual/en/function.array-map.php
http://php.net/manual/en/function.array-combine.php
EDIT
As @Wrikken pointed out, the use of array_values
is redundant and not needed. Thanks for the pointer!
How to replace array keys with another array values
<?php
$array = array('A' => 'code', 'B' =>'name');
$replacement_keys = array
(
array("A"=>'sara','B'=>2020),
array("A"=>'ahmed','B'=>1010)
);
foreach($replacement_keys as &$value)
{
foreach ($array as $key => $name) {
$value[$name] = $value[$key];
unset($value[$key]);
}
}
var_dump($replacement_keys);
Related Topics
JSON_Encode Function: Special Characters
Accessing Class Properties with Spaces
Using X-Sendfile with Apache/Php
Get Date Range Between Two Dates Excluding Weekends
Best Way to Get Files from a Dir Filtered by Certain Extension in PHP
Send Cookie with File_Get_Contents
How to Make Number_Format() Not to Round Numbers Up
The Post Method Is Not Supported for This Route. Supported Methods: Get, Head. Laravel
Strip All HTML Tags, Except Allowed
How to Get Rid of MySQL Error 'Prepared Statement Needs to Be Re-Prepared'
Least Memory Intensive Way to Read a File in PHP
PHP Configuration: It Is Not Safe to Rely on the System's Timezone Settings
How to Replace (Monkeypatch) PHP Functions