Posts Tagged ‘array’

stdClass Object problems

Monday, August 23rd, 2010

I’m struggling to parse the below data using PHP. An API returns it, and I’ve tried various syntaxes. How do I return the data in a non-object way? Or, what’s the syntax to call the data using the stdClass?

Could I convert this to one data based array, or even two? I’m lost when it comes to object-based data sets.

stdClass Object
(
    [0] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 29
                )

        )

    [1] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 26
                )

        )

)

When i create the object on my own, and then do a var_dump, I get this:

object(stdClass)#8 (2) {
  [0]=>
  object(stdClass)#4 (1) {
    ["district"]=>
    object(stdClass)#5 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "29"
    }
  }
  [1]=>
  object(stdClass)#6 (1) {
    ["district"]=>
    object(stdClass)#7 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "26"
    }
  }
}

stdClass Object problems

Monday, August 23rd, 2010

I’m struggling to parse the below data using PHP. An API returns it, and I’ve tried various syntaxes. How do I return the data in a non-object way? Or, what’s the syntax to call the data using the stdClass?

Could I convert this to one data based array, or even two? I’m lost when it comes to object-based data sets.

stdClass Object
(
    [0] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 29
                )

        )

    [1] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 26
                )

        )

)

When i create the object on my own, and then do a var_dump, I get this:

object(stdClass)#8 (2) {
  [0]=>
  object(stdClass)#4 (1) {
    ["district"]=>
    object(stdClass)#5 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "29"
    }
  }
  [1]=>
  object(stdClass)#6 (1) {
    ["district"]=>
    object(stdClass)#7 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "26"
    }
  }
}

Question about FilterFunction of ArrayCollection

Monday, August 23rd, 2010

Hi,

I want to filter ArrayCollection based on multiple values ( or array ). For example, My sample array “myArray” has four values; Active, Close,Returned and Submitted.

My code looks like,

sampleData.filterFunction =doStatusFilter
sampleData.refresh();

public function doStatusFilter(item:Object):Boolean {
    var i:int;
    var query:String;
    var sep:String
    query="(";
    sep=""

    for (i=0; i<myArray.length; i++) {
           query=query+sep+(item.status + "=='"+ myArray[i]+"'");
       sep=" || ";
    }
    query=query+")";

    //Here is my problem..Since return type is boolean
return query
  }

I can build query successfully but how do I execute “query” ?

I need to filter my collection based on multiple status.

How can I order an array by the amount of times each object appears in a join table in RoR?

Monday, August 23rd, 2010

I’m doing a search on a model, like this:

search_results = Note.description_like_all("test string")

So I’ve got an array of notes. I want to order them by the frequency that the note_id appears in a join table I have, note_categories. How can I do this?

No adapter for handler in Spring MVC

Monday, August 23rd, 2010

Hi, I have problem with simple spring mvc controller written in scala:

@Controller
class HelloWorldController {

 implicit def sessionFactory2Session(sf: SessionFactory) = sf.getCurrentSes
 @Autowired
  var sessionFactory: SessionFactory = null

 @Transactional
 @RequestMapping(value=Array("/hello.html"),method = Array(RequestMethod.GET,RequestMethod.POST))
 def showHello  = {

   val document = new CustomDocument("name","custom")
   sessionFactory.save(document)
   sessionFactory.flush

   "helloPage"
  }
}

When I tried to access /hello.html I received exception:

javax.servlet.ServletException: No adapter for handler: Does your handler implement a supported interface like Controller?
at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:951)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:758)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)

But when I removed @Transactional annotation – everything works!!. Spring can’t find request mapping with two annotations? My applicationContext.xml fragment:

 <bean id="openSessionInViewInterceptor"
       class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
     <property name="sessionFactory" ref="sessionFactory"/>
 </bean>

 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
     <property name="interceptors">
         <list><ref bean="openSessionInViewInterceptor"/></list>
     </property>
     <property name="alwaysUseFullPath" value="true" />
 </bean>
<context:component-scan base-package="scala.hibernate"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
 </bean>

 <!-- for transaction -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManaager">
      <property name="sessionFactory" ref="sessionFactory"/>
      <property name="dataSource" ref="dataSource"/>
 </bean>
 <tx:annotation-driven/>

passing arrays to functions in tcl. Only upvar ?

Monday, August 23rd, 2010

As far as I understand, in tcl if you want to pass a named array to a function, you have to access the upper scope of the caller via the upvar command within the callee body. Is this the only way to pass an array in tcl ?

dynamically creating bind_result

Sunday, August 22nd, 2010
In a pagination class, I use it for multiple query sets. The bind_result code as follows works fine to populate an array with the result set.

$prep1->bind_result($venueid, $name, $address, $city, $state, $zipcode);
$x = 0;
while ($prep1->fetch())
{
$users[$x][0] = $venueid;
$users[$x][1] = $name;
$users[$x][2] = $address;
$users[$x][3] = $city;
$users[$x][4] = $state;
$users[$x][5] = $zipcode;
$x++;
}

