2

I am querying sql and response feedback is as follows in an array format stored in $result_rows,

print_r($result_rows) - shows the following 2D array

Array
(
    [0] => Array
        (
            [employee_zipcode]   => 70062
            [employee_name]      => Carter Jr
            [employee_address]   => 472 Shadowmar Drive Kenner, LA 
            [employee_id_series] => 6144

        )

    [1] => Array
        (
            [employee_zipcode]  =>  70062
            [site_name]          => Carter Sr
            [address]            => 472 Shadowmar Drive Kenner, LA 
            [employee_id_series] => 6144
        )

    [2] => Array
        (
            [employee_zipcode]  => 29210
            [site_name]         => Claude 
            [address]           => 2308 Wexford Way Columbia, SC 
            [employee_id_series]=> 6144

        )


)

The above dynamic result is obtained when I query employee_id_series by 6144

I am confused on how to sort down the array and print it to a html table in this format

Desired Result:

Emp                 Emp     Emp
Zipcode_Series      Name    Address

70062_6144
70062_6144          Carter  472 Shadowmar Drive Kenner, LA 

29210_6144          Claude  2308 Wexford Way Columbia, SC 

NOTE: sorting_joining is done based on employee_zipcode value,

whose same value(70062) is cross checked through out the array.

4
  • Have you tried something ? Commented Jun 29, 2015 at 23:34
  • Tried using foreach,while...few but my scenario isn't working ,totally blown brains trying few ways,everything failed. Commented Jun 29, 2015 at 23:37
  • did you try adding your query group by employee_zipcode ? Commented Jun 30, 2015 at 0:15
  • Dont want to sort in sql ,php array need to compare those 70062 with input query 6144 and dump those matches in a single table row Commented Jun 30, 2015 at 0:16

2 Answers 2

1

Once you use usort that @Crayon mentioned. You could use another associative array to print the result that you want. It's a rough version. Hope it help.

usort($result_rows, 'cmp');
function cmp($row1, $row2) {
    if ($row1['zipcode'] == $row2['zipcode']) {
        return 0;
    }
    return ($row1['zipcode'] > $row2['zipcode']) ? -1 : 1;
}

//Create the printing_results as an associative array to keep the same zipcode together and increase the count.
$printing_results = array();
foreach ($result_rows as $result) {
    $zipcode = $result['zipcode'];
    $result['cnt'] = 1;
    if (!isset($printing_results[$zipcode])) {
        $printing_results[$result['zipcode']] = $result;
    } else {
        $printing_results[$result['zipcode']]['cnt'] = $printing_results[$result['zipcode']]['cnt'] + 1;
    }
}

//For each associative array, print zipcode for other records and print all details on the last record.
foreach ($printing_results as $key => $printing_result) {
    $cnt = $printing_result['cnt'];
    for ($i=0; $i < $cnt; $i++) {
        echo "\n".$printing_result['zipcode'];
    }
    print "\t".$printing_result['name']."\t".$printing_result['address'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the code,excellent way to go , but there is a fundamental issue, row s in realtime sql response are not fixed 1 or 2 or 10 so it need to compare for all the rows not just between $row1 and $row2 , can u suggest a genric instead ?
The compare function compares any 2 items in array, not just row1 and row2. I might have not be clear about using the variable names. You could change them to function cmp($a, $b)
Here is the updated one. tutorialspoint.com/… and the reference for usort function php.net/manual/en/function.usort.php
0

You can use usort to custom sort by the 2nd level array element:

usort($result_rows, function($a, $b)
{
    if ($a['employee_zipcode'] == $b['employee_zipcode'])
        return 0;
    else if ($a['employee_zipcode'] > $b['employee_zipcode'])
        return -1;
    else 
        return 1;
});

from there.. i'm not sure why you really want/need to merge employee_zipcode and employee_id_series.. seems better that you should leave them separate and just concat in presentation but I assume that's something trivial for you to do if you really want that..

Dont want to sort in sql ,php array need to compare those 70062 with input query 6144 and dump those matches in a single table row

Is there any particular reason you need to show multiple 70062_6144 in a single row like that? If not, you can just array_unique the sorted array.

1 Comment

adding to the above code - goo.gl/v0Uq8g can u suggest how to solve the logic if the row count is unknown , a genric way for X no of rows

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.