I have created a script that generates an array.
This array needs to be stored so that if a user wants to export its contents( eg print ) it may be possible.
This is my array ($doc)
Array
(
[0] => Array
(
[0] => ------
[1] => ---Start----
[2] => -------
[3] => Generated on Saturday, 3rd, August 2013
)
[1] => Array
(
[0] => ------
[1] => -------
[2] => -------
[3] => -----------------------
)
[2] => Array
(
[0] => Tid
[1] => Policy
[2] => Budget
[3] => User
[4] => Capturetime
[5] => Country
)
[3] => Array
(
[0] => 1
[1] => ask
[2] => das
[3] => carol
[4] => 2013-07-09
[5] => Ethiopia
)
[4] => Array
(
[0] => 2
[1] => das
[2] => adsasd
[3] => ck
[4] => 2007-07-13
[5] => Slovakia
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[6] => Array
(
[0] => ------
[1] => ---End----
[2] => -------
)
)
Now I save the array to a file:
file_put_contents('array.txt', print_r($printdoc, true));
Then I would like to call back the contents of array.txt to the array $printdoc and use it as an array.
$printdoc = file_get_contents('array.txt', true);
I get $printdoc as a string instead of an array
Any help?
You can use serialize() to convert your array into a serializable string, then store it in a file. When you read the string from the file use unserialze() to convert it back into an array.
Related
I am attempting to convert an associative array to a 2D array to allow me to export it to Google Sheets. I've figured out a simplistic solution that works as follows:
$headers = $data["resultSets"][0]["headers"];
$rowSet0 = $data["resultSets"][0]["rowSet"][0];
$rowSet1 = $data["resultSets"][0]["rowSet"][1];
$hackresults = array_map(null, $headers, $rowSet0, $rowSet1);
This produces the following:
(
[0] => Array
(
[0] => SEASON_ID
[1] => 22017
[2] => 22017
)
[1] => Array
(
[0] => Player_ID
[1] => 203954
[2] => 203954
)
[2] => Array
(
[0] => Game_ID
[1] => 0021701118
[2] => 0021701105
)
[3] => Array
(
[0] => GAME_DATE
[1] => MAR 28, 2018
[2] => MAR 26, 2018
)
[4] => Array
(
[0] => MATCHUP
[1] => PHI vs. NYK
[2] => PHI vs. DEN
)
[5] => Array
(
[0] => WL
[1] => W
[2] => W
)
[6] => Array
(
[0] => MIN
[1] => 9
[2] => 27
)
[7] => Array
(
[0] => FGM
[1] => 2
[2] => 6
)
[8] => Array
(
[0] => FGA
[1] => 6
[2] => 12
)
[9] => Array
(
[0] => FG_PCT
[1] => 0.333
[2] => 0.5
)
[10] => Array
(
[0] => FG3M
[1] => 0
[2] => 0
)
[11] => Array
(
[0] => FG3A
[1] => 1
[2] => 1
)
[12] => Array
(
[0] => FG3_PCT
[1] => 0
[2] => 0
)
[13] => Array
(
[0] => FTM
[1] => 1
[2] => 8
)
[14] => Array
(
[0] => FTA
[1] => 2
[2] => 10
)
[15] => Array
(
[0] => FT_PCT
[1] => 0.5
[2] => 0.8
)
[16] => Array
(
[0] => OREB
[1] => 2
[2] => 1
)
[17] => Array
(
[0] => DREB
[1] => 1
[2] => 12
)
[18] => Array
(
[0] => REB
[1] => 3
[2] => 13
)
[19] => Array
(
[0] => AST
[1] => 0
[2] => 2
)
[20] => Array
(
[0] => STL
[1] => 0
[2] => 1
)
[21] => Array
(
[0] => BLK
[1] => 0
[2] => 2
)
[22] => Array
(
[0] => TOV
[1] => 1
[2] => 4
)
[23] => Array
(
[0] => PF
[1] => 1
[2] => 5
)
[24] => Array
(
[0] => PTS
[1] => 5
[2] => 20
)
[25] => Array
(
[0] => PLUS_MINUS
[1] => 7
[2] => 20
)
[26] => Array
(
[0] => VIDEO_AVAILABLE
[1] => 1
[2] => 1
)
)
This is the output I'm looking for, but there are 27 "rowSet"s, and it seems there must be a recursive way of performing this task.
I've looked at a number of custom array_map_recursive style functions but haven't had any success. Apologies and thanks in advance, I am a terrible novice coder!
You can use argument unpacking.
With the ... operator, you can use all the elements under $data["resultSets"][0]["rowSet"] as additional arguments to array_map.
$headers = $data["resultSets"][0]["headers"];
$rowSets = $data["resultSets"][0]["rowSet"];
$results = array_map(null, $headers, ...$rowSets);
(This isn't recursion, but I think it does what you're trying to do.)
I've got this simple code to test the output of token_get_all...
$arr = token_get_all("<?php $array=array(1,2,3); foreach($array as $key => $value) print($value); ?>");
print("<pre>");
print_r($arr);
print("</pre>");
But what ends up being displayed is this:
Array
(
[0] => Array
(
[0] => 372
[1] => 1
)
[1] => =
[2] => Array
(
[0] => 362
[1] => array
[2] => 1
)
[3] => (
[4] => Array
(
[0] => 305
[1] => 1
[2] => 1
)
[5] => ,
[6] => Array
(
[0] => 305
[1] => 2
[2] => 1
)
[7] => ,
[8] => Array
(
[0] => 305
[1] => 3
[2] => 1
)
[9] => )
[10] => ;
[11] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[12] => Array
(
[0] => 322
[1] => foreach
[2] => 1
)
[13] => (
[14] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[15] => Array
(
[0] => 326
[1] => as
[2] => 1
)
[16] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[17] => Array
(
[0] => 360
[1] => =>
[2] => 1
)
[18] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[19] => )
[20] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[21] => Array
(
[0] => 266
[1] => print
[2] => 1
)
[22] => (
[23] => )
[24] => ;
[25] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[26] => Array
(
[0] => 374
[1] => ?>
[2] => 1
)
)
From everything I've read about token_get_all, I'd expect the [0] key of these arrays to be the token names. What's going on with my code/server that I'm getting this instead?
I've also tried doing:
$arr = token_get_all(file_get_contents('someOtherValidPHPFile.php'));
And I get the same kind of result.
I'm using PHP version 5.4.19
Yes the token type is on index 0.
This is just a numeric value which identifies the token type. You can then compare them against the following list of token types: List of Parser Tokens
You can get the token name by using the token_name() function.
Tokens are defined as constants. E.g. the constant is named T_ARRAY and its value is 362. You can compare the tokens to that constant:
if ($token[0] == T_ARRAY) ...
If you want to get the readable name, use token_name.
I have an array encoded in UTF-8, sorta like this:
Array
(
[0] => אלף
[1] => בית
[2] => גימל
[3] => דלת
[4] => הא
[5] => ואו
)
Is it possible to leave the 0 array item empty and start the array at 1? I sorta wanna nudge everything over, and the array would then look like this:
Array
(
[0] =>
[1] => אלף
[2] => בית
[3] => גימל
[4] => דלת
[5] => הא
[6] => ואו
)
Thanks!
Use array_unshift, example:
<?php
$arr=array(1,2,3);
array_unshift($arr,null);
print_r($arr);
?>
Prints Array ( [0] => [1] => 1 [2] => 2 [3] => 3 )
Running PHP v5.3.10 using the simplexls class v0.4 and after loading a spreadsheet the dimensions (i.e. rows and columns counts) are off.
The rows do contain the column headings but all of the data rows are empty even though there is data in the spreadsheet.
Following is a small sampling of output for illustrative purposes. Any ideas why I'm not getting any actual cell data?
Array
(
[0] => cheyco.xlsx
)
Sheets count is: 7
i = 1:
Dimensions(1) returns:
Array
(
[0] => 4
[1] => 5
)
Rows(1) returns:
Array
(
[0] => Array
(
[0] => Facility Code
[1] => Site ID
[2] => Name
[3] => Description
)
[1] => Array
(
[0] =>
)
[2] => Array
(
[2] =>
)
)
i = 2:
Dimensions(2) returns:
Array
(
[0] => 5
[1] => 29
)
Rows(2) returns:
Array
(
[0] => Array
(
[0] => Unit Code
[1] => Facility Code
[2] => Type (I,E,O)
[3] => Name
[4] => Description
)
[1] => Array
(
[0] =>
[1] =>
[3] =>
)
[2] => Array
(
[0] =>
[1] =>
[3] =>
)
[3] => Array
(
[0] =>
[1] =>
[3] =>
)
[4] => Array
(
[0] =>
[1] =>
[3] =>
)
[5] => Array
(
[0] =>
[1] =>
[3] =>
)
[6] => Array
(
[0] =>
[1] =>
[3] =>
)
[7] => Array
(
[0] =>
[1] =>
[3] =>
)
It turns out that SimpleXLS is not able to read the newer .xlsx format. Saving the document in the previous .xls format resolved the issue.
How do I rearrange the following array
Array
(
[0] => Array
(
[0] => 1
[1] => Electronic
[2] => 0
)
[1] => Array
(
[0] => 2
[1] => mobile
[2] => 1
)
[2] => Array
(
[0] => 3
[1] => Tv
[2] => 1
)
[3] => Array
(
[0] => 4
[1] => smartphone
[2] => 2
)
[4] => Array
(
[0] => 5
[1] => freeze
[2] => 1
)
[5] => Array
(
[0] => 6
[1] => lg
[2] => 5
)
[6] => Array
(
[0] => 7
[1] => philips
[2] => 3
)
[7] => Array
(
[0] => 8
[1] => Onida
[2] => 3
)
[8] => Array
(
[0] => 9
[1] => Samsung
[2] => 3
)
[9] => Array
(
[0] => 10
[1] => Apple
[2] => 4
)
[10] => Array
(
[0] => 11
[1] => Apple 1.0
[2] => 10
)
[11] => Array
(
[0] => 12
[1] => Sasmung flat
[2] => 9
)
to create a parent child relationship having Parent with their respective child and save them in database. I want to save them as Wordpress categories and Show the result in wp-admin categories as categories are shown. looking forward for help.
Why not just register a new taxonomy? Make sure hierarchical is set to true.