bland_dan
bland_dan

Reputation: 446

Creating new array from two existing arrays

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.

Upvotes: 0

Views: 366

Answers (3)

Pascal MARTIN
Pascal MARTIN

Reputation: 400972

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

Upvotes: 0

Jase
Jase

Reputation: 599

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']); }

Upvotes: 0

SergeS
SergeS

Reputation: 11779

In your case -

$new = array( );
foreach( $crc as $k => $v ) {
  $new[$k] = array( $v, $crc_id["{$k}_id"] );
}

Upvotes: 1

Related Questions