I'm not really a web developer and I'm trying to make a web interface for parsing and displaying experimental data, so I apologize if this is a ridiculously easy solution. I'm trying to take data from an XML element and convert it into a PHP numerical array that I can plot using GD. So far, I have the XML creation, XML loading, and GD plotting finished (I think), but what I need is the key to converting the data into a PHP array to finish.
I have an xml file with numeric data stored like this:
<?xml version="1.0" encoding="utf-8" ?>
<NotData>
<Data>0 1 2 3 4 5 6 7 8 9</Data>
</NotData>
I'm using simplexml_load_file to read in the data file. Then I pull out the data like this:
$myData=$xmldata->NotData[0]->Data;
Now I have an object with the data inside, but I'm not sure how to get this out into a numeric array.
Considering you have that string, you first have to load it using SimpleXML :
$str = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<NotData>
<Data>0 1 2 3 4 5 6 7 8 9</Data>
</NotData>
XML;
$data = simplexml_load_string($str);
I used simplexml_load_string because it was simpler for my test ; of course, you can still use simplexml_load_file, as you are already doing.
Then, to get the field containing the numbers into a variable :
$numbers_string = (string)$data->Data;
var_dump($numbers_string);
Which gets you :
string '0 1 2 3 4 5 6 7 8 9' (length=19)
And, then, you can use the explode function to... well, explode... the string into an array, using a space as a separator :
$numbers_array = explode(' ', $numbers_string);
var_dump($numbers_array);
And you finally get this array :
array
0 => string '0' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '3' (length=1)
4 => string '4' (length=1)
5 => string '5' (length=1)
6 => string '6' (length=1)
7 => string '7' (length=1)
8 => string '8' (length=1)
9 => string '9' (length=1)
Which seems to be what you were waiting for ;-)
Related
i would like to import an csv file and I have got a problem to compare the result with a constant.
my file is looking something like this:
1;1;23
1;2,11
...
When I dumped the first row I got this result:
array (size=3)
0 => string '1' (length=4)
1 => string '1' (length=1)
2 => string '23' (length=2)
How the hell could be the result string '1' = length 4
I also returned the length of the string with strlen and also trim the string but i got everytime the result 4. Btw I use the PHP version 7.4 with xampp
So I have the following data array:
array (size=10)
0 => int 5
1 => string '5B1' (length=7)
2 => int 4
3 => string '4B1' (length=7)
4 => int 3
5 => string '3B1' (length=7)
6 => int 2
7 => string '2B1' (length=7)
8 => int 1
9 => string '1B1' (length=7)
What I want to do is sort it to this:
array (size=10)
0 => string 5B1
1 => int '5' (length=7)
2 => string 4B1
3 => int '4' (length=7)
4 => string 3B1
5 => int '3' (length=7)
6 => string 2B1
7 => int '2' (length=7)
8 => string 1B1
9 => int '1' (length=7)
The hierarchy of the numbers never change although there is an additional level as seen here:
7 -> 6b2 -> 6b1 -> 6 -> 5b2 -> 5b1 -> 5 4b2 -> 4b1 -> 4 -> 3b2 -> 3b1 -> 3 -> 2b2 -> 2b1 -> 2 -> 1b2 -> 1b1 -> 1 -> b2 -> b1
I am wondering what is the best way to sort this in PHP?
On the one hand I am thinking of looping through the static hierarchy array from top to bottom and using this to order the dynamic array.
Any other suggestions?
The answer to this was quite simple, Mark Baker put me on the right track.
I originally used a convoluted mix between rsort, krsort and array_flip to get an almost working solution.
Using rsort with the SORT_NATURAL flag did the trick.
I have two arrays in a smarty template: $months and $contract.
{$months|var_dump} gets this:
array (size=12)
1 => string 'января' (length=12)
2 => string 'февраля' (length=14)
3 => string 'марта' (length=10)
4 => string 'апреля' (length=12)
5 => string 'мая' (length=6)
6 => string 'июня' (length=8)
7 => string 'июля' (length=8)
8 => string 'августа' (length=14)
9 => string 'сентября' (length=16)
10 => string 'октября' (length=14)
11 => string 'ноября' (length=12)
12 => string 'декабря' (length=14)
array values are russian names of months in genitive.
{$contract|var_dump} gets this
'date_till' => '1355518365' (length=10)
so I need to create a month number first from $contract.date_till. it is usually done like
{$contract.date_till|date_format:"%m"}
And now the question is: how do I extract a month name from $months array by the month number made of $contract.date_till with date_format?
I've tried many variants described in smarty manuals, but noone works. For example, this one doesn't:
{$months[{$contract.date_till|date_format:"%m"}]}
{assign var=monthNo value=$contract.date_till|date_format:"%m"}
{$months.$monthNo}
This will give u the month of the necessary date.
Hey guys I'm tired and can't figure this one out so any help would be appreciated.
I have an array called $Row that I get from database table.
When I run var_dump($Row); I get the following:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'erik' (length=4)
'username' => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
'email' => string '' (length=0)
4 => string '0' (length=1)
'date_join' => string '0' (length=1)
5 => string '0' (length=1)
'date_mod' => string '0' (length=1)
6 => string '1' (length=1)
'active' => string '1' (length=1)
7 => string '1' (length=1)
'admin' => string '1' (length=1)
8 => string '0' (length=1)
'deleted' => string '0' (length=1)
When I run echo count($Row); I get value 18.
Count and var_dump are right next to each other, there is no modification of $Row.
Question: Why does Count() return 18 entried when there are only 8 of them inside $Row shown by var_dump()? I guess I just don't understand count()... I checked http://php.net/manual/en/function.count.php, but still don't get it...
Edit: I understand whats wrong know, thanks everyone. Another question. How can I remove the ones that are in a string, for instance I want this kind of a table:
array
0 => string '1' (length=1)
1 => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
4 => string '0' (length=1)
5 => string '0' (length=1)
6 => string '1' (length=1)
7 => string '1' (length=1)
8 => string '0' (length=1)
* I'm using mysql_fetch_array() to retrieve the data and put it into a table.
You are having mixed array integer and associative and having 18 elements. This is I think returned from mysql_fetch_array which by default return associative and numeric indexed array.
Because your strings are also values in $Row they just seem to be indented because of the string char.
That means
'id' => string '1' (length=1)
'username' => string 'erik' (length=4)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
// ...
are also in your array
becuase you're getting back a mix of associative and numeric result set from you db ( you can precise which one you really want with FETCH_ASSOC or alike as a parameter to you dbvendor_query() function ).
There is very well 18 elements in your array.
I think that applying count to this:
0 => string '1' (length=1)
'id' => string '1' (length=1)
returns 2 instead of 1!
0 and id are two different items of the array!
This taken for the others 8 elements make the count return 18
Actually, there are indeed 18 elements in your array:
indices 0 through 8 (9 elements in total)
id, username, ... another 9 elements
9 + 9 = 18
If you only want the numeric indices, and supposing you're using MySQL:
$Row = mysql_fetch_array($result, MYSQL_NUM)
Source: http://php.net/manual/en/function.mysql-fetch-array.php
I am using SimpleHTMLDOM to get information from my school roster. The problem is that the table structure is pretty hard to parse and I am looking for some help.
The table looks like this:
http://pastebin.com/xg3mRAHw
The code looks like this:
http://pastebin.com/gWW7WyDA
The result looks like this (also included how I want the result to look like!):
Current format:
array
3 =>
array
'28-11-2011' =>
array
0 => string '08.45-10.30 ' (length=12)
1 => string 'CMD-1 HC interaction design' (length=27)
2 => string 'CMD-1vt-p2.01 - CMD-1vt-p2.18 ' (length=30)
3 => string 'OVk45' (length=5)
4 => string 'J.P. van Leeuwen' (length=16)
5 => string '10.30-12.15 ' (length=12)
6 => string 'CMD-1 Training samenwerken' (length=26)
7 => string 'CMD-1vt-p2.09 - CMD-1vt-p2.10 ' (length=30)
8 => string 'SL433' (length=5)
9 => string 'B. Hartman' (length=10)
Wanted format:
array
3 =>
array
'28-11-2011' =>
array
0 =>
array
'time' => string '08.45-10.30 ' (length=12)
'name' => string 'CMD-1 HC interaction design' (length=27)
'group' => string 'CMD-1vt-p2.01 - CMD-1vt-p2.18 ' (length=30)
'place' => string 'OVk45' (length=5)
'teacher' => string 'J.P. van Leeuwen' (length=16)
1 =>
array
'time' => string '10.30-12.15 ' (length=12)
'name' => string 'CMD-1 Training samenwerken' (length=26)
'group' => string 'CMD-1vt-p2.09 - CMD-1vt-p2.10 ' (length=30)
'place' => string 'SL433' (length=5)
'teacher' => string 'B. Hartman' (length=10)
The problem is that I do not understand how I can get to this result using (only) SimpleHTMLDOM. I am sure that I'm missing something here because I'm close to the final markup of the array. The last step to have it actually show up like the future example is something I cannot get to work.
Could someone give me a few tips on how to proceed and get the array like the way I want it to? I have been looking at XSL too but that is far too complicated for me at this point.
You need to segment the tr array as well.
$count = 0;
foreach ($table as $tr) {
...
$output[$info['week']][$info['date']][$count] = array();
$count++;
...
$output[$info['week']][$info['date']][$count][] = $td->innertext;
Now as for the 'time', 'name', 'group' etc. values, I don't see those anywhere in the xml, so I guess you will just have to maintain an inner count when appending td->innertext.