What I would like to be able to do is pass in an array of field names and dynamically create the bind_result statement (which I would assume can be done using call_user_func_array with an array of field names starting with $. But I still need to reference those names in the while statement to populate my array. Even with $$ all I get is a literal so it is interpreting it as a string, not a field name from the result set.

It is my understanding that in order to retrieve the result set from a prepared statement I need to bind column names and then do a fetch, so I am not allowed to process the results with a numeric array, is that correct?

Best practice for de-serializing nested JSON objects?

Sunday, August 22nd, 2010

I have a PHP back-end that produces a JSON string containing several nested objects, based on the following hierarchy:

Inventory contains Categories
Categories contain Varieties

Now, I can deserialize the string in my Javascript front-end quite fine, into an object hierarchy.

inventory = new Object {categories: new Array({categoryName: "Name One",...

However, I want each object in the hierarchy to have functionality. Say, I want to be able to call

var inventory = jsonInventoryString.evalJSON();
var cat = inventory.categories[0];
var valuesSum = cat.addVarietyValues();

How should I go about attaching functions to my deserialised inventory?

My current implementation consists of writing Inventory(), Category(), Variety() functions, attaching prototype methods to them… And manually deserializing my JSON object.

Inventory.prototype.parseJSON = function (json){
    var jsonInventory = json.evalJSON();
    jsonInventory.categories.each(function (item) {
        var category = new Category(item.name);
        category.parseJSONObject(item);
        this.categories.push(category);
    }.bind(this));
}

Is there a better way to do this?

Accessing later index in array using enumerate(array) Python

Sunday, August 22nd, 2010

hey guys, how would you access an array from array[n] in an array of 100 floats in this for loop (i need the enumerate):

for index,value in enumerate(array):
    #do stuff with array[n]
    n=n+1

im trying to make it so that it operates in a smaller and smaller space each iteration..

thanks

Only getting the first character when accessing post variable

Sunday, August 22nd, 2010

A common way I handle things in Drupal is to create a CCK type, and then create a custom submit handler to perform logic on the CCK fields when the node is submitted. However, I have been unable to get the value when the CCK field type is a Date field because the array is structured differently.

Here I assign the portion of the $form array I want.

 //This is in the submit handler
  $fromDatePRE = array();
  $fromDatePRE[] =  $form['field_bill_start'][0]['value']['date'];
  $fromDate = ['0']['#value']['#date'];

$fromDate is printing the first character “S” of Sat 8/21/2010 which is the value I want , but I want the whole value. I know this has something do with how I’m accessing the array but I can’t figure it out.

This is what I’m working with – I added a comment to the value I’m trying to access:

drupal_set_message('<pre>'. print_r($fromDatePRE, TRUE) .'</pre>');

Which Yiedls:

 Array
    (
        [0] => Array
            (
                [#type] => textfield
                [#default_value] => Sat, 08/21/2010
                [#id] => edit-field-bill-start-0-value-datepicker-popup-0
                [#size] => 20
                [#maxlength] => 30
                [#attributes] => Array
                    (
                    )

                [#description] =>  Format: Sat, 08/21/2010
                [#post] => Array
                    (
                        [title] => llllllllllllll
                        [menu] => Array
                            (
                                [link_title] =>
                                [parent] => Array
                                    (
                                        [hierarchical_select] => Array
                                            (
                                                [selects] => Array
                                                    (
                                                        [0] => primary-links:0
                                                        [1] => label_1
                                                    )

                                            )

                                    )

                                [weight] => 0
                            )

                        [hs_form_build_id] => hs_form_7bf97c3c59ba2a064d6e841ae405dd30
                        [changed] =>
                        [form_build_id] => form-1f928faaa990f3809da391165d51981a
                        [form_token] => 616f6e8a7bb9c4ffc2aec65174c0817d
                        [form_id] => billing_node_form
                        [log] =>
                        [pathauto_perform_alias] => 1
                        [print_display] => 1
                        [print_display_urllist] => 1
                        [print_mail_display] => 1
                        [print_mail_display_urllist] => 1
                        [print_pdf_display] => 1
                        [print_pdf_display_urllist] => 1
                        [field_bill_start] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => Array
                                            (
                                                //This is the value I need
                                                [date] => Sat, 08/21/2010
                                            )

                                    )

                            )

                        [field_bill_end] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => Array
                                            (
                                                [date] => 08/25/2010
                                            )

                                    )

                            )

                        [name] => administrator
                        [date] =>
                        [status] => 1
                        [promote] => 1
                        [op] => Save
                    )

                [#programmed] =>
                [#tree] => 1
                [#parents] => Array
                    (
                        [0] => field_bill_start
                        [1] => 0
                        [2] => value
                        [3] => date
                    )

                [#array_parents] => Array
                    (
                        [0] => field_bill_start
                        [1] => 0
                        [2] => value
                        [3] => date
                    )

                [#weight] => 0
                [#processed] => 1
                [#required] =>
                [#input] => 1
                [#autocomplete_path] =>
                [#process] => Array
                    (
                        [0] => form_expand_ahah
                    )

                [#name] => field_bill_start[0][value][date]
                [#value] => Sat, 08/21/2010
                [#needs_validation] => 1
                [#defaults_loaded] => 1
                [#sorted] => 1
            )

    )