0

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
  • 1
    Could 'Country' contain values other than 'Jpn'? Commented Aug 20, 2010 at 17:16
  • Yes, 'Country' can be any string, but when updating, all will be changed to only one value Commented Aug 20, 2010 at 17:26

5 Answers 5

3
$newArray = array();
foreach($array as $ar){
    $newArray[] = array(
       'Brand' => $ar['Branch'],
       'Country' => 'Japan',
       'id' => $ar['id']
    )
}
$array = $newArray;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for showing me just how stupid I am. "It's simple: Don't change the key, just create a new array and replace the old one."
1
foreach($array as &$item) {
    $branch = $item['Branch'];
    array_unshift($item, array("Brand"=>$branch));
    unset($item['Branch']);
    $item['Country'] = 'Japan'; 
}

Haven't tested it..

1 Comment

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!
0

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.

Comments

0

TRY:

<?php
foreach($array as $key => $data)
{
$data['Brand'] = $data['Branch'];
$data['Country'] = 'Japan';
unset($data['Branch']);
$array[$key] = $data;
}
?>

6 Comments

That'll put the Brand key at the end of the array.
So what? Keys in associative arrays should have no explicit order.
This will also update every 'Country' element to null, or possibly the empty string. $data['Japan'] does not exist.
hmm, that will.. But that shouldn't really matter in case of associative arrays.
@haydenmuhl: That was a typo.
|
0
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 ) )

Comments

Your Answer

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.