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
Related
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
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?
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.
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
Ok, I usually do this in reverse, post a specific data set, then expect an JSON object back, however. In today's case I kinda need to the opposite. Except my confusion is brought on by there is no static elements to work with. My data won't be coming from a customary form, so serialization is not an option I don't think. So before I go more into it a little info on the spec
I have a Unordered list that can have anywhere from 1 List Element to dozens, each one contains 3 pieces if info I need to pass in a POST to my PHP as I would a form. But Im not sure whats the best way to handle it.
Can I pass it as an array through the post where the array is what it is? Or do I have to transform the output to resemble a JSON object and post that, treating it as a JSON object with the PHP and running it through json_decode? Not sure what to do here, so any advice is greatly appreciated.
edit worth mentioning is I can get the data I need from the list elements, its just what should I do with it to pass it in a post, so that it posts in sense like its a multidimensional object or array
You can pass the array from js using ajax as array. And it will come to $_POST as array without any additional steps
You need to transform it into a JSON string and then json_decode it on the other side. This is where node.js can be superior as a backend because you can pass the array directly to Node without going into and out of JSON.