0

I have 15 countries, for each country there are a set amount of 20 points. For each point there is a different value per country

Example

The UK in Price Point 1 has a Price Value of 1.3

The US in Price Point 1 has a Price Value of 1.5

And so on,

Should I go with the approach like this or is there a better way to do this.

I want to firstly put this into a CSV then use fgetcsv to convert it.

Array
(
    [0] => Array
        (
            [country] => US
            [price_point] => 1
            [price_value] => 1.5
        )

    [1] => Array
        (
            [country] => UK
            [price_point] => 1
            [price_value] => 1.3
        )
)
3
  • it's perfect approach. Commented Apr 11, 2017 at 15:52
  • If going into a CSV this is the ideal. Row is the array position, those 3 attributes are column headers Commented Apr 11, 2017 at 15:52
  • Thanks, however surely with this approach the array size will be 300, I thought there might be a simple way of doing it as there is only 20 fixed points for price point Commented Apr 11, 2017 at 15:54

1 Answer 1

2

Hide that in your code-treasue-box;

$array = 'what you have above';
//re-index
$array = array_values($array);
//keep keys ordered
ksort($array[0]);
//make the header
$table ="<table><tr><th>".implode("</th><th>",array_keys($array[0]))."</th></td>";
//iterate 
foreach($array as $arr){
    ksort($arr);//sort 
    $table .= "<tr><td>".implode("</td><td>",$arr)."</td></tr>";
}

print $table."</table>";

This can also updated for use with fwrite and so on.

( I missed a little the topic here, but can be handy ;-) )

Have a nice day

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

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.