jugni
jugni

Reputation: 113

Read and write multi dimentional php array in ini file

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => >2013-05-02 09:46:26<
                    [1] => >441656721714 <441656721714><
                    [2] => >United Kingdom FIX United Kingdom - Fixed Landline<
                    [3] => >ANSWERED<
                    [4] => >441752387112<
                    [5] => >132<
                    [6] => >441<
                    [7] => >441656721714<
                    [8] => >0.0099<
                )

            [1] => Array
                (
                    [0] => 2013-05-02 09:46:26
                    [1] => 441656721714 <441656721714>
                    [2] => United Kingdom FIX United Kingdom - Fixed Landline
                    [3] => ANSWERED
                    [4] => 441752387112
                    [5] => 132
                    [6] => 441
                    [7] => 441656721714
                    [8] => 0.0099
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => >2013-05-02 09:46:26<
                    [1] => >441656721714 <441656721714><
                    [2] => >  <
                    [3] => >ANSWERED<
                    [4] => >448455774074<
                    [5] => >132<
                    [6] => ><
                    [7] => >441656721714<
                    [8] => >0.0<
                )

            [1] => Array
                (
                    [0] => 2013-05-02 09:46:26
                    [1] => 441656721714 <441656721714>
                    [2] =>   
                    [3] => ANSWERED
                    [4] => 448455774074
                    [5] => 132
                    [6] => 
                    [7] => 441656721714
                    [8] => 0.0
                )

        )
)

I have this type of PHP array, want to write this array in sample.ini file and read that array in same format.I have written this array using this link this is write data in file but when read data from file like $ini_array = parse_ini_file("sample.ini"); and when i print that array then it will show me as,

