Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have merged an 2 multidimensional arrays with the same info but with different keys using array_merge() and i get the output

    Array
   (
    [0] => Array
    (
        [Ttitle] => lilly
        [Price] => 1.75
        [Number] => 3
    )

   [1] => Array
    (
        [Title] => rose
        [Price] => 1.25
        [Number] => 15
    )

    [2] => Array
    (
        [Title] => daisy
        [Price] => 0.75
        [Number] => 25
    )

    [3] => Array
    (
        [Title] => nettle
        [Price] => 2.75
        [Number] => 33
    )

    [4] => Array
    (
        [Title] => orchid
        [Price] => 1.15
        [Number] => 7
    )

  )

As you can see the first key name in the array is Ttitle and the rest is Title. What I want now is to change all Ttitle keys (just the one at the mo) to Title (like all the others).When I the use array_map() function I manage to change all the keys to Title but i also delete all Title values except for the Ttitle key i just changed to Title.

my array_map() code is as folows

   if(! empty($notmatchingarray))
   {
    $completearray = array_merge($notmatchingarray, $datastuff2);

    $completearray = array_map(function($complete) {
        return array(
           'Title' => $complete['Ttitle'],
           'Price' => $complete['Price'],
           'Number' => $complete['Number'],
       );
}, $completearray);

echo "Array which includes each unique enrty from both arrays";
echo "<pre>";
print_r($completearray);
echo "</pre>";
echo "<br/>";
   }

the out put of this code is

    Array
  (
  [0] => Array
    (
        [Title] => lilly
        [Price] => 1.75
        [Number] => 3
    )

  [1] => Array
    (
        [Title] => 
        [Price] => 1.25
        [Number] => 15
    )

  [2] => Array
    (
        [Title] => 
        [Price] => 0.75
        [Number] => 25
    )

  [3] => Array
    (
        [Title] => 
        [Price] => 2.75
        [Number] => 33
    )

  [4] => Array
    (
        [Title] => 
        [Price] => 1.15
        [Number] => 7
    )

   )

Am i doing the wrong thing in using this array_map() function? Should i be doing something else other than that?

regards Mike

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Better ways but I'm headed out. With your existing code you need to set Ttitle or Title whichever is present:

$completearray = array_map(function($complete) {
        $title = isset($complete['Ttitle']) ? $complete['Ttitle'] : $complete['Title'];
        return array(
           'Title' => $title,
           'Price' => $complete['Price'],
           'Number' => $complete['Number'],
       );
}, $completearray);

Ternary operator Is a shortcut for a simple if / else.

share|improve this answer
    
Thanks again for your help, works straight away. Does that $title variable basically say if the Key in $complete is Ttitle then swap $complete['Ttitle'] with $complete['Title']. And if i had all the keys with different names but similar values could i then use the same principle to change all the keys? –  Mike Mar 12 at 21:48
    
@Mike No, it doesn't swap the values. ?: is the ternary operator (also called the conditional operator). It's basically a compact version of an if/else statement. –  p.s.w.g Mar 12 at 22:02

You might try using the array_key_exists function:

$completearray = array_map(function($complete) {
    return array(
       'Title' => (array_key_exists('Title', $complete) 
                   ? $complete['Title'] 
                   : $complete['Ttitle']),
       'Price' => $complete['Price'],
       'Number' => $complete['Number'],
   );
}, $completearray);

Or if you prefer:

$completearray = array_map(function($complete) {
    $titleKey = array_key_exists('Title', $complete) ? 'Title' : 'Ttitle';
    return array(
       'Title' => $complete[$titleKey],
       'Price' => $complete['Price'],
       'Number' => $complete['Number'],
   );
}, $completearray);

If the array $complete contains the key, 'Title', it will use that; otherwise it will use the key 'Ttitle'.

share|improve this answer
    
Thank you for your quick response, though it doesn't seem to work. –  Mike Mar 12 at 21:41
    
@Mike What about it didn't work? Did you get an error? I had a problem in my initial answer (in_array instead of array_key_exists), but I have since corrected it. Please see my updated answer. –  p.s.w.g Mar 12 at 21:44
    
Ah it works now i must of jumped the gun. Great help thank you! Could i in theory expand on this to change all keys to a particular name if the multidimensional array had a lot more different keynames.? –  Mike Mar 12 at 21:55
    
@Mike Sure. You could tweak the second example pretty easily. You could even create an array of keys and just pick the first one that was found. whichever way you want to do it, Find the correct key and assign it to $titleKey. Your return statement would stay the same. –  p.s.w.g Mar 12 at 21:59
    
Thanks for your help, i'm slowly getting it. ill start tweaking your code and see what happens. great answers! –  Mike Mar 12 at 22:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.