0

Updated Question. How can i add additional column in array. I want to add a column distance which is a result from the formula that i have created

Here's my code

    $db = $this->getDbo();
        $query = $db->getQuery(true)
                ->select('*')
                 ->from('#__listing');
     $db->setQuery($query);
      $db->query();

     $rows = $db->loadRowList();


$arr_data = array();
foreach ($rows as $row) {
    $theta = $longitudeFrom - $row->longitude;
    $dist =   sin(deg2rad($latitudeFrom)) 
            * sin(deg2rad($row>latitude)) 
            + cos(deg2rad($latitudeFrom)) 
            * cos(deg2rad($row->latitude)) 
            * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $distance = $miles * 1.609344;
    $key = $row[0];
    $arr_data[$key] = array('id' => $row[0], 'name' => $row[1]);
}

This is what i have tried

$arr_data = array();
foreach ($rows as $row) {
    $theta = $longitudeFrom - $row->longitude;
    $dist =   sin(deg2rad($latitudeFrom)) 
            * sin(deg2rad($row>latitude)) 
            + cos(deg2rad($latitudeFrom)) 
            * cos(deg2rad($row->latitude)) 
            * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $distance = $miles * 1.609344;

    $key = $row[0];
    $arr_data[$key] = array(
                        'id' => $row[0],
                        'name' => $row[1],
                        'distance' => $distance);
}

1 Answer 1

1

Am I correct understanding that you want to get new array which contains aggregated record for each record from $rows? If yes, you can do something like this:

$db = $this->getDbo();
$query = $db->getQuery(true)
            ->select('*')
            ->from('#__listing');
$db->setQuery($query);
$db->query();

$rows = $db->loadAssocList();


$arr_data = [];
foreach ($rows as $rowIndex=>$rowData) {
    $theta = $longitudeFrom - $rowData['longitude'];
    $dist =   sin(deg2rad($latitudeFrom)) 
            * sin(deg2rad($rowData['latitude'])) 
            + cos(deg2rad($latitudeFrom)) 
            * cos(deg2rad($rowData['latitude'])) 
            * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $distance = $miles * 1.609344;

    $arr_data[$rowIndex] = [
        'id' => $rowData['id'],
        'name' => $rowData['name'],
        'distance' => $distance
    ];
}

PS: I'm using a shorthand array notation [], but it is equal to array()

PPS: there is also an issue in the original code with treating $row variable as an object $row->longitude and than as an array $row[1]. Which one is correct depends on how you get $rows, but should not mix it anyway.

PPPS: as per comment bellow i've update my answer using loadAssocList to get associative array and changed

'id' => $rowData[0],
'name' => $rowData[1],

to

'id' => $rowData['id'],
'name' => $rowData['name'],

Assuming that your 'id' and 'name' fields are named respectively in DB.

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

10 Comments

and if i change the $miles to $rowData->id.. is that possible?
how will i get the row ID for instance? like this? $distance = $rowData->id * 1.609344; ?
@JEROLDCOQUILLA it depends on what is inside of $rows. If it is an array of objects, than $rowData->id, alternatively, if each item in $rows is an array, than $rowData['id']
@JEROLDCOQUILLA please show how you getting initial data ($rows)
i select all from the database then use $arr_data = loadrowlist.. inside database there is id column,. thats why i am wondering if i can do like this $RowData->id..
|

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.