Array
(
    [0] => Array
        (
            [0] => >2013-05-02 09:46:26<
            [1] => >441656721714 <441656721714><
            [2] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [3] => >ANSWERED<
            [4] => >441752387112<
            [5] => >132<
            [6] => >441<
            [7] => >441656721714<
            [8] => >0.0099<
            [9] => >2013-05-02 09:46:26<
            [10] => >441656721714 <441656721714><
            [11] => >  <
            [12] => >ANSWERED<
            [13] => >448455774074<
            [14] => >132<
            [15] => ><
            [16] => >441656721714<
            [17] => >0.0<
            [18] => >2013-05-02 08:59:01<
            [19] => >441217720398 <441217720398><
            [20] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [21] => >ANSWERED<
            [22] => >441752387112<
            [23] => >82<
            [24] => >441<
            [25] => >441217720398<
            [26] => >0.00615<
            [27] => >2013-05-02 08:59:01<
            [28] => >441217720398 <441217720398><
            [29] => >  <
            [30] => >ANSWERED<
            [31] => >448455774074<
            [32] => >82<
            [33] => ><
            [34] => >441217720398<
            [35] => >0.0<
            [36] => >2013-05-02 08:36:14<
            [37] => >Anonymous <Anonymous><
            [38] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [39] => >ANSWERED<
            [40] => >441752387112<
            [41] => >378<
            [42] => >441<
            [43] => >Anonymous<
            [44] => >0.02835<
            [45] => >2013-05-02 08:36:14<
            [46] => >Anonymous <Anonymous><
            [47] => >  <
            [48] => >ANSWERED<
            [49] => >448455774074<
            [50] => >378<
            [51] => ><
            [52] => >Anonymous<
            [53] => >0.0<
            [54] => >2013-05-01 21:54:20<
            [55] => >443303500040 <443303500040><
            [56] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [57] => >ANSWERED<
            [58] => >442920337371<
            [59] => >1535<
            [60] => >442<
            [61] => >443303500040<
            [62] => >0.117683<
            [63] => >2013-05-01 21:53:37<
            [64] => >443303500040 <443303500040><
            [65] => >United Kingdom MOB United Kingdom - Mobile - H3G<
            [66] => >ANSWERED<
            [67] => >447878354165<
            [68] => >20<
            [69] => >447878<
            [70] => >443303500040<
            [71] => >0.004067<
            [72] => >2013-05-01 21:53:27<
            [73] => >443303500040 <443303500040><
            [74] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [75] => >BUSY<
            [76] => >442920337371<
            [77] => >0<
            [78] => >442<
            [79] => >443303500040<
            [80] => >0.0<
            [81] => >2013-05-01 21:53:17<
            [82] => >443303500040<
            [83] => >United Kingdom NGN Aloha GB - On Net<
            [84] => >NO ANSWER<
            [85] => >443303500067<
            [86] => >0<
            [87] => >44330350<
            [88] => >443303500040<
            [89] => >0.0<
            [90] => >2013-05-01 21:52:42<
            [91] => > <443303500040><
            [92] => >United Kingdom NGN Aloha GB - On Net<
            [93] => >ANSWERED<
            [94] => >443303500067<
            [95] => >12<
            [96] => >44330350<
            [97] => >443303500040<
            [98] => >0.0013<
            [99] => >2013-05-01 21:51:44<
            [100] => >443303500040 <443303500040><
            [101] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [102] => >BUSY<
            [103] => >442920337371<
            [104] => >0<
            [105] => >442<
            [106] => >443303500040<
            [107] => >0.0<
            [108] => >2013-05-01 21:51:20<
            [109] => >443303500040 <443303500040><
            [110] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [111] => >BUSY<
            [112] => >442920337371<
            [113] => >0<
            [114] => >442<
            [115] => >443303500040<
            [116] => >0.0<
            [117] => >2013-05-01 21:50:56<
            [118] => > <443303500040><
            [119] => >Peru MOB Peru - Mobile - Telefonica<
            [120] => >ANSWERED<
            [121] => >51971812978<
            [122] => >5<
            [123] => >51971<
            [124] => >443303500040<
            [125] => >0.0051<
            [126] => >2013-05-01 21:40:53<
            [127] => >51971812978 <51971812978><
            [128] => >United Kingdom NGN Aloha GB - On Net<
            [129] => >BUSY<
            [130] => >443303506050<
            [131] => >0<
            [132] => >44330350<
            [133] => >51971812978<
            [134] => >0.0<
            [135] => >2013-05-01 21:31:01<
            [136] => >443303500040 <443303500040><
            [137] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [138] => >NO ANSWER<
            [139] => >442920337371<
            [140] => >0<
            [141] => >442<
            [142] => >443303500040<
            [143] => >0.0<
            [144] => >2013-05-01 19:34:26<
            [145] => >443303500040 <443303500040><
            [146] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [147] => >ANSWERED<
            [148] => >442920337371<
            [149] => >1314<
            [150] => >442<
            [151] => >443303500040<
            [152] => >0.10074<
        )

    [1] => Array
        (
            [0] => 2013-05-02 09:46:26
            [1] => 441656721714 <441656721714>
            [2] => United Kingdom FIX United Kingdom - Fixed Landline
            [3] => ANSWERED
            [4] => 441752387112
...

So can anybody help me how can i get back array in the same format when i have written in file?

Upvotes: 3

Views: 721

Answers (1)

pinepain
pinepain

Reputation: 12859

if you are going to read this array in php you can just use something like this

$content = '<?php' . PHP_EOL;
$content .= '$arr = ' . var_export($your_array, true) . ';' .PHP_EOL;

file_put_contents('file.php', $content);

and then just inclure your file and be happy

function getArray() {
    include 'file.php';
    if(!isset($arr) {
        // ooops! something goes wrong
        return array(); // throw exception or process as you want such sitation
    }
    return $arr;
}

Pros:

  • fast, especially when your file doesn't change frequently and you have some opcode cacher, e.g. XCache or APC or else

Cons:

  • not secure. The content may be changed to any code and it will be executed with webserver rights (or with logged in user if you use it as a CLI)

Actually we use this solution on high load cause it cost-effective and we have pretty good security to do not worry about unauthorized access. You may try but this is not the best practice but the fastest way to do the job.

Upvotes: 1

Related Questions