0

I am trying to uniformly rename all of my array keys using a foreach loop and unset.

Array before:

Array 
( [0] => Array ( 
 [store_nl] => Store One 
 [id_nl] => 123456 
 [title_nl] => Product One 
 [price_nl] => 9ドル.00 ) 
 [1] => Array ( 
 [store_ds] => Store Two 
 [id_ds] => 789012 
 [title_ds] => Product Two
 [price_ds] => 8ドル.00 ) 
) 

foreach using unset:

if(isset($data)){
 foreach ( $data as $k=>$v )
 {
 //Store One (ds)
 $data[$k]['Store'] = $data[$k]['store_ds'];
 $data[$k]['ItemID'] = $data[$k]['id_ds'];
 $data[$k]['Description'] = $data[$k]['title_ds'];
 $data[$k]['Price'] = $data[$k]['price_ds'];
 unset($data[$k]['store_ds']);
 unset($data[$k]['id_ds']);
 unset($data[$k]['title_ds']);
 unset($data[$k]['price_ds']);
 //Store Two (nl)
 $data[$k]['Store'] = $data[$k]['store_nl'];
 $data[$k]['ItemID'] = $data[$k]['id_nl'];
 $data[$k]['Description'] = $data[$k]['title_nl'];
 $data[$k]['Price'] = $data[$k]['price_nl'];
 unset($data[$k]['store_nl']);
 unset($data[$k]['id_nl']);
 unset($data[$k]['title_nl']);
 unset($data[$k]['price_nl']);
 }
}

Array after:

Array 
( [0] => Array ( 
 [Store] => Store One 
 [ItemID] => 123456 
 [Description] => Product One 
 [Price] => 9ドル.00 ) 
 [1] => Array ( 
 [Store] => 
 [ItemID] => 
 [Description] => 
 [Price] => ) 
) 

All of the array keys have been changed, but some of the data is now gone? Can someone please tell me a better way to do this without data loss?

asked Jan 10, 2014 at 0:30
1
  • "rename all of my array keys using a foreach loop and unset." --- just create a complretely new array instead Commented Jan 10, 2014 at 0:44

4 Answers 4

4

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.

answered Jan 10, 2014 at 0:44

2 Comments

You didn't look carefully at the OP's input array. The keys in the first sub-array end in _nl and the keys in the second sub-array end in _ds.
It shouldn't matter, he wants all of the keys in the initial array(s) to be changed to same keys.
0

Use array_flip() to flip between the keys and values, then it'll be easy to run over the values and "flip" the array again (back to its original state). It should be something like:

$tmp_arr = array_flip($arr);
$fixed_arr = array();
foreach($tmp_arr as $k => $v){
 if($val == "store_nl"){
 $fixed_arr[$k] = "Store";
 }
 // etc...
}
// and now flip back:
$arr = array_flip($fixed_arr);
answered Jan 10, 2014 at 0:41

Comments

0

The arrays should have the same number of elements, but I think that's the case here.

$data = [[
 'store_nl' => 'Store One',
 'id_nl' => 123456,
 'title_nl' => 'Product One',
 'price_nl' => '9ドル.00'
], [
 'store_ds' => 'Store Two',
 'id_ds' => 789012,
 'title_ds' => 'Product Two',
 'price_ds' => '8ドル.00'
]];
$keys = ['Store', 'ItemID', 'Description', 'Price'];
$data = [
 'nl' => array_combine($keys, $data[0]),
 'ds' => array_combine($keys, $data[1])
];
answered Jan 10, 2014 at 0:53

Comments

0

Recursive php rename keys function:

function replaceKeys($oldKey, $newKey, array $input){
 $return = array();
 foreach ($input as $key => $value) {
 if ($key===$oldKey)
 $key = $newKey;
 if (is_array($value))
 $value = replaceKeys( $oldKey, $newKey, $value);
 $return[$key] = $value;
 }
 return $return;
}
answered Apr 14, 2015 at 4:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.