0

I would like to replace all numerical keys in the multidimensional array $aValues with a textual equivalent stored in $aKeyNames.

$aKeyNames = array(0 => 'foo', 1 => 'bar');
$aValues = array( 
    0 => array( 0 => 'content relating to foo', 1 => 'content relating to bar' ), 
    1 => array( 0 => 'content relating to foo', 1 => 'content relating to bar') 
);

The desired output;

array (size=2)
  0 => 
    array (size=2)
      'foo' => string 'content relating to foo' (length=23)
      'bar' => string 'content relating to bar' (length=23)
  1 => 
    array (size=2)
      'foo' => string 'content relating to foo' (length=23)
      'bar' => string 'content relating to bar' (length=23)

To achieve this I've written the following working code;

foreach ($aValues as $iValuePos => $aValue) {
    foreach ($aValue as $iKey => $sTempValue){
        $aValues[$iValuePos][ $aKeyNames[$iKey] ] = $sTempValue;
        unset($aValues[$iValuePos][$iKey]);
    }
}

My concern is that $aValues is very large. Is there a more efficient way to achieve this?

Please note this question is different to the one provided as a duplicate due to the use of a multidimensional array.

2
  • Possible duplicate of Update all array keys using value from another array Commented Jun 28, 2016 at 14:44
  • This question is clearly different to that one (being my last question). As suggested on that thread I have created a new one! Thanks for not taking the time to read and marking as a duplicate... Commented Jun 28, 2016 at 14:53

2 Answers 2

1

I haven't bench-marked but it should be faster:

$aValues = array_map(function($v) use ($aKeyNames) {
                         return array_combine($aKeyNames, $v);
                     }, $aValues);

Another alternative using a reference & to the value:

foreach($aValues as &$v) {
    $v = array_combine($aKeyNames, $v);
}
Sign up to request clarification or add additional context in comments.

6 Comments

looks good, going to benchmark with the other answer and will post back
getting an error back off this; array_combine(): Both parameters should have an equal number of elements
Which they do in your question. Maybe your real arrays are not what you show?
yeah spot on. This answer came out slightly faster than the one below. Thank you
It's about which is better not faster. Mine was actually slightly later than the other one.
|
1

Try this:

$aKeyNames = array(0 => 'foo', 1 => 'bar');

$aValues = array( 
    0 => array( 0 => 'content relating to foo', 1 => 'content relating to bar' ), 
    1 => array( 0 => 'content relating to foo', 1 => 'content relating to bar') 
);

foreach ($aValues as $iValuePos => $aValue) {
    $aValues[$iValuePos] = array_combine($aKeyNames, $aValue);
}

1 Comment

thats great thank you! Will run a quick test using the data set and post back timings

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.