First of all, I really don't know if that kind of topic exist. But I searched a lot and now I am here.
My question about parsing. For example I would like to unset some items.
$now = array();
$now[0]['name'] = "Hello1";
$now[0]['si'] = "BumBum1";
$now[1]['name'] = "Hello2";
$now[1]['si'] = "BumBum2";
$now[2]['name'] = "Hello3";
$now[2]['si'] = "BumBum3";
$now[3]['name'] = "Hello4";
$now[3]['si'] = "BumBum4";
echo json_encode($now)."<br>";
unset($now[0]);
echo json_encode($now);
And the output:
[{"name":"Hello1","si":"BumBum1"},{"name":"Hello2","si":"BumBum2"},{"name":"Hello3","si":"BumBum3"},{"name":"Hello4","si":"BumBum4"}]
{"1":{"name":"Hello2","si":"BumBum2"},"2":{"name":"Hello3","si":"BumBum3"},"3":{"name":"Hello4","si":"BumBum4"}}
And my the JSON file turns to messy code. Appears numbers and etc.
Any ideas how to solve this.
You need to "reindex" the array (use array_values() function).
//unset..
unset($now[0]);
//reindex
$now = array_values($now);
//display as before
echo json_encode($now);
Related
I am new to this but making some real good progres :-).
It is all in PHP, JSON.
I am stuck at this simple question and cannot find an answer anywhere on the web.
Here is my problem:
I have code stored inside the variable.
I want to output that code just below some other code and I want it to be interpreted.
Alternatively I want the code from the $variable to be runned in the middle of my code.
I have tried echo, print, var_dump etc. their output is human readable and I want it to be just runned in the middle of my code.
$variable='
$request[method1][0] = array();
$request[method1][0][var1] = 1;
$request[method1][0][var2] = 13;
$request[method1][0][var3] = nonrefundable;
$request[method1][0][var4] = 1;
$request[method1][0][var5] = 96;
$request[method1][0][var6] = "2019-02-12";
$request[method1][0][var7] = whole;'
needless to say it is exactly 6211 times longer
$request = array();
$request['authenticate'] = array();
$request['authenticate']['systemKey'] =
$request['authenticate']['systemLogin'] =
$request['authenticate']['lang'] = 'eng';
output ($variable)
I am looking for that 'output' function or some other method. Later on $request is encoded with 'json_encode'
I expect it to run as the code from the variable would be just pasted below.
To display a variable in PHP, you have several options, each being more useful in certain contexts:
echo (https://php.net/echo)
var_dump (https://php.net/var_dump)
print_r (https://php.net/print_r):
They are used the following way:
echo $variable;
var_dump($variable);
print_r($variable);
If you wish to recover your result from a JavaScript script for example, you would encode it as JSON and then echo it:
echo json_encode($variable);
I don't know the problem.
The data is saved in the session but it only shows the last input.
$_SESSION['data'][] = $_POST;
$_SESSION['data']['lengtezijde'] = $_POST['lengtezijde'];
$_SESSION['data']['kleur'] = $_POST['kleur'];
$_SESSION['data']['hoogte'] = $_POST['hoogte'];
?><tr><?
?><th><?echo $_SESSION['data']['lengtezijde'];?></th><?
?><th><?echo $_SESSION['data']['kleur'];?></th><?
?><th><?echo $_SESSION['data']['hoogte'];?></th><?
?></tr><?
I have tried your code also and it work me. Since it is an array, you must loop trough it to display the values and that is why I think it is giving you only the last value.
//Declare your variables
$lengtezijde = $_POST['lengtezijde'];
$kleur = $_POST['kleur'];
$hoogte = $_POST['hoogte'];
//Store it in session
$_SESSION['data'] = array(
'lengtezijde' => $lengtezijde,
'kleur' => $kleur,
'hoogte' => $hoogte,
);
Now you can loop through your data and display it. I hope this helps.
if i understand what you looking for you must check your $_POST
i test this code
$_SESSION['data'][] = "Test";
$_SESSION['data']['lengtezijde'] = 'test1';
$_SESSION['data']['kleur'] = 'test2';
$_SESSION['data']['hoogte'] = 'test3';
echo $_SESSION['data']['hoogte'];
br();
echo $_SESSION['data']['kleur'];
br();
echo $_SESSION['data']['lengtezijde'];
result :
test2
test1
test3
Make sure you have mentioned session_start() in First Line of your code ,
session_start() too, before you assign values to $_SESSION
Rest of things seems ok in your code.
Never use below kind of assignment it will ruin your machine memory . If you run your script you will see that on each refresh of the page this size will be increasing. that is not advisable.
$_SESSION['data'][] = $_POST;
I am trying to parse this JSON data to print on a fanpage I am working on.
If you look at that JSON link, you will see that the structure is [{key:value,key:value,key:value}]. I recently learned how to parse JSON with a slightly different structure like this JSON file, where the data structure is {"identifier":[{key:value,value,value,value,value}{key:value,value...}]}
Here is my code I am attempting:(I have tried about 10 variations of this with explodes for the commas too)
<?php
$json = file_get_contents('http://live.nhl.com/GameData/SeasonSchedule-20152016.json');
$json = json_decode($json, TRUE);
foreach($json as $d){
$estTime = $d['est'];
echo $estTime;
?>
As I said, I had some success with that other structure of JSON I linked by doing this:
$json = file_get_contents('http://nhlwc.cdnak.neulion.com/fs1/nhl/league/playerstatsline/20152016/2/SJS/iphone/playerstatsline.json');
$json = json_decode($json, TRUE);
$skaterData = $json['skaterData'];
$goalieData = $json['goalieData'];
foreach($skaterData as $d){
$stats = explode(',', $d['data']);
$number = $stats[0];
$position = $stats[1];
$name = $stats[2];
$gp = $stats[3];
$goals = $stats[4];
$assists = $stats[5];
$points = $stats[6];
$plsmns = $stats[7];
$pim = $stats[8];
$shots = $stats[9];
$toi = $stats[10];
$pp = $stats[11];
$sh = $stats[12];
$gwg = $stats[13];
$ot = $stats[14];
Edit: JSON data successfully parsed
The only thing wrong with your code is that you are missing the closing curly bracket to your foreach.
I would strongly recommend paying attention to the error messages you get, often they will let you easily solve the problem. If your server does not display them in the browser (this is usually a good thing on live sites), you will find them in an error log somewhere on the server.
Additionally you may want to use a proper editor with linting (what is linting), which would probably have immediately notified you of this omission one way or another. One such free tool is Atom.
I have this code for scraping team names from a table
$url = 'http://fantasy.premierleague.com/my-leagues/303/standings/';
$html = #file_get_html($url);
//Cut out the table
$FullTable = $html->find('table[class=ismStandingsTable]',0);
//get the text from the 3rd cell in the row
$teamname = $FullTable->find('td',2)->innertext;
echo $teamname;
This much works.. and gives this output....
Why Always Me?
But when I add these lines..
$teamdetails = $teamname->find('a')->href;
echo $teamdetails;
I get completely blank output.
Any idea why? I am trying to get the /entry/110291/event-history/33/ as one variable, and the Why Always Me? as another.
Instead do this:
$tdhtml = DOMDocument::loadHTML($teamdetails);
$link = $tdhtml->getElementsByTagName('a');
$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
I also fail to see how your "works" code could possibly work. You don't define $teamname in there either, so all you'd never get is the output of a null/undefined variable, which is...no output all.
Marc B is right, I get that you don't have to initialize a variable, but he is saying you are trying to access a property of said variable:
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
This is essentially:
$teamname = null;
$teamname->find('a')->href;
The problem in your example is that $teamname is a string and you're treating it like a simple_html_dom_node
I just wanted some input about my use of JSON.
<?php
header('Content-Type: text/plain');
//results
$results = array();
for($i=0;$i<20;$i++)
{
$result = array();
$result['name'] = 'Test Season '.ceil(($i+1)/13).' Episode '.(($i%13)+1);
//$result['torrent'] = 'https://www.example.com/?id='.$i.'&key='.uniqid();
$result['torrents'] = array();
$c = mt_rand(1,4);
for($j=0;$j<$c;$j++)
{
$torrent = array();
$torrent['url'] = 'https://www.example.com/?id='.uniqid().'&key='.md5(uniqid());
$torrent['codec'] = $j%2 == 0 ? 'xvid' : 'h264';
$torrent['resolution'] = '720p';
$result['torrents'][] = $torrent;
}
$results[] = $result; //push
}
echo json_encode($results);
?>
This is just some test code, not an actual implementation. Am I using JSON correctly and too the fullest? Or is some better method of doing this?
I have legal torrents that I'd like to do some JSON with.
Torrents are grouped by name which contain multiple torrents (actual links to data). And other information such as codec etc.
This is my first time actually outputting JSON, would XML be better?
Are there any guides on this topic (hopefully not entire books)?
Thanks.
What you doing is right. I like to use the StdClass to make objects rather than key value arrays, just cause it looks sexier! E.g.
$torrent = new StdClass();
$torrent->url = 'https://www.example.com/?id='.uniqid().'&key='.md5(uniqid());
$torrent->codec = $j%2 == 0 ? 'xvid' : 'h264';
$torrent->resolution = '720p';
$result['torrents'][] = $torrent;
As you say you don't need to read a whole book on the matter, I would have a look here http://php.net/manual/en/book.json.php to get to grips with the basics of JSON.
In terms of JSON vs XML, I find it far easier to represent data as JSON as it is easier to fetch the specific data you want much in the same way you can access the info in a stdClass object.
[EDIT]
And as Stefan Gehrig says make sure you define your content type as "application/json".
Absolutely fine. You could only change the MIME type to be RFC 4627 compliant though: application/json.