0

I currently have two arrays:

$crc = Array([crc_01b]=>Blah blah blah[crc_02b]=>Blah blah[crc_03b]=>Testing);
$crc_id = Array([crc_01b_id]=>1[crc_02b_id]=>23[crc_02b_id]=>28);

I need to create a new array:

$new = Array(
         [crc_01b]=>(Blah blah blah,1),
         [crc_02b]=>(Blah blah,23),
         [crc_03b]=>(Testing,28)
       );

But I'm not sure how to do it.

1
  • Yup, foreach is probably the way to go, I feel stupid now as use foreach all the time and just didn't think this one through. Commented Mar 30, 2011 at 21:10

3 Answers 3

1

In your case -

$new = array( );
foreach( $crc as $k => $v ) {
  $new[$k] = array( $v, $crc_id["{$k}_id"] );
}
Sign up to request clarification or add additional context in comments.

Comments

0

i'd use a foreach in this instance to build the new array

$new = array(); foreach ($crc as $key => $value){ $new[$key] = array($crc[$key], $crc_id[$key.'_id']); }

1 Comment

oops, mistake, replace $crc[$key] with $value
0

What about something like this :

$a = array(
    'crc_01b' => 'Blah blah blah', 
    'crc_02b' => 'Blah blah', 
    'crc_03b' => 'Testing', 
);

$b = array(
    'crc_01b_id' => 1, 
    'crc_02b_id' => 23, 
    'crc_03b_id' => 28,   // I suppose the key is not crc_02b_id here ?
);

$new = array();
foreach ($a as $key => $value) {
    $new[$key] = array(
        $value, 
        $b[$key . '_id']
    );
}
var_dump($new);

Which would get you :

array
  'crc_01b' => 
    array
      0 => string 'Blah blah blah' (length=14)
      1 => int 1
  'crc_02b' => 
    array
      0 => string 'Blah blah' (length=9)
      1 => int 23
  'crc_03b' => 
    array
      0 => string 'Testing' (length=7)
      1 => int 28

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.