Avatar of kadin
kadin
Flag for United States of America asked on

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

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

Apache Flex

Avatar of undefined
Last Comment
kadin

8/22/2022 - Mon
dgofman

kadin

ASKER
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.
dgofman

try ArrayCollection.source.join(",");
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
dgofman

kadin

ASKER
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 {
                        
                  }
dgofman

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(", ");
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kadin

ASKER
I tried this and the error went away.

weatherInfo.source.join(",");

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

you have to try

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

ASKER
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]
Your help has saved me hundreds of hours of internet surfing.
fblack61
dgofman

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
kadin

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

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

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);
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kadin

ASKER
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;
  }
dgofman

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(",");
  }
kadin

ASKER
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}
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
kadin

ASKER
ASKER CERTIFIED SOLUTION
dgofman

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
kadin

ASKER
The alert output:

weatherinfo={tex01:text},{tex02:text}
dgofman

that what you want
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kadin

ASKER
That is. I will have to learn about mx_internal_uid.

Thanks for sticking with me on this.
dgofman

"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
kadin

ASKER
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck