How to Rename Sub-Array Keys in PHP

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 rename array keys in multidimensional array

foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = $array[$k] ['fee_id'];
unset($array[$k]['fee_id']);
}

This should work

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);

Recursively rename caps in array keys to dash and lower case

This looks pretty obvious to me:

        if(is_array($value)){
$value = changeArrayKeys($value);
}else{
$key = strtolower(trim(preg_replace('/([A-Z])/', '_$1', $key)));
}

If the $value is an array, you do a recursive call, but you do not change the key of that nested array in any way. It could help to move the $key manipulation out of the else branch

PHP Rename all the keys in arrays

This way you can change keys in your array:

for ($i=0, $c = count($array); $i<$c; ++$i) {
$array[$i]['id'] = $array[$i][0];
$array[$i]['name'] = $array[$i][1];
$array[$i]['status'] = $array[$i][2];
unset($array[$i][0];
unset($array[$i][1];
unset($array[$i][2];
}

You have to use syntax $array[$key1][$key2] to use multidimensional array.

How to rename an array?

this is what i want!

 $searchRs['searchRs'] = $resultArr->d;
unset($resultArr->d);
return $searchRs;

Rename PHP array key and maintain element data

The following will do what you deed:

$myArray = array(
array(
'store_ni' => 'Store One',
'id_ni' => 123456,
'title_ni' => 'Product One',
'price_ni' => '$9.00'
),
array(
'store_ds' => 'Store Two',
'id_ds' => 789012,
'title_ds' => 'Product Two',
'price_ds' => '$8.00'
)
);

$newKeys = array('Store', 'ItemID', 'Description', 'Price');

$result = array();
foreach($myArray as $innerArray)
{
$result[] = array_combine(
$newKeys,
array_values($innerArray)
);
}

array_combine() combines the first array you pass to it and assign it as the keys of the returned array and the second array you pass it as the values of that array.



Related Topics



Leave a reply



Submit