0

I need to reconstruct an array. Here is the original array:

array(8) {
      [0] => array(1)
      {
            ["L_TRANSACTIONID0"] => string(17) "62M97388AY676841D"
      }

      [1] => array(1)
      {
            ["L_TRANSACTIONID1"] => string(17) "9FF44950UY3240528"
      }

      [2] => array(1)
      {
            ["L_STATUS0"] => string(9) "Completed"
      }

      [3] => array(1)
      {
            ["L_STATUS1"] => string(9) "Completed"
      }
}

I would like to reconstruct it to be as such:

array(2) {
      [0] => array(2)
      {
            ["L_TRANSACTIONID0"] => string(17) "62M97388AY676841D"
            ["L_STATUS0"] => string(9) "Completed"
      }
      [1] => array(1)
      {
            ["L_TRANSACTIONID1"] => string(17) "9FF44950UY3240528"
            ["L_STATUS1"] => string(9) "Completed"
      }
}

Notice that the KEYS both match with the numeric representation... Is this at all possible?

edit:

here is my code I am using:

        foreach($comparison as $key => $val) {
        $findme1 = 'L_TRANSACTID'.$i++;
        $findme2 = 'L_STATUS'.$c++;
        $arrDisable = array($findme1,$findme2);
        if( in_array($key, $arrDisable ) ) {
          unset( $comparison[ $key ][$val]);
        }
            if( in_array($key, $arrDisable) ) {
        unset( $comparison[ $key ][$val]);
        }
      }
3
  • Yes. But you will need to write your own code to do this. That is to say, there is no single, magic PHP function. Commented Nov 8, 2012 at 16:27
  • Sure, it's possible. What have you tried so far? We're not here to do your job for you, so show what you've attempted. Commented Nov 8, 2012 at 16:27
  • added code I am trying to use. Commented Nov 8, 2012 at 16:31

2 Answers 2

1

Try this

$labels = array('L_TRANSACTIONID', 'L_STATUS');
$res = array();
foreach($arr as $val) {
  $key = str_replace($labels, '', key($val));
  $res[$key] = isset($res[$key]) ? array_merge($res[$key], $val) : $val;
}
print_r($res);

http://codepad.org/MwqTPqtA

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

Comments

0

If you are certain the the vector cointains pairs L_TRANSACTIONIDn / L_STATUSn keys,that is to say, for each transactionID, there is a corresponding status, what you can do, is to get the number of id/status records (which should equal the length of the initial array, divided by two), and compose the resultin keys, by increasing the current element count.

Could look something like this:

$numItems = sizeof($myInitialArray) / 2;
$newArray = array();

for($i = 0; $i < $numItems; $i++)
{
    $itemID = $i * 2; // since we're getting id/status pairs, we're using a step equal to 2

    $newArray[] = array(
        ("L_TRANSACTIONID" . $i) => $myInitialArray[$itemID], // this is the id value
        ("L_STATUS" . $i) => $myInitialArray[$itemID + 1] // this is the status for that id
    );
}

Hope this helps. Have a great day!

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.