0
 $data= array();
 foreach($itfdatas as $key=>$itfdata){

$checkemps = explode(',',$itfdata['employee_id']);


  $data[] = array(
            'id'=>mysql_real_escape_string($itfdata['P_id']),
            'job_id'=>mysql_real_escape_string($itfdata['quote_id']),
            'job_title'=>mysql_real_escape_string($itfdata['job_title']));



foreach ($checkemps as $checkempss){
  $empusers =  $users->ShowAllEmployeeByJobId($checkempss['profile_id']);



      $data[] =array(
            'helper_id'=>mysql_real_escape_string($empusers['profile_id']),
            'helper_name'=>mysql_real_escape_string($empusers['name']));




   }

$msg=array('status'=>'success','emp_job_list'=>$data);
$itfresinfo=json_encode($msg);

     }

    echo $itfresinfo;

The above code gives the below output

          {
          "status": "success",
        "emp_job_list": [
        {
        "id": "1",
        "job_id": "MM\/IR1",
        "job_title": "2 Men with 4.5 Tonne Truck"
        },
        {
        "helper_id": "1",
        "helper_name": "Vivek"   
        },
        {
        "helper_id": "3",
        "helper_name": "Akhilesh"
        },
        {
        "id": "2",
        "job_id": "MM\/IR2",
        "job_title": "This is the test asasascvcvcv"
        },
        {
        "helper_id": "1",
        "helper_name": "Vivek"
        },
        {
        "helper_id": "3",
        "helper_name": "Akhilesh"
        }
        ]
        }

But I want the below output

                         {
            "status": "success",   
            "emp_job_list": [ 
            {  
            "id": "1",
            "job_id": "MM\/IR1",
            "job_title": "2 Men with 4.5 Tonne Truck",
            "helper_detail":[  
            {  
            "helper_id": "1",
            "helper_name": "Vivek"  
            }
            ]

            },
            {  
            "id": "1",
            "job_id": "MM\/IR1",
            "job_title": "2 Men with 4.5 Tonne Truck",
            "helper_detail":[  
            {  
            "helper_id": "1",
            "helper_name": "Vivek"  
            }
            ]

            }
            ] 
            }
1

Try it like this. But watch out for typos. I haven't tested it.

If this does not work, add to the question a sample of how $itfdatas looks like and one of what $users->ShowAllEmployeeByJobId($checkempss['profile_id']) returns.

$data= array();
foreach($itfdatas as $key=>$itfdata) {
    $employee = array(
        'id' => mysql_real_escape_string($itfdata['P_id']),
        'job_id' => mysql_real_escape_string($itfdata['quote_id']),
        'job_title' => mysql_real_escape_string($itfdata['job_title'])
        'helper_detail' => array();
    );
    $checkemps = explode(',',$itfdata['employee_id']);
    foreach ($checkemps as $checkempss) {
        $empusers =  $users->ShowAllEmployeeByJobId($checkempss['profile_id']);
        $employee['helper_detail'][] = array(
            'helper_id' => mysql_real_escape_string($empusers['profile_id']),
            'helper_name' => mysql_real_escape_string($empusers['name'])
        );
    }
    $data[] = $employee;
}
$msg = array('status' => 'success','emp_job_list' => $data);
$itfresinfo = json_encode($msg);
echo $itfresinfo;
  • $itfdatas looks like below Array ( [0] => Array ( [id] => 1 [job_id] => MM/IR1 [job_title] => 2 Men with 4.5 Tonne Truck ) [1] => Array ( [id] => 2 [job_id] => MM/IR2 [job_title] => 3 Men with 4.5 Tonne Truck ) ) – Sheenu May 15 '19 at 5:31
  • Thanks for you quick Answer – Sheenu May 15 '19 at 5:48
  • what I have to do for one more child of $employee['helper_detail'][] – Sheenu Nov 1 '19 at 4:38
  • something like this foreach ($checkemps as $checkempss) { $empusers = $users->ShowAllEmployeeByJobId($checkempss['profile_id']); $employee['helper_detail'][] = array( 'helper_id' => mysql_real_escape_string($empusers['profile_id']), 'helper_name' => mysql_real_escape_string($empusers['name']), 'helper_product' => array() ); } – Sheenu Nov 1 '19 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.