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 )
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
Actual JSON Object
[[["पिता का नाम","Father's name",null,null,1] ,[null,null,"pita ka naam"] ] ,null,"en",null,null,[["Father's name",null,[["पिता का नाम",1000,true,false] ] ,[[0,13] ] ,"Father's name",0,0] ] ,1.0,[] ,[["en"] ,null,[1.0] ,["en"] ] ]
After doing json_decode; i am getting below result.
i want to access first value which is in Hindi(INDIAN LANGUAGE)
Array ( [0] => Array ( [0] => Array ( [0] => पिता का नाम [1] => Father's name [2] => [3] => [4] => 1 ) [1] => Array ( [0] => [1] => [2] => pita ka naam ) ) [1] => [2] => en [3] => [4] => [5] => Array ( [0] => Array ( [0] => Father's name [1] => [2] => Array ( [0] => Array ( [0] => पिता का नाम [1] => 1000 [2] => 1 [3] => ) ) [3] => Array ( [0] => Array ( [0] => 0 [1] => 13 ) ) [4] => Father's name [5] => 0 [6] => 0 ) ) [6] => 1 [7] => Array ( ) [8] => Array ( [0] => Array ( [0] => en ) [1] => [2] => Array ( [0] => 1 ) [3] => Array ( [0] => en ) ) )
You can access properties of an array by index. The index starts at 0 being the first position then 1, and so on.
$arr = ['a','b','c'];
echo $arr[0]; //Output is 'a'
echo $arr[1]; //Output is 'b'
If you have a multi-dimensional array you just access the first property of the first array and then the second property of the second array like so:
$arr = [
['a','b','c'],
['d','e','f']
];
echo $arr[0][2]; //Output is 'c'
Note: in this example, $arr[0] is the first array holding a,b & c.
In your exact example, it looks like there are 4 nested arrays, so to get the Hindi text it would be:
echo $arr[0][0][0][0];
I was trying to perform KMP pattern match with multiple patterns that can be stored in array but cannot got a clue in accessing values in an array.
I have a sentence say, 'hello this is me doing something'. I exploded by space and got an array of words and stored in a variable $pattern. Now I have an array with lenght 6, having six words. Now I am using str_split function on each index to get letter from it. Code is as follows
$a="hello this is me doing something";
$b=explode(" ",$a);
print_r ($b);
Got an output
Array ( [0] => hello [1] => this [2] => is [3] => me [4] => doing [5] => something )
as expected
Then I tried splting each array using str_split function
$pattern=array();
for($i=0;$i<count($b);$i++){
$pattern[$i]=str_split($b[$i]);
}
print_r($pattern);
I got following output
Array ( [0] => Array ( [0] => h [1] => e [2] => l [3] => l [4] => o ) [1] => Array ( [0] => t [1] => h [2] => i [3] => s ) [2] => Array ( [0] => i [1] => s ) [3] => Array ( [0] => m [1] => e ) [4] => Array ( [0] => d [1] => o [2] => i [3] => n [4] => g ) [5] => Array ( [0] => s [1] => o [2] => m [3] => e [4] => t [5] => h [6] => i [7] => n [8] => g ) )
Now I want every pattern to be different array which can be accessed and called separately. Like if I have [0] => Array ( [0] => h [1] => e [2] => l [3] => l [4] => o )
The expected output should be stored in different array variables dynamically.
How can I get this?
-----problem solved, see the update 2 below----
I put all MySQL query results into a 2D array:
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
But now I cannot access the $allSuppliersInfo fields.
echo $allSupplierInfo[1][1]; // prints out 'Array'
echo $allSupplierInfo[1]['id']; //prints out nothing
What am I doing wrong?
------- UPDATE-----
print_r($allSupplierInfo) prints the following, so it looks like the loop is not working as I wanted it to:
Array (
[0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) )
[1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) )
[2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) )
[3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) )
[4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) )
------ UPDATE 2-----
Using the while loop, as suggested by RiggsFolly, solved the problem and I can access the fields as I initially wanted. I still do not understand why the for loop I used did not loop as I thought it would - any explanation would be greatly appreciated.
I think you got confused with your mysql result processing, you are using $result when checking for the number of rows returned but it should be $suppDescription.
This means that your for loop will not run as you will be getting 0 or probbaly FALSE as a response to mysql_num_rows($suppDescription);
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
Also this is easier done with a while loop, then you just process whatever is returned and dont need to bother getting the number of rows.
$suppDescription=mysql_query($query);
$allSupplierInfo=array();
while ( $rows = mysql_fetch_row($suppDescription) ) {
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
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.
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.