PHP Array for specified JSON output - php

I just started working with JSON, so this is a hard way for me. I spent more than 15 hours trying to configure out how I should create and fill an array in PHP to get specified JSON format.
I need to get exactly this example1
{"players":["xZero","Lux"],"points":[{"player":"xZero","points":"20"}]}
and this
example2
{"players":["xZero","Lux"],"points":[]}
PHP will use foreach loops to fill data in example1 from database.
foreach($userdb as $users){
$output["users"][$i]=$users;
$i++;
}
This is just first half, I do not have any idea how to insert data in another half.
To get exact output like example1.
For example 2 I also don't have idea how to fill it out. I known for first half with users, but another half is mistery, like in example1.
As you can see, my experience with JSON is extremely poor. Also I'm not verry experienced with multidimensional arrays.
Thanks in advance for any help!

Based on your JSON your PHP array should look something like this:
array("players"=>array("xZero","Lux"),"points"=>array(array("player"=>"xZero","points"=>20)))
My best advice for you is to populate an array like this or create objects using classes and then using the json_encode() function.

Related

Parse XML data to JSON

In our environment we use MS SQL w/ stored procedures. In those procedures we return our results as XML and when access the data as we need to.
I'm introducing some charts into our tools which are 3rd party and require specific formats in order to operate and I am running into an issue.
In this screenshot, you are able to see what the structure should look like which I can get to work with the plugin just fine. The issue is with how SimpleXML handles single result sets.
As you can see in the image below, with one result item, it is no longer formatted as an array. The problem being is that the plugin expects to find the data in the format in the first example, but when there is only one value, it doesn't store it as an array.
As you can see from this image, the dataset for the escalationTypes is in the array format where the one below, submittedByDepartment is not.
I am trying to find out if there is something I can do to fix the root of this problem, with SimpleXML. Is this a common issue found with SimpleXML with a workaround?
UPDATE
Here is a sample of the XML Object I am working with: http://pastebin.com/uPh0m3qX
I'm not 100% clear what your required structure is, but in general, it's not a good idea to jump straight from XML to JSON or vice versa. Both can represent data in various ways, and what you really want is to extract data from one and turn it into the other.
Note that this is what SimpleXML is designed to help you with - it never contains any arrays, but it gives you an API which helps you extract the data you need.
If I understand correctly, you want to build an array from each of the dataset elements, and put those into your JSON, so you'd want something like this:
foreach ( $xml->children() as $item_name => $item ) {
foreach ( $item->dataset as $dataset ) {
$json[$item_name]['dataset'] = (array)$dataset->attributes();
}
}
Note that neither of those loops will behave differently if there is only one item to loop over. SimpleXML decides whether to behave like an array or an object based on how you use it, not based on what the XML looks like.
Note that while you can build a more general XML to array (or XML to JSON) function, getting it to always give the desired output from any input will probably take more time, and lead to harder-to-debug code, than writing specific code like the above.
you can declare the object as an array by adding (array) before referencing the variable. (array)$wasobject

How to handle with array in mysql?

I have some questions about handling with array concerning storing in mysql.
I stored an array (of numbers) in my mysql database as text Like: COLUMN_ARRAY: [2,3,4,5,6,7]
( including [ and ] )
My first question: is this a good method or can I work with that for my further steps? or are there any other better methods for storing array in sql column?
My second question: how could it be possible to pick every single element, or go throu all elements in the array with php?
because I want to make a query like if COLUMN_ARRAY includes the number 3 then do blablabla...
maybe you can give me some hint, idea, link or tutorial :)
EDIT:
Ok I did some change and changed everything to something like that:
How to store arrays in MySQL?
the only problem is now: how can I handle with an array (like [1,2,3]) in php - to handle with every single element in that?

Building a multidimensional JSON array through PHP/MySQL

I am trying to build what I think is a multidimensional array in json through php and a connection to a mysql database.
Here's the output I am looking for:
[{"region":"Americas"},
{"region":"West","division":"Americas","revenues":"100000","headcount":"6"},
{"region":"East","division":"Americas","revenues":"1500000","headcount":"8"},
{"region":"South","division":"Americas","revenues":"1000000","headcount":"7"},
{"office":"San Francisco","location":"50 Kearny Street", "branch":"West"},
{"office":"Los Angeles","location":"200 Sepulveda","branch":"West"},
{"office":"New York","location":"Penn Street","branch":"East"},
{"office":"Philadelphia","location":"155 Trent","branch":"East"},
{"office":"New Jersey","location":"420 Broadway","branch":"East"},
{"office":"Atlanta","location":"39000 Parker","branch":"South"}]
This gives me the feed into a graphing program that creates the proper visualization. My database returns all the values in the JSON above (region, division, revenues, etc.) EXCEPT for "branch" which I had to hand code myself. I can't sure "region" again (because of the graphing algorithm) so I had to rename it to "branch." Under this current configuration, the "Americas" is the top node, with the "regions" underneath, and then the "branches" under each region as appropriate.
So far, I've only been able to build a basic JSON array using PHP and the json_(encode) function ; to get the output above, I edited the file by hand to make sure it would render properly in my graph, which it does.
The question I am looking to answer is: what is the fastest and easiest way to build the above array on the fly? Through a loop in PHP, I would imagine, but I can't get my head around what that would look like.
I probably left out information you need to help, so this is just to get the ball rolling.
Thanks!
Look at this topic: Looping a multidimensional array in php
You can loop through the array using the foreach construct

whats the best way to process an xml to MySql in php

hi guys been struggling with this for a while, i can do xsl transformation on the data and that works to output the data, but i want to save the data i have tried automatically generating an array like shown here complex multidimentional associative array process with foreach but this didn't work as there are multiple accounts of room and i can only have the name ['room'] once in an array
so i need to know the best way to get xml to MySql using php
an easy way is to use PHP's integrated SimpleXML

Ideas for importing text data using PHP array functions

I am new to php and am asking for some coding help. I have little experience with php and have gone to the php.net site and read couple books to get some ideas on how to perform this task.
There seems to be many functions and I am confused on what would be the best fit. (i.e. fgetcsv, explode(), regex??) for extracting data in the file. THen I would need assistance printing/display this information in orderly fashion.
Here is what I need to do:
import, readin txt file that is
delimited (see sample)
The attributes are not always ordered and some records will have missing attributes.
Dynamically create a web table (html)
to present this data
Sample records:
attribute1=value;attribute2=value;attribute3=value;attribute4=value;
attribute1=value;attribute2=value;attribute4=value;
attribute1=value;attribute2=value;attribute3=value;
How do I go about this? What would be best practice for this? From my research it seems I would create an array? multidimensional? Thank you for your time and insight and i hope my question is clear.
Seems like homework, if so best to tag it as such.
You will want to look into file(), foreach() and explode() given that it is delimited by ;
The number of attributes should not matter if they are missing, but all depends on how you setup the display data. Given that they are missing though, you will need know what is the largest amount of attributes to setup the table correctly and not cause issues.
Best of luck!
i would first use the file() method, which will give you an array with each line as an element. Then a couple of explodes and loops to get through it all,first exploding on ';', then loop through each of these and explode on '='.

Categories