Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

use php to change javascript data file item JavaScript

  • SOLVED

TASK: The "Exp" item is incorrect in the file below. I have MMYY and I need YYMM. For example, "0918" should be "1809". Please use php to write this.


JAVASCRIPT:
==========

// uscf_mems.json:

[
{
"Name": "Aaron, Deepak",
"ID": 12877422,
"Exp": "0918",
"St": "GA",
"Gr": "",
"Team": "",
"Rate": 2418,
"Local": ""
},

{
"Name": "Aaron, Dilip",
"ID": 12921644,
"Exp": "1809",
"St": "NY",
"Gr": "",
"Team": "",
"Rate": 1877,
"Local": ""
}
]

PHP
===


$array = json_decode(file_get_contents("uscf_mems.json"), true);

// This is where your php code goes.

file_put_contents("uscf.json", json_encode($array));

Answers (1)

2018-11-17

Rainner Lins answers:

One approach would be to split the Exp value in two, if one of them is less than 13 it's most likely the month, so move that one after the other and you get YYMM, something like this:


foreach ( $array as $i => $item ) {
$exp = $item['Exp'];
$num1 = substr( $exp, 0, 2 );
$num2 = substr( $exp, 2 );
$date = ( +$num1 < 13 ) ? $num2 . $num1 : $num1 . $num2;
$array[ $i ]['Exp'] = $date;
}