PHP Change Array Keys

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



Leave a reply



Submit