1

I'm retrieving some data from a database where the values might be something like the following:

Column1 | Column2
-----------------
Bob     | 24
Joe     | 17
Jimmy   | 38
Joe     | 10
Bob     | 5
Sam     | 8

With this data, I want to somehow select the Bob rows and multiply them together. I want to do this for all rows that match in Column1. I'm not concerned about doing it in SQLite, but what is the best way to go about this in PHP? I've currently added the results to an array in PHP by doing the following:

$myArray[$resultRow['Column1']] = $resultRow['Column2'];

I was wondering how to go about navigating through this and finding matching keys, multipying their values together, and outputting something like

Column1 = Column2-a * Column2-b

6
  • do you want something like array('bob' => array('24', '5')) and the multiply 24 and 5 ? are you saved the data in array?
    – user1646111
    Jan 29, 2013 at 6:33
  • I'm trying that now, but it's coming out a bit messy so I was wondering if there was a more elegant solution. I could also be going about that method the wrong way, do you have a suggestion?
    – muttley91
    Jan 29, 2013 at 6:36
  • I cant understand exactly: do you want PHP code or query that do multiplication like sum()?
    – user1646111
    Jan 29, 2013 at 6:42
  • Either, but I'm fine with it being PHP code. Basically your first response is what I want, so that I could then go to each key and multiply its values together. I'm just not sure of a neat way to do it.
    – muttley91
    Jan 29, 2013 at 6:47
  • if you send your array that contains these data, or you printed that data from db? please update your question with exact code so I can write for you the exact solution, or, I can provide you general solution
    – user1646111
    Jan 29, 2013 at 6:53

2 Answers 2

1

consider this code:

$results = array();
foreach(array('Bob' => 24, 'Joe' => 17, 'Jimmy' => 38, 'Joe' => 10, 'Bob' => 5, 'Sam' => 8) as $key => $val){
 $results[$key][] = $val;
}
echo  '<pre>';
print_r($results);
echo  '</pre>';

out put:

Array
(
    [Bob] => Array
        (
            [0] => 5
        )

    [Joe] => Array
        (
            [0] => 10
        )

    [Jimmy] => Array
        (
            [0] => 38
        )

    [Sam] => Array
        (
            [0] => 8
        )

)

PHP ignores duplicate key, you have to create $results array when you retreiving the data from database, lets assume that you do like this:

while () {
    $col1 = $row["col1"];
    $col2 = $row["col2"];
    $results[$col1][] = $row["col2"];
}

now the results array must be like this:

Array
(
    [Bob] => Array
        (
            [0] => 24
            [1] => 5
        )

    [Joe] => Array
        (
            [0] => 17
            [1] => 10
        )

    [Jimmy] => Array
        (
            [0] => 38
        )

    [Sam] => Array
        (
            [0] => 8
        )

)

now, its easy to search for colmun1 names like this:

$values_for_colmn1 = $results[$column1]; //which is an array

// use array_product() function for array product

$product_for_column1 = array_product($values_for_colmn1);
4
  • Won't $results[$col1][] = $row["col2"]; just replace the value at that key every time?
    – muttley91
    Jan 29, 2013 at 19:07
  • I cant understand what you mean? dose my answer not helped you!?
    – user1646111
    Jan 29, 2013 at 19:58
  • For example, array['bob'] will be set with = on every loop, so won't the previous value be overwritten?
    – muttley91
    Jan 29, 2013 at 20:20
  • i used array['bob'][] with extra ([]) that adds a key automatically to the array- enjoy it
    – user1646111
    Jan 29, 2013 at 20:24
1

First, to read only Bob's values:

$stmt = $db->prepare('SELECT Column2 FROM MyTable WHERE Column1 = ?')
$stmt->bindValue(1, 'Bob', SQLITE3_TEXT);
$result = $stmt->execute();

Then, multiply all of them:

$value = 1.0;
while ($res = $result->fetchArray()) {
    $value = $value * $res['Column2'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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