0

I have php result set, and from these result i am extracting value using php foreach loop. I put the foreach loop value in array $summery[]. but when i try to print value its print value at once. but i need separate value/result set for each foreach loop as json code so that i can print each result separately. My foreach loop following :

foreach($result_UserWrSet as $UserWrInfo) {
        $summery[]=$UserWrInfo['wr_id'];
        $summery[]=$UserWrInfo['wr_number'];
        $summery[]=$UserWrInfo['wr_title'];
        $dateFlag=1;
        $result_StartDate = $WrDates ->getDateById($UserWrInfo['date_id'],$dateFlag);
        $result_EndDate = $WrDates ->getDateById($UserWrInfo['date_id'],$dateFlag);
        $summery[]=$result_StartDate;
        $sql_GetUserName = "SELECT user_name FROM user_information where user_id='$UserWrInfo[user_id]'";
        $result_GetUserName = mysqli_query($conn, $sql_GetUserName);
        $num_GetUserName = mysqli_num_rows($result_GetUserName);
        if ($num_GetUserName > 0){
            $UserNameByIdRos = $result_GetUserName->fetch_assoc();
            $UserNameById=$UserNameByIdRos['user_name'];
        }
        else {$UserNameById=NULL;}
        $summery[]=$UserNameById;
        $result_CurrentHop = $WrDates ->getCurrentHopByWrId($UserWrInfo['wr_id']);
        $result_CurrentHopName = $WrDates ->GetHopsNameById($result_CurrentHop);
        $summery[]=$result_CurrentHopName;
        $result_EndDate = $WrDates ->completedDate($UserWrInfo['wr_id']);
        $summery[]=$result_EndDate;
    }
print json_encode($summery);

My result become

["69","010116-69","Wr test","01\/01\/16 18:45 PM","planner","Done","01\/01\/16 19:16 PM","68","010116-","This is title","01\/01\/16 18:44 PM","planner","Done"]

but i need :

[["69","010116-69","Wr test","01\/01\/16 18:45 PM","planner","Done"],["01\/01\/16 19:16 PM","68","010116-","This is title","01\/01\/16 18:44 PM","planner","Done"]]

3 Answers 3

1

Use this code, you need to use another array in which all the sub array's to be pushed and encode that array after pushing all the items into it

<?php 
$dataArray = array(); /// empty array in which sub array's to be pushed..
foreach($result_UserWrSet as $UserWrInfo) {
        $summery= array();
        $summery[]=$UserWrInfo['wr_id'];
        $summery[]=$UserWrInfo['wr_number'];
        $summery[]=$UserWrInfo['wr_title'];
        $dateFlag=1;
        $result_StartDate = $WrDates ->getDateById($UserWrInfo['date_id'],$dateFlag);
        $result_EndDate = $WrDates ->getDateById($UserWrInfo['date_id'],$dateFlag);
        $summery[]=$result_StartDate;
        $sql_GetUserName = "SELECT user_name FROM user_information where user_id='$UserWrInfo[user_id]'";
        $result_GetUserName = mysqli_query($conn, $sql_GetUserName);
        $num_GetUserName = mysqli_num_rows($result_GetUserName);
        if ($num_GetUserName > 0){
            $UserNameByIdRos = $result_GetUserName->fetch_assoc();
            $UserNameById=$UserNameByIdRos['user_name'];
        }
        else {$UserNameById=NULL;}
        $summery[]=$UserNameById;
        $result_CurrentHop = $WrDates ->getCurrentHopByWrId($UserWrInfo['wr_id']);
        $result_CurrentHopName = $WrDates ->GetHopsNameById($result_CurrentHop);
        $summery[]=$result_CurrentHopName;
        $result_EndDate = $WrDates ->completedDate($UserWrInfo['wr_id']);
        $summery[]=$result_EndDate;
        ////Push sub array i.e summary into the main array...
        $dataArray[] = $summery;
}
print json_encode($dataArray);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You need a multidimensional array, you can follow below code:

$result_UserWrSet = array(
 '0' => array('wr_id'=>'12','wr_number' =>'785', 'wr_title' => 'title1'),
 '1' => array('wr_id'=>'12','wr_number' =>'785', 'wr_title' => 'title1'));

foreach($result_UserWrSet as $key => $UserWrInfo) {
    $summery[$key][]=$UserWrInfo['wr_id'];
    $summery[$key][]=$UserWrInfo['wr_number'];
    $summery[$key][]=$UserWrInfo['wr_title'];
}

print json_encode($summery);
output: [["12","785","title1"],["12","785","title1"]]

Good Luck :)

Comments

0

Currently, you are just adding new items to a one dimensional array. You need to create a separate array per result, like this:

foreach($result_UserWrSet as $UserWrInfo) {
    $item = array();

    // Change all $summary in the loop to: $item, like this:
    $item[]=$UserWrInfo['wr_id'];
    $item[]=$UserWrInfo['wr_number'];
    $item[]=$UserWrInfo['wr_title'];

    //...and so on

    // Then add this last in your loop:
    $summary[] = $item;
}

This will create one array per iteration that is put in the main array, which then becomes a multi dimensional array.

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.