tags:

views:

52

answers:

5

I have the array:

$array = Array(
      [0] => Array(
                  [Branch] => 'Toyota',
                  [Country] => 'Jpn',
                  [id] => 'jp01'
      )
      [1] => Array(
                  [Branch] => 'Nissan',
                  [Country] => 'Jpn',
                  [id] => 'jp05'
      )
      [2] => Array(
                  [Branch] => 'Honda',
                  [Country] => 'Jpn',
                  [id] => 'jp20'
      ) )

What I want to do is:
1 - Change the key Branch to Brand, but without moving it to the end or the array.
2 - Update all the values to the key Country, changing Jpn to Japan

The result should be like this:

$array = Array(
      [0] => Array(
                  [Brand] => 'Toyota',
                  [Country] => 'Japan',
                  [id] => 'jp01'
      )
      [1] => Array(
                  [Brand] => 'Nissan',
                  [Country] => 'Japan',
                  [id] => 'jp05'
      )
      [2] => Array(
                  [Brand] => 'Honda',
                  [Country] => 'Japan',
                  [id] => 'jp20'
      ) )

I really appreciate your help.

+2  A: 
$newArray = array();
foreach($array as $ar){
    $newArray[] = array(
       'Brand' => $ar['Branch'],
       'Country' => 'Japan',
       'id' => $ar['id']
    )
}
$array = $newArray;
Kau-Boy
A: 

TRY:

<?php
foreach($array as $key => $data)
{
$data['Brand'] = $data['Branch'];
$data['Country'] = 'Japan';
unset($data['Branch']);
$array[$key] = $data;
}
?>
Aman Kumar Jain
That'll put the `Brand` key at the end of the array.
Daniel Vandersluis
So what? Keys in associative arrays should have no explicit order.
Aillyn
This will also update every 'Country' element to null, or possibly the empty string. $data['Japan'] does not exist.
haydenmuhl
hmm, that will.. But that shouldn't really matter in case of associative arrays.
Aman Kumar Jain
@haydenmuhl: That was a typo.
Aman Kumar Jain
In PHP, hashtables (which are arrays) are ordered. You never lose the order of an array/hashtable in PHP. It's the only language I'm aware of that works this way, and implementation experts will bash PHP for it, because it's really not a good idea from a performance perspective... except PHP is still ridiculously fast so the standard attitude is to simply not care. ;)And lots of projects make use of this feature of PHP so they can't really change it now without problems.
Helgi Hrafn Gunnarsson
A: 

This should do it.

<?php
foreach($array as $k => $v) {
  $array[$k]['Brand'] = $array[$k]['Branch'];
  unset($array[$k]['Branch'];
  if($array[$k]['Country'] == 'Jpn') {
    $array[$k]['Country'] = 'Japan';
  }
}
?>

There are probably clever array functions that could do it, too, but this will get the job done.

haydenmuhl
+1  A: 
foreach($array as &$item) {
    $branch = $item['Branch'];
    array_unshift($item, array("Brand"=>$branch));
    unset($item['Branch']);
    $item['Country'] = 'Japan'; 
}

Haven't tested it..

Leon
If it works, that is a cool solution. Especially using an ampersand for the items so you can change the array without the need of the key. Very clever!
Kau-Boy
A: 
foreach($array as $key => $subArray){
    foreach($subArray as $subkey => $value)
        if($value === 'Jpn') $subArray[$subkey] = 'Japan';
    $array[$key] = $subArray;
}


foreach($array as $key => $subArray){
    $subArray = array_flip($subArray);
    foreach($subArray as $subkey => $value)
        if($value === 'Branch') $subArray[$subkey] = 'Brand';
    $subArray = array_flip($subArray);
    $array[$key] = $subArray;
}

Outputs:

Array ( [0] => Array ( [Brand] => Toyota [Country] => Japan [id] => jp01 ) [1] => Array ( [Brand] => Nissan [Country] => Japan [id] => jp05 ) [2] => Array ( [Brand] => Honda [Country] => Japan [id] => jp20 ) )
Ben
Works like a charm! Thank you!
Derick
You're welcome.
Ben