Replace keys in an array based on another lookup/mapping array
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
Search for keys from an array within values of another array and replace
Need $i
referenced with &
to use this way:
foreach ($info as &$i){
$i[1] = str_replace(array_keys($map), $map, $i[1]);
}
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);
Replace one arrays keys with another arrays values in php
This is made quite nice with a foreach loop
foreach( $data as $origKey => $value ){
// New key that we will insert into $newArray with
$newKey = $map[$origKey];
$newArray[$newKey] = $value;
}
A more condensed approach (eliminating variable used for clarification)
foreach( $data as $origKey => $value ){
$newArray[$map[$origKey]] = $value;
}
Using PHP, search an array by key and replace value in another array with value from matched key
You can do it as following:
foreach($array_a as $elemKey => $elemValue){
foreach($elemValue as $itemKey => $itemValue){
if(isset($array_a[$elemKey][$itemKey]['label'])){
$array_a[$elemKey][$itemKey]['label'] = $array_b[$array_a[$elemKey][$itemKey]['label']];
}
}
}
print_r($array_a);
This will return:
Array
(
[0] => Array
(
[0] => Array
(
[name] => name
[label] => Name:
)
[1] => Array
(
[name] => phone_office
[label] => Office phone:
)
)
[1] => Array
(
[0] => Array
(
[name] => website
[label] => Website:
)
[1] => Array
(
[name] => phone_fax
[label] => Fax number:
)
)
)
Replace array keys (foreach loop) who matches another array
How about:
$replacements = array();
$replacements[2] = 'Car'; // your key should be the key of $array_a here and
$replacements[1] = 'Boat'; // value should be the key you want to be used
$replacements[5] = 'Bike';
$replacements[3] = 'Photo';
$finalPollArray = array();
foreach($replacements as $key => $value)
{
$finalPollArray[$value] = $array_a[$key];
}
$finalPollArray = asort($finalPollArray)
print "<pre>";
print_r($finalPollArray);
print "</pre>";
A very practical example would be:
$poll[5] = 13;
$poll[6] = 12;
$poll[3] = 10;
$poll[12] = 7;
$poll[8] = 6;
$poll[1] = 5;
$poll[7] = 5;
$poll[16] = 5;
$poll[13] = 4;
// with this code I get the following output
$replacements = array();
$replacements[5] = 'Car';
$finalPollArrayA = array();
foreach($replacements as $key => $value)
{
$finalPollArrayA[$value] = $poll[$key];
}
print "<pre>";
print_r($finalPollArrayA);
print "</pre>";
and this outputs me:
Array
(
[Car] => 13
)
is it as expected?
PHP - How to edit a key of an array?
well simple solution is:
$array[$newkey] = $array[$oldkey];
unset($array[$oldkey]);
In your case you would loop through:
$newArray = array();
foreach($array as $key => $value) {
$newKey = end(explode("\\", $key)); //need latest php for this otherwise split end and explode
$newArray[$newKey] = $value;
}
Related Topics
PHP Method Chaining or Fluent Interface
Reference: What Is a Perfect Code Sample Using the MySQL Extension
Detect Encoding and Make Everything Utf-8
When Should I Use Prepared Statements
When Do I Use the PHP Constant "PHP_Eol"
How to Remove "Index.PHP" in Codeigniter'S Path
Strange Character Encoding of Stored Data , Old Script Is Showing Them Fine New One Doesn'T
How to Use Filesystem Functions in PHP, Using Utf-8 Strings
Split a Comma-Delimited String into an Array
How to Change the Session Timeout in PHP
Convert a PHP Object to an Associative Array
Laravel 5 - Remove Public from Url
How to Check If a Url Exists Via PHP
How to Extract Data from CSV File in PHP