vendredi 31 juillet 2015

json_decode multidimensional from database

I have this json saved in my database in a column called 'price' with the type of 'text'.

{
  "desks": {
    "Dedicated Desk": "$400 mo"
  },
  "private offices": {
    "1 Person": "$550 mo",
    "2 Person": "$1100 mo",
    "3 Person": "$1600 mo",
    "4 Person": "$2100 mo",
    "6 Person": "$3300 mo"
  },
  "flexible membership": {
    "Starting at": "$45 mo",
    "Every Extra Day": "$50 Day"
  }
}

I then make a call from PHP to return my all the fields in my database and encode them as json.

$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;

$response is the response from the database. When I var_dump on $response I get

array(22) {
    [0]=>
      object(stdClass)#6 (38) {
        [...]

        ["price"]=>
        string(242) "{"desks":{"Dedicated Desk":"$400 mo"},"private offices":{"1 Person":"$550 mo","2 Person":"$1100 mo","3 Person":"$1600 mo","4 Person":"$2100 mo","6 Person":"$3300 mo"},"flexible membership":{"Starting at":"$45 mo","Every Extra Day":"$50 Day"}}"

        [...]
      }
    [...]
}

When I echo the result from json_encode i get

[
    {
        [...]

        "price": "{\"desks\":{\"Dedicated Desk\":\"$400 mo\"},\"private offices\":{\"1 Person\":\"$550 mo\",\"2 Person\":\"$1100 mo\",\"3 Person\":\"$1600 mo\",\"4 Person\":\"$2100 mo\",\"6 Person\":\"$3300 mo\"},\"flexible membership\":{\"Starting at\":\"$45 mo\",\"Every Extra Day\":\"$50 Day\"}}",

        [...]
    },
    [...]
]

My problem is json_encode is taking the json from the database and formatting it as a string. I am trying to get it to format it as part of a multidimensional json structure. This is what im trying to achieve:

[  
   {  
      "price":[  
         {  
            "desks":{  
               "Dedicated Desk":"$400 mo"
            },
            "private offices":{  
               "1 Person":"$550 mo",
               "2 Person":"$1100 mo",
               "3 Person":"$1600 mo",
               "4 Person":"$2100 mo",
               "6 Person":"$3300 mo"
            },
            "flexible membership":{  
               "Starting at":"$45 mo",
               "Every Extra Day":"$50 Day"
            }
         }
      ]
   }
]

Any help would be appreciated. Running latest version of php.

How to assign array into variable in jquery

The original and working array is this:

 var columns =[{
        name: 'level',
        minWidth: '200px'},
        {
        name: 'subject',
        minWidth: '70px'},
        {
        name: 'catid',
        minWidth: '70px'}],

However, I want to use if else statement to assign different array for different 'target' value. So I tried as below but it doesn't seem to supply the array correctly to the 'columns' variable.

 var useThis=[];
        if(target=="subject")
        {
            useThis = {
            name: 'level',
            minWidth: '200px'},
            {
            name: 'subject',
            minWidth: '70px'},
            {
            name: 'catid',
            minWidth: '70px'};

        }
        else
        {
            useThis = {
            name: 'level',
            minWidth: '200px'};

        }
var columns="["+useThis+"],";

How do I assign array correctly with if else statement?

Modify global variable array through a bash function, passing the function the name of the variable as a string

Dear I need something to help me in this. I'm trying to modify a global array through a function , using an auxiliary variable called "array". I would like to modify the global array "config " using a function that takes a string with the name of the variable " config".

I'm trying the following but I have not gotten results .

declare -A config

function testABC {
    array=${1}[@]
    array["key"]="value1"
    array["key2"]="value2"
}

testABC "config"
echo ${config["key"]}
echo ${config["key2"]}

#desired output:
#value1
#value2

My version of bash is 4.2.45

regards.

PHP Database Incremental Search using Array

I am learning coding, but I am wanting to make an interactive roster for a local youth camp where upon registering a new camper's personal information (in database is 'name', 'birth_year', 'hobbies', 'favorite_subject', 'spirit_animal_explanation' and 'contact_information') the camper can select a "group-me" button that will display the profiles of other campers who have similar hobbies and spirit animals as them as well as being within the same age group (11-13yrs, 14-16yrs,etc).

