0

I am needing to rename dynamic array keys, and create a new array. Here is the array as given:

array(21)
{
    ["0161"] =>
    array(5)
    {
        ["L_NAME0161"] =>
        string(13) "john%20Hewett"
        ["L_TRANSACTIONID0161"] =>
        string(17) "50350073XN1446019"
        ["L_AMT0161"] =>
        string(6) "8%2e50"
        ["L_FEEAMT0161"] =>
        string(9) "%2d0%2e55"
        ["L_NETAMT0161"] =>
        string(6) "7%2e95"
    }
    ["08591"] =>
    array(5)
    {
        ["L_NAME08591"] =>
        string(18) "Norbert%20Bendixen"
        ["L_TRANSACTIONID08591"] =>
        string(17) "1WN98871MS4263823"
        ["L_AMT08591"] =>
        string(6) "8%2e50"
        ["L_FEEAMT08591"] =>
        string(9) "%2d0%2e55"
        ["L_NETAMT08591"] =>
        string(6) "7%2e95"
    }
}

Here is the code I am using which is not working for me:

foreach ($reb AS $newrebarray)
{
    foreach ($newrebarray as $ke => $val)
    {

        if (preg_match("/L_NETAMT/i", $ke))
        {
            $newarrayreb1 = array('Net' => $val);
        }
        if (preg_match("/L_TRANSACTIONID/i", $ke))
        {
            $newarrayreb1 = array('TransactID' => $val);
        }
        if (preg_match("/L_NAME/i", $ke))
        {
            $newarrayreb1 = array('Name' => $val);
        }
    }
}

notice that the array keys are dynamic, I want to create a new array with static keys, and the associated values. When I run the code, I get five different arrays.

2
  • so basically you just want to strip the numbers off those keys, leaving only the initial text-only portion, and build a new array with those stripped keys? Commented Nov 15, 2012 at 14:33
  • yes Mark, that is what I am wanting to do Commented Nov 15, 2012 at 14:35

2 Answers 2

1

First I would define a function that does replacement based on a captured memory segment of a regular expression:

function do_translate($match)
{
    switch ($match[1]) {
        case 'L_NAME':
            return 'Name';

        case 'L_NETAMT':
            return 'Net';

        case 'L_TRANSACTIONID':
            return 'TransactID';
    }
    // in all other cases, return the full match
    return $match[0];
}

Then iterate over the blocks, send the array keys through a translation pass and then recombine the new keys with the existing values:

foreach ($reb as $id => $data) {
        $new_keys = preg_replace_callback('/^(L_[A-Z]+)' . preg_quote($id) . '$/i', 'do_translate', array_keys($data));
        // create the new array with translated keys
        $reb[$id] = array_combine($new_keys, $data);
}

I noticed that the array keys were a combination of the field and the product id (I guess), so I've used that knowledge to strengthen the regular expression

Sign up to request clarification or add additional context in comments.

Comments

1

Not tested, haven't fully woken up yet, so this'll probably kick your dog and delete all your savegames:

$translations = array(
    'L_TRANSACTIONID' => 'Translation',
    'L_NAME' => 'Name',
    'L_NETAMT' => 'Net'
);

foreach($array as $parentkey => $subarray) {
    foreach($subarray as $subkey => $val) {
       if (preg_match('/^(L_.*?)\d*$/', $matches)) {
           $newKey = $translations[$matches[1]];
           $array[$parentkey][$newkey] = $val;
           unset($array[$parentkey][$subkey]);
       }
    }
}

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.