I am trying create an array of variables in different
types.
My aim is to create a .csv file to use with Canada Post Electronic Shipping Tools.
(code to create .csv file is not included here)
I am using Canada Post’s Electronic Shipping Guide’s
2.2.4 Import Orders Items Records – Layout Type 5 document.
I have to create 52 elements array and keep some “integer”, “boolean”,”currency”, “long”, and “place holder values” in the array.
My issues are.
- How can I define long values?
- What should I do to fill “place holder” values.
This is an example of place holder requirement.
Field NO:32, Field:US Postal Box indicator Type: Placeholder Length: 1 Format Notes: “1” indicates that the field is blank.
Below is the code I use to create an array
<?php
$record_count_array = array();
//Create an integer value
$integer = 00;
$integer = (integer)$integer ;
//Create a boolean value
$boolean = 1;
$boolean = (boolean )$boolean ;
//Fake long value - Please lset me know your suggestions
$long = 100000;
$string = "string_value";
//Create a currency value
$currency = 10.00;
$currency = money_format('%i', $currency);
//Fake spaceholder value - Please lset me know your suggestions
$placeholder = 2;
//Create a 52 item array
for($count = 0; $count< 52 ; $count++)
{
if($count == 0 || $count == 23 || $count == 47){
$record_count_array[] = $integer;
}
elseif($count == 18 || $count == 24 || $count == 25 || $count == 26) {
$record_count_array[] = $long;
}elseif($count == 27 || $count == 28 || $count == 29 || $count == 32
|| $count == 33 || $count == 34 || $count == 35 || $count == 36
|| $count == 37 || $count == 38 || $count == 39 || $count == 40
|| $count == 41 || $count == 44 || $count == 48 || $count == 49
|| $count == 50) {
$record_count_array[] = $boolean;
}elseif($count == 31 || $count == 42 || $count == 43) {
$record_count_array[] = $placeholder;
}elseif($count == 46) {
$record_count_array[] = $currency;
}
else{
$record_count_array[] = $string;
}
}
var_dump($record_count_array)."<br />";
?>
And this is the output I get (using var_dump())
array(52)
{[0]=> int(0) [1]=> string(12) "string_value"
[2]=> string(12) "string_value" [3]=> string(12) "string_value"
[4]=> string(12) "string_value" [5]=> string(12) "string_value"
[6]=> string(12) "string_value" [7]=> string(12) "string_value"
[8]=> string(12) "string_value" [9]=> string(12) "string_value"
[10]=> string(12) "string_value" [11]=> string(12) "string_value"
[12]=> string(12) "string_value" [13]=> string(12) "string_value"
[14]=> string(12) "string_value" [15]=> string(12) "string_value"
[16]=> string(12) "string_value" [17]=> string(12) "string_value"
[18]=> int(100000) [19]=> string(12) "string_value"
[20]=> string(12) "string_value" [21]=> string(12) "string_value"
[22]=> string(12) "string_value" [23]=> int(0) [24]=> int(100000)
[25]=> int(100000) [26]=> int(100000) [27]=> bool(true)
[28]=> bool(true) [29]=> bool(true) [30]=> string(12) "string_value"
[31]=> int(2) [32]=> bool(true) [33]=> bool(true)
[34]=> bool(true) [35]=> bool(true) [36]=> bool(true)
[37]=> bool(true) [38]=> bool(true) [39]=> bool(true)
[40]=> bool(true) [41]=> bool(true) [42]=> int(2)
[43]=> int(2) [44]=> bool(true)
[45]=> string(12) "string_value" [46]=> string(5) "10.00"
[47]=> int(0) [48]=> bool(true)
[49]=> bool(true) [50]=> bool(true)
[51]=> string(12) "string_value" }
Can somebody guide me to
- Format long variables correctly.
- Understand what is “place holder” variable do and how to format them?
Thank you very much
1 – You can treat the long as integer, as PHP doesn’t have the long type (AFAIK)
2 – For the placeholder, I don’t know if I understood correctly the problem but I’d suggest you to take a look on
sprintf(https://www.php.net/sprintf) to format the string.