convert array collection to string. The array has excess information.

kadin
kadin used Ask the Experts™
on
This is a datagrid with two columns. I plan on having many columns. Each column input says add text here. When user clicks on that input,(add text here) gets replaced or updated with the user input. The columbs are editable, so if the user changes his mind the ArrayCollection gets updated.

I test this by inputing: sentence01 test. and sentence02 test. and click the button which converts the contents of the weatherInfo array object to a string that I can send to php.

The array is converted to a string and sent to php. PHP emails me the string and this is what I recieve:

nullD3315A22-768D-F239-D0D6-729798515A31, sentence01 test, F55EA87E-A1B9-B9DF-2EC1-7297993BDD51, sentence02 test,

When I use  mx.controls.Alert.show("weather info="+ weatherInfo);. It says the same thing.

The weatherInfo array object has this (D3315A22-768D-F239-D0D6-729798515A31) additional information.

How do I get a string into php with just the input values (sentence01 test, sentence02 test,)? Thanks.

<s:HTTPService method="POST" resultFormat="e4x">
  <s:request xmlns="">
  <str>{str}</str>
</s:HTTPService>

[Bindable] private var weatherInfo:ArrayCollection = new ArrayCollection
                ([{tex01:'add text here'}, {tex02:'add text here'}]);

[Bindable] private var str:String;

private function buttonHandler(event:Event):void {
  str = arrayCollectionToString(weatherInfo);
}

public static function arrayCollectionToString( weatherInfo:ArrayCollection ):String 
  { 
     var collStr:String;
     for each( var obj:Object in weatherInfo ) 
        { 
           for each( var obj2:Object in obj ) 
               { 
                   collStr += obj2.toString() + ", "; 
               } 
        } 
      return collStr; 
  }

<mx:DataGrid dataProvider="{weatherInfo}" editable="true">
        <mx:columns>
            <mx:DataGridColumn headerText="tex01" dataField="tex01" editable="true"/>
            <mx:DataGridColumn headerText="tex02" dataField="tex02" editable="true"/>
        </mx:columns>
</mx:DataGrid>

<s:Button id="btn" label="upload" click="buttonHandler(event)" enabled="true"/>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:

Author

Commented:
Thanks for responding.

Please forgive me if I am wrong.
I don't think that reference you gave addresses the question I am asking. I know how to use the http service component to send values to php and I know how receive values into flex from php for I have successfully done this. But this time around my values in flex are inside an ArrayCollection object which allows the values to be interactively edited by the user. There will be a varying number of values ranging from 1 to 20 depending on the users input. If I send this ArrayCollection object as is through http service to php; php can't read it. I must first convert the ArrayCollection object into a string of comma separated values and send to php to be processed.

When I do this with the arrayCollectionToString() I discover there is other information in the ArrayCollection object.

I would like to know the source cause of that other information and how to get rid of it, or some other solution like using ArrayList instead of ArrayCollection.
Top Expert 2011

Commented:
try ArrayCollection.source.join(",");
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thanks for that.

It says access to undefined property source.
I am having trouble defining the property source, thus I am unable to test it just yet.

Would you know how to do that?

I have tried:
var source:Array;    source = ArrayCollection;

source = weatherInfo:ArrayCollection;

public function set source(weatherInfo:ArrayCollection):void {
                        
                  }
Top Expert 2011

Commented:
No, you should in ArrayCollection get source

Example

var ac:ArrayCollection = new ArrayCollection([1,2,3,4,5,6,7,8,9,0]);
var source:Array = ac.source;
trace(source.join(", ");

Author

Commented:
I tried this and the error went away.

weatherInfo.source.join(",");

I tested it  Alert.show("weatherinfo="+weatherInfo);
It shows objects, but no string.
Top Expert 2011

Commented:
you have to try

Alert.show("weatherinfo="+weatherInfo.source.join(","));

Author

Commented:
var source:Array = weatherInfo.source;
weatherInfo.source.join(",");
mx.controls.Alert.show("weatherinfo="+weatherInfo.source.join(","));

This is what I get.
weatherinfo=[object Object],[object Object]
Top Expert 2011

Commented:
in this case you have array of Objects. That is valid case. Do you know what keys in your array elements?

Read how to traverse array

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#forEach%28%29

Author

Commented:
By keys do you mean text01: and text02: below?

[Bindable] private var weatherInfo:ArrayCollection = new ArrayCollection
                ([{tex01:'add text here'}, {tex02:'add text here'}]);
Top Expert 2011

Commented:
Yes, create function with will return your object something like this


function toObj(key:String, value:String):Object{
    var o:Object = {};
      o[key] = value;
    o.toString = function():String {
         return "{" + key + ":" + value + "}";
    };
      return o;
}

var array:Array = [toObj('tex01', 'add text here'), toObj('tex02', 'add text here')];

[Bindable] private var weatherInfo:ArrayCollection = new ArrayCollection(array);

trace(weatherInfo);

Author

Commented:
Was I supposed to put weatherInfo.source.join(","); inside a for each to join it all into a string? Because I didn't.

In the beginning I used this and was getting extra unwanted info. I disabled this while you have been helping me.

public static function arrayCollectionToString( weatherInfo:ArrayCollection ):String
  {
     var collStr:String;
     for each( var obj:Object in weatherInfo )
        {
           for each( var obj2:Object in obj )
               {
                   collStr += obj2.toString() + ", ";
               }
        }
      return collStr;
  }
Top Expert 2011

Commented:
I didn't test but should work like this:

public static function arrayCollectionToString( weatherInfo:ArrayCollection ):String
  {
     var array:Array = [];
     for(var i:uint = 0; i < weatherInfo.length; i++)
     {
            var obj:Object = weatherInfo.getItemAt(i);
            for (var key:String in obj)
            {
                   array.push("{" + key + ":" + obj[key] + "}");
            }
        }
      return array.join(",");
  }

Author

Commented:
I tried your code.
I think there are clues in the alert output.

weatherInfo={tex01:text},{mx_internal_uid:164879B0-D87D-0B20-7ACA-7795E80F4B3F},{mx_internal_uid:C9979691-80C4-F397-C4FB-7795E8F95614},{tex02:text}
Top Expert 2011
Commented:
OK, check if key is valid

           
             for (var key:String in obj)
            {
                  if(key != "mx_internal_uid")
                          array.push("{" + key + ":" + obj[key] + "}");
            }

Author

Commented:
The alert output:

weatherinfo={tex01:text},{tex02:text}
Top Expert 2011

Commented:
that what you want

Author

Commented:
That is. I will have to learn about mx_internal_uid.

Thanks for sticking with me on this.
Top Expert 2011

Commented:
"mx_internal_uid" auto generating by ArrayCollection for serialization objects. If you don't want to see it do not use Array collection use ArrayList or Array

Author

Commented:
I will look into array list or try to make arraycollection work as you did. So long as I can work with it in php.

Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial