2

i have 2 arrays and i want 2 create 2D array for create mysql record

      Array
      (
           [0] => a
           [1] => b
           [2] => c
       )
       Array
       (
           [0] => 1
           [1] => 2
           [2] => 3
         )

i want 2 merge them into 2 dimensional array like this

      Array
(
[0] => Array
    (
        [designation_id] => 1
        [judge_name] => a
    )

[1] => Array
    (
        [designation_id] => 2
        [judge_name] => b
    )

[2] => Array
    (
        [designation_id] => 3
        [judge_name] => c
    )

 )

i use array_merge_recursive and it generates result like this

  Array
 (
     [0] => a
     [1] => b
     [2] => c
     [3] => 1
     [4] => 2
     [5] => 3
  )
2
  • If you got what you need from any of the answers (or not) you should comment (and maybe accept one) and let people know. The same goes for your previous questions. Commented Nov 26, 2012 at 13:30
  • Drat, my answer was too late into the game =/ Commented Nov 26, 2012 at 14:52

7 Answers 7

3

Assuming there will always be the same amount of values in $array1 as there are in $array2..

$array1 = Array("a","b","c");
$array2 = Array(1,2,3);
$newArray = Array();

foreach($array1 as $key => $arr1Val){
    $newArray[$key]['designation_id'] = $array2[$key];
    $newArray[$key]['judge_name'] = $array1[$key];
}

Of course, you will have to alter $array1 and $array2 to your needs, but you understand the basic idea. Check it here.

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

Comments

2

Assuming $array1 is the judge_name and $array2 is the designation_id

$newArray = array();

for($i=0; $i<count($array1); $i++)
{
    $newArray[] = array(
        'designation_id' => $array2[$i],
        'judge_name' => $array1[$i]
    );
}

Codepad Demo

Outputs

Array
(
    [0] => Array
        (
            [designation_id] => 1
            [judge_name] => a
        )

    [1] => Array
        (
            [designation_id] => 2
            [judge_name] => b
        )

    [2] => Array
        (
            [designation_id] => 3
            [judge_name] => c
        )

)

Comments

1

simple as hell

$array1 = array('a', 'b', 'c');
$array2 = array(1,2,3);

$merged = array();
foreach($array1 as $key => $value)
{
    $merged[$key] = array(
        'judge_name' => $value,
        'designation_id' => array_key_exists($key, $array2) ? $array2[$key] : null
    );
}

Comments

1

Assuming that both arrays are of same size

$length = count($array1);
$finalArray = array();

for ($i = 0; $i < $length; $i++) {
    $temp = array();
    $temp['designation_id']  = $array1[$i];
    $temp['judge_name']  = $array2[$i];
    $finalArray[$i] = $temp;
}

Comments

1
$a = array('a', 'b', 'c');
$b = array(1,2,3);
$output = array_map(function($i, $j){
    return array(
        'judge_name'     => $j,
        'designation_id' => $i
    );
}, $a, $b);

var_dump($output);

Outputs

array(3) {
  [0]=>
  array(2) {
    ["judge_name"]=>
    int(1)
    ["designation_id"]=>
    string(1) "a"
  }
  [1]=>
  array(2) {
    ["judge_name"]=>
    int(2)
    ["designation_id"]=>
    string(1) "b"
  }
  [2]=>
  array(2) {
    ["judge_name"]=>
    int(3)
    ["designation_id"]=>
    string(1) "c"
  }
}

1 Comment

Try changing the closure to return an array item instead and use the output of array_map() directly to populate $output.
1

If you have PHP >= 5.3 you could use MultipleIterator for that purpose:

$designations = array(1, 2, 3);
$judges = array('a', 'b', 'c');

$mt = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mt->attachIterator(new ArrayIterator($designations), "designation_id");
$mt->attachIterator(new ArrayIterator($judges), "judge_name");

$final = iterator_to_array($mt, false);
print_r($final);

Demo

It iterates over multiple arrays, taking a value from each array at every iterator; you can assign a key for each array that will be used to form a single array item.

Afterwards you convert the results into an array using iterator_to_array().

Comments

0
$new1=array("a","b","c");
$new2=array("1","2","3");
$req=array();
$d=0;
foreach($new1 as $value1)
{
    foreach($new2 as $value2)
    {
        $req[$d]["designation_id"]=$value1;
        $req[$d]["judge_name"]=$value2;
        $d++;
    }
}

echo "<pre>";
print_r($req);

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.