To achieve this, when a camper enters in his/her information, it is first saved into the database. I then query a SELECT syntax that takes the info from the 'hobbies' and 'spirit_animal_explanation' columns of the camper and filter it of unwanted words and put them in an array (so for 'hobbies' if the camper says "I like Running and Jumping" the the filter will array will only print_r Array([0]=>running[1]=>jumping).

I then array_merge the two filtered arrays into one so I easily manage the search (some campers like mixing up the hobbies into the spirit_animal_explanation so by having a single reference array for both columns the search can be easier) leaving me with a $compiled variable containing the two merged arrays.

Now the part(s) I am Struggling with: I am able to prepare the reference array for the search, but I don't know the query syntax to begin the search or scanning the 'hobbies' and 'spirit_animal_explanation' columns of every user using the reference array. From previous suggestions I've read up on JOIN queries but I don't see a point in making two tables because the elements of the reference array are not fixed/constant for every camper that executes the search. Also, I have no idea how/when I should add in the age conditional. Finally, the improve latency I want the system to look for the first 10 matching users on the first search session, then the next ten users on the second, etc. How can I go about doing this?

Python "numpy.dtype" structure for reading binary to "list" with "numpy.fromfile"

+++ WARNING, THE FOLLOWING CONTAINS VERY UGLY PROGRAMMING +++

+++ PLEASE HELP!!! +++

Hey, I am playing around quit a long time with my read in routines and I still not have figured out a good and fast way!

I have something like this: A huge binary file, which I want to slice down to a numpy-array!

I created this structure to read in fromfile a certain amount of bytes:

    mydt = numpy.dtype([
                       ('col1', np.uint64),
                       ('col2', np.int32),
                       ('cols3_56', np.float32, (53,))
                       ])

reading that like this:

data_block = numpy.fromfile(openfile, dtype=mydt, count=ntimes)

What I am getting out is something like this:

[(88000031189210L, 1, [-1000.0, -1000.0, -1000.0, -2.0, -2.0, -2.0, 65004000.0, 0.0, 760680000.0, 0.0, 0.12124349921941757, 0.04971266910433769, 2328.39990234375, 0.00013795999984722584, 0.0, 0.0, -1.0, -1.0, -1.0, 65004000.0, -1.0, 760680000.0, 0.0, 0.0, -1.0, 825680000.0, 0.0, -1.0, -1.0, -1.0, 157630.0, 0.0, 756310.0, 0.0, -1.0, -1.0, 0.0, 5.250500202178955, 0.0, 5.250500202178955, -13.602999687194824, -16.760000228881836, -17.283000946044922, -16.95800018310547, -17.513999938964844, -17.57200050354004, -13.657999992370605, -16.77199935913086, -17.291000366210938, -16.9689998626709, -17.520999908447266, -17.57200050354004, 1.0]), [(88......1L, 1, [-1000.0, ....]), ....

then I extend this datablock to my array

data_block_array.extend(data_block)

... and this million of times ....

I want now to access two things:

  • the 2th element in the above structure (in this example "1") for the entire data array which is a couple of millions times the above mentioned array
  • the 8th (in total the 12th) element in the 53-column data block for the entire array, again millions of substructures!

I figured that out with doing some loops over a count:

 i=0           
 while i<count:
     self.data_array[i,element1] = data_block_array[i][1]
     self.data_array[i,element8] = data_block_array[i][2][13]  

which is incredible slow ... I would like to develop a very fast and easy way to filter my data that way and extract the columns I am interested in. Appreciate some advise and insights!

Sending a dynamic range to a function

Newbie here, so please bear with me. I'm just not getting something about properly putting a dynamic column of values into an array.

The following lines of code work properly for me:

Set RangeD = Sheets("Data").Range("D2:D15")
unique2chan = UniqueItems(RangeD, False)

RangeD is the range I will pass to the function, called "UniqueItems". "unique2chan" is the array of values that will be returned by UniqueItems.

The assumption above is that the values always run from D2 to D15. But for these data, the range's length can vary on each run. (All values will be consecutive in the column, no spaces.)

Here's my question: How do I put a changing number of values into an array that I can pass to my function? I added a counter that tells me how many rows are filled in the column, so if there are 12 values in the column (plus the header), I have a counter that will read 13. How can I use that number to define a range of all the values (minus the header) that I can put into an array to pass to my function?

Any help is appreciated. I've tried a bunch of things I've found on the net, but everyone seems to have a different approach, and I haven't found one that worked for me.

XML parsing in a function and return data in an array list - java [on hold]

I need to read an xml in a method and use that data to drive selenium webdriver script. I am able to read the xml data in to a Arraylist and print the contents in the console. I am also returning ArrayList in the method. I am just struggling to use that returned data in my selenium method for different browser actions(type, click etc). This is my read method which is main method of my class

public static ArrayList main(String[] args) {

    ArrayList<String> testData = null;
    try {
        testData = new ArrayList<>();

        File fXmlFile = new File("C://Javaseleniumworld/Book.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://ift.tt/IJO9S6
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("firstname : " + eElement.getAttribute("id"));
                System.out.println("server : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                String FirstName = eElement.getElementsByTagName("firstname").item(0).getTextContent();
                testData.add(FirstName);
                System.out.println("server : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                String LastName = eElement.getElementsByTagName("lastname").item(0).getTextContent();
                testData.add(LastName);
                System.out.println("Array List" + testData);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return testData;
}