0

I need help :)

I have a while/loop that gets values from my database. This loop combines database values with $_POST input and generales a $score for each item. CODE:


while($row=mysql_fetch_assoc($res)){ 
    $score=($row["near_beach_score"]*$result_a)+
    ($row["near_city_score"]*result_b)+
    ($row["near_swim_pool_score"]*result_c)+
    ($row["near_public_transport_score"]*result_d)+
    $row["house_promote_score"];

$array_content.='array('.$row["id"].', '.$score.') ';

when the while/loop is done I want to have a multiple array with the content of the string generated in the while/loop.

Like: $complete_array = array ($array_content);

and then i want to apply a array_multisort.

How to concatenate/insert $array_content into $complete_array = array ($array_content); ?

Apreciate if you have time to help. /Sérgio

2 Answers 2

1

Assuming that id is a unique field:

$arr = array();
while ($row = mysql_fetch_assoc($res)) {
    $score = ...;
    $arr[$row['id']] = $score;
}

It stores the value of $row['id'] in the keys and $score in the values.

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

1 Comment

Thank you Jack! I did as you suggested and did arsort($arr); to sort my array. Nice.
1

Try this:

<?php
$array_content = array();
while($row=mysql_fetch_assoc($res)){
    $score=($row["near_beach_score"]*$result_a) + 
            ($row["near_city_score"]*result_b)+ 
            ($row["near_swim_pool_score"]*result_c)+ 
            ($row["near_public_transport_score"]*result_d)+ 
            $row["house_promote_score"];

    $array_content []=array($row["id"], $score);
}

you produced a String. If you remove the ' you will get an 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.