Page 1 of 1
Convert "Array" to comma delimited list
Posted: Sat Nov 28, 2015 2:35 am
by newpie
Hello, I have a php pdo script that gives me an format posted below. I know the order of the keys, but I am trying to get get in a list that I can analyze with item 1, item 2 and so on. Would nice to have a function. The Array may expand with keys, but I will know the key order still.
In the below example, with two keys, my goal would to be have a result of : John,Manager
Code: Select all
Array
(
[0] => Array
(
[Name] => John
)
[1] => Array
(
[Position] => Manager
)
)
I appreciate any help in this matter. Happy Holidays as well.
Re: Convert "Array" to comma delimited list
Posted: Sat Nov 28, 2015 3:36 am
by sritcp
Hi newpie:
Here you go:
Code: Select all
-- Create an example array
put "John" into tArray[1]["Name"]
put "Manager" into tArray[1]["Position"]
put "Jane" into tArray[2]["Name"]
put "Accountant" into tArray[2]["Position"]
put "Joan" into tArray[3]["Name"]
put "Counsel" into tArray[3]["Position"]
set the rowDelimiter to comma
repeat with i = 1 to 3
combine tArray[i] by row
end repeat
set the rowDelimiter to return
combine tArray by row
put tArray
Result:
John,Manager
Jane,Accountant
Joan,Counsel
Happy Thanksgiving,
Sri
Note: This gives you a general method to explore. If you have a number of keys, your particular set of keys may not sort naturally as in the above examples. A simple solution is to (temporarily) replace your keys with integers (1,2,....) so they will be displayed in order.
Re: Convert "Array" to comma delimited list
Posted: Sat Nov 28, 2015 3:53 am
by newpie
Thanks sritcp, I appreciate the quick reply. I tried the below piece of code on example in my first post and it seems the "array" I am given from php pdo is in a different format and the livecode script returned nothing for tArray I am afraid.
Code: Select all
set the rowDelimiter to comma
repeat with i = 1 to 3
combine tArray[i] by row
end repeat
set the rowDelimiter to return
combine tArray by row
put tArray
Is there a way to parse out the data from below to perhaps just grab everything after "=>" ignoring the words "Array"?
Code: Select all
Array
(
[0] => Array
(
[Name] => John
)
[1] => Array
(
[Position] => Manager
)
)
Re: Convert "Array" to comma delimited list
Posted: Sat Nov 28, 2015 3:12 pm
by sritcp
Hi newpie:
Sorry, I don't know anything about php's output format.
Regards,
Sri
Re: Convert "Array" to comma delimited list
Posted: Sat Nov 28, 2015 7:57 pm
by newpie
Thanks sritcp for trying, I researched the forum further and came up with this function using regex and MatchChunk. Hopefully it can help someone out there dealing with print arrays from PHP. Thanks
Code: Select all
on mouseUp
put PHPArrayParser (field "text") into tAnswer
answer word 2 of tAnswer --this is an example of retrieving 2nd word to analyze further
end mouseUp
function PHPArrayParser Chunk
local tVar, R
repeat for each line tLine in Chunk -- process the lines
put word 1 of tLine into tVar
get MatchChunk( tVar, "\[.[a-zA-Z]+\]$") --this will return true or false if the line begins with this format ["letters"]
if it is true then
put word 3 of tLine & tab after R
end if
end repeat
return R
end PHPArrayParser
Edit:
Added the links that help me to the above conclusion:
http://forums.livecode.com/viewtopic.php?f=7&t=10708
http://forums.livecode.com/phpBB2/viewtopic.php?t=3094
http://forums.livecode.com/phpBB2/viewt ... =7&t=25729
Re: Convert "Array" to comma delimited list
Posted: Sat Nov 28, 2015 8:03 pm
by bn
Hi newpi,
could you please add a link to the thread where you found the script?
Thank you
Bernd
Re: Convert "Array" to comma delimited list
Posted: Sat Nov 28, 2015 8:15 pm
by newpie
Hey bn, I posted some links which help me make this script, it is not a direct cut and paste. Hope it helps. Thanks