Reputation: 13
I have the following php code that I am looking to put into an array, and then json_encode:
$teamQuery = $xpath->query("//td[@align='left']");
$pointsQuery = $xpath->query("//td[@class='sortcell']");
$data = array ();
for($x=1; $x<=30; $x++){
$data[$x]['id'] = $teamQuery->item($x)->nodeValue;
$data[$x]['Team Points'] = $pointsQuery->item($x)->nodeValue;
}
echo json_encode($data);
The output looks like this:
{"1":{"id":"Team Query\n","Points Query":"110"},"
For the purposes of my project I would like to remove the line break (\n), as well as the leading {"1": in the code.
Any help and guidance would be greatly appreciated!
Upvotes: 0
Views: 108
Reputation: 522016
I would like to remove the line break (\n)
trim($teamQuery->item($x)->nodeValue)
as well as the leading {"1":
Then don't index your array starting from 1.
Put together:
$data = array();
for ($x = 1; $x <= 30; $x++) {
$data[] = array(
'id' => trim($teamQuery->item($x)->nodeValue),
'Team Points' => $pointsQuery->item($x)->nodeValue
);
}
Upvotes: 4