0

Here i like to explain my problem clearly.

id  company ID  Employee ID Name        Relationship    Dob     Age Gender       
1   EMPL        00001       Choodamani  Spouse      11-Aug-66   49  Female            
2   EMPL        00001       Komala      Mother      30-Oct-39   76  Female            
3   EMPL        00001       Varshini    Daughter    29-Apr-04   11  Female            
4   EMPL        00001       Vasudevan   Employee    15-Jul-62   53  Male    
5   EMPL        00002       Siddharth   Son         1-Jun-00    15  Male              
6   EMPL        00002       Poongavanam Mother      21-Oct-39   76  Female            
7   EMPL        00002       Aruna       Spouse      16-Sep-68   47  Female            
8   EMPL        00002       Abirami     Daughter    7-May-97    18  Female            
9   EMPL        00002       Murali      Employee    7-Oct-67    48  Male    

Here you can see a table, this table i have took from my db. In this data you can see that employee_id is same for first four row but id different for each row. Here what i need to merge same employee_id in one array.

example: all 00001 employee_id in one array

1
  • 2
    have you tried anything ? Commented Jun 9, 2015 at 7:52

3 Answers 3

1

Try this:

<?php 
  $new_sort = array();
  foreach ($db_array as $db_row){
    $key_row = $db_row["Employee_ID"];
    unset($db_row["Employee_ID"]);
    $new_sort[$key_row][] = $db_row;
  }
?>

on output you will have nested array, where $new_sort[some_employee_id] = your data

p.s if you want to create new array where name of array must be $employee_id, for example: $0001, it's impossible, because variables name must begin with a letter

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

Comments

0

I hope this will help you

_form.php

<?php

class Claim
 {
      public function Arraycombine($keys, $values)
      {
         $result = array();
         foreach ($keys as $i => $k) {
           $result[$k][] = $values[$i];
         }
         array_walk($result, create_function('&$v', '$v = (count($v) == 1)?        array_pop($v): $v;'));
         return    $result;
         } 

    }

    $data1 = ArrayHelper::map(Employee::find()->all(), 'id', 'employee_id');
    $data2 = ArrayHelper::map(Employee::find()->all(), 'id', 'name');

    $claim = new Claim();

    $claimers = $claim->Arraycombine( $data1, $data2 );

    echo "<pre>"; print_r($claimers);exit(); echo "</pre>";

?>

ouput:

enter image description here

Comments

0
// Assuming your data is available in an associative array ($_data):
$employees = array();
foreach($_data as $employee) {
    $id = $employee['Employee ID'];
    if(isset($employees[$id])) {
        $employees[$id][] = $employee;
    } else {
        $employees[$id] = array($employee);
    }
}

// The results are now grouped, so:
$employees['00001'] = array(
    array(
        'Name' => 'Choodamani',
        // ...
    ),
    array(
        'Name' => 'Komala',
        // ...
    )
);

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.