I have an XML feed that is automatically generated on one site and I'm trying to copy it over to another and import the data into a mysql db, but, I'm having difficulty as I'm not familiar with arrays broken down into multiple elements.
Does it get processed the same way a standard array would or does each have to be processed separately? Outputted XML example is included below;
[str] => Array
(
[0] => 0
[1] => USD
[2] => USPS
[3] => 4228547
[4] => 486948677
[5] => 7
[6] => IndyGen3
[7] => 1
[8] => 8 units|8
[9] => N/A
[10] => 1
[11] => Unlimited Refill
[12] => Unlimited Refill
[13] => www
[14] => 1297081
[15] => unitACTIVE4228547
[16] => 2
[17] => 0
[18] => unit-486948677
[19] => unit
[20] => ACTIVE
[21] => unit
[22] => 1
[23] => 1
[24] => 47d 23h 24m
[25] => 2013-02-15T19:35:15.153Z
)
[float] => Array
(
[0] => 94.88
[1] => 94.88
)
[date] => Array
(
[0] => 2013-02-15T19:35:15.438Z
[1] => 2013-02-15T19:33:14Z
[2] => 2013-02-15T19:35:02Z
[3] => 2013-04-04T19:00:00Z
)
[arr] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => fm_ship_rules
)
[str] => Array
(
[0] => 6,3,25.0,,2013-04-02T19:00:00Z
[1] => 6,7,11.95,,2013-03-22T19:00:00Z
[2] => 6,15,19.95,,2013-04-02T19:00:00Z
[3] => 6,16,19.95,,2013-04-02T19:00:00Z
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => fm_dm_rules_desc
)
[str] => Array
(
[0] => USPS,USPS Priority, 25.0,,2013-04-02T19:00:00Z
[1] => USPS,USPS Two Day,11.95,,2013-03-22T19:00:00Z
[2] => USPS,USPS International Priority Puerto Rico,19.95,,2013-04-02T19:00:00Z
[3] => USPS,USPS International Priority Canada,19.95,,2013-04-02T19:00:00Z
)
)
)
[int] => Array
(
[0] => 8
[1] => 8
[2] => 1
)
)
To access the information in multidimensional array like the one you have above you would use the following notation. Let's say your main array is called $arr.
$sql = "INSERT INTO `mytable`
(
`currency`,
`postal_service`,
`date`,
`amount_1`,
`amount_2`
) VALUES (
'".$arr[str][1]."',
'".$arr[str][2]."',
'".$arr[date][0]."',
'".$arr[float][0]."',
'".$arr[float][1]."'
)";
This is the equivalent of:
$sql = "INSERT INTO `mytable`
(
`currency`,
`postal_service`,
`date`,
`amount_1`,
`amount_2`
) VALUES (
'USD',
'USPS',
'2013-02-15T19:35:15.438Z',
'94.88',
'94.88'
)";
I have the following string which I need to extract the full name plus the URI/URL associated with it into an array for each one. It's not constructed like typical data, so I am unsure how to proceed. I thought explode at first, but there is "garbage" data in the beginning not needed, so I need a way to maybe cleanse the data first? But maybe there is an efficient way of doing this in one or two steps/stages so my code is efficient? After some research I am thinking mb_split() but I am very poor at constructing regex. Need some direction here...
In my example below, you can see the data pattern that emerges after the initial "junk". The 3rd "row" for each grouping that forms is where the meat (aka data) that I seek is. The first string in that first grouping, in the 3rd row in the example is "Amanda Grider". This is the full name and its position in the returned data example I am looking for from each grouping. Further along that row is the URI/URL for the jpg image, which is the second piece of data I seek. Everything else as far as I am concerned is garbage and can get tossed.
What is the fastest, most efficient way to process this chunk and get the values I seek into an array so i can use this in my work?
(BTW, the following code I hand added return characters to make the data more easily understood. In reality, there are no return characters and it presents as one big string)
[
[
"tsg.lac",
[],
[
[
[null,null,"100829745667958569941"],
[],
["Amanda Grider",null,null,null,"4b3347c83f0a1","8nwbFHob02C8CmojHF","BoZrAHx801Rz8o3h8k",null,"https://lh3.googleusercontent.com/-zIK8ZN_ZDt8/AAAAAAAAAAI/AAAAAAAAAAA/fsiR92bLDlU/photo.jpg",null,1,"Marina del Rey, CA",null,null,null,0,null,[],null,null,null,""],
[]
],[
[null,null,"115014076410206782853"],
[],
["VWvortex",null,null,null,"4b13c6667b3c9","JKCGFo_CApJ","JKCGFo_CApJ",null,"//lh6.googleusercontent.com/-X_wSt8nwpOU/AAAAAAAAAAI/AAAAAAAAACQ/R_jcIPcegbM/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://WWW.VWVORTEX.COM",null,null,3],null,null,"World's largest Volkswagen enthusiast community and blog."],
[]
],[
[null,null,"102608018926739248428"],
[],
["Wale",null,null,null,"4b1ded89a3721","JmRxAk","JmRxAk",null,"//lh4.googleusercontent.com/-xyeyjc4Avow/AAAAAAAAAAI/AAAAAAAAABU/SY-9EKeDnhw/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://www.ralphfolarin.com/",null,null,6],null,null,""],
[]
],[
[null,null,"114161985228080012446"],
[],
["The Opus Rhythm Music Blog",null,null,null,"4b177a5207d09","IIJj03C4Iog3HIKMIIJz02xEHnRf01ZxFnB","IIJj03C4Iog3HIKMIIJz02xEHnRf01ZxFnB",null,"//lh5.googleusercontent.com/-4QRl1IgDCLU/AAAAAAAAAAI/AAAAAAAAABI/pVoxTQ7SH8Y/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://www.bacchusentertainment.com",null,null,6],null,null,"We are the team music blog of Bacchus Entertainment"],
[]
],[
[null,null,"114645267718535118440"],
[],
["Jalopnik",null,null,null,"4b12fccb6f809","DHRxFoK0Cng","DHRxFoK0Cng",null,"//lh6.googleusercontent.com/-_M1nn9mKyY8/AAAAAAAAAAI/AAAAAAAAABI/aXhkyN7cuuk/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://jalopnik.com/",null,null,3],null,null,"Jalopnik: Drive Free or Die"],
[]
],[
[null,null,"105503202599719238167"],
[],
["Audi USA",null,null,null,"4b14db7535e99","8owhCkGEHmR","8owhCkGEHmR",null,"//lh3.googleusercontent.com/-mHHyVhWfARE/AAAAAAAAAAI/AAAAAAAAAC4/Qn0lYbilT8M/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://www.audiusa.com","(800) 822-2834",null,3],null,null,"Progress is social media, and listening, and fans, and Google+. So here we are."],
[]
],[
[null,null,"104108787932235341403"],
[],
["Audi Sport",null,null,null,"4b23243c864b1","8owhCkGAGJC8IF","8owhCkGAGJC8IF",null,"//lh4.googleusercontent.com/-jGBNL9dbwYs/AAAAAAAAAAI/AAAAAAAAAUA/pgsAqvaX8XM/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://www.facebook.com/AudiSportPage",null,null,6],null,null,"Unofficial Audi Sport fan page, not affiliated with or endorsed by Audi AG."],
[]
],[
[null,null,"106689856342933829975"],
[],
["Volkswagen USA",null,null,null,"4b20ca9b7fa69","JJBxDohI8nBjFFGEHmR","JJBxDohI8nBjFFGEHmR",null,"//lh5.googleusercontent.com/-i3MO9CsymQ8/AAAAAAAAAAI/AAAAAAAAAB4/ddmTW3D8s20/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://www.vw.com","(800) 822-8987",null,3],null,null,"Take a look around, kick the tires, ask questions and get to know our community."],
[]
],[
[null,null,"115425298803319911308"],
[],
["Internal Frequency",null,null,null,"4b177b6d46119","Co4CAo_08no3BJZjGowjFHhM","Co4CAo_08no3BJZjGowjFHhM",null,"//lh4.googleusercontent.com/-lZeecuGL3Ig/AAAAAAAAAAI/AAAAAAAAABk/Afv5eGuBzUM/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://www.internalfrequency.com",null,null,6],null,null,"The 1st hand ups-and-downs of the CEO of an up-and-coming entertainment label in Southern California"],
[]
],[
[null,null,"101358795463286919640"],
[],
["Music Think Tank",null,null,null,"4b1947fea8251","EoxACmg3IIJrFIg3IHS0Dk","EoxACmg3IIJrFIg3IHS0Dk",null,"//lh4.googleusercontent.com/-B2KTfl4uNyE/AAAAAAAAAAI/AAAAAAAAACM/N955ZhPV08E/photo.jpg",null,1,null,null,null,null,0,null,[],[null,"http://www.musicthinktank.com",null,null,6],null,null,"Where the music industry speaks out loud. Create the Chaos."],
[]
]
]
]
]
UPDATE:
So I stumbled on something and discovered the data is in fact valid JSON, does get decoded and passed back but still something odd and seems very complex (too complex for what I need). I use json_decode() to prase the data, which then is assigned to the variable $jsondata. When I added the following immediately after that:
print_r ( print_r($jsondata));
I got this (I added return characters so it makes more sense and can be easily read):
Array (
[0] => Array (
[0] => tsg.lac [1] => Array () [2] => Array (
[0] => Array (
[0] => Array (
[0] => [1] => [2] => 100829745667958569941 )
[1] => Array ( )
[2] => Array (
[0] => Amanda Grider [1] => [2] => [3] => [4] => 4b33843806e03 [5] => 8nwbFHob02C8CmojHF [6] => BoZrAHx801Rz8o3h8k [7] => [8] => https://lh3.googleusercontent.com/-zIK8ZN_ZDt8/AAAAAAAAAAI/AAAAAAAAAAA/fsiR92bLDlU/photo.jpg [9] => [10] => 1 [11] => Marina del Rey, CA [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => [19] => [20] => [21] => )
[3] => Array ( )
)
[1] => Array (
[0] => Array (
[0] => [1] => [2] => 115014076410206782853 )
[1] => Array ( )
[2] => Array (
[0] => VWvortex [1] => [2] => [3] => [4] => 4b13c6667b3c9 [5] => JKCGFo_CApJ [6] => JKCGFo_CApJ [7] => [8] => //lh6.googleusercontent.com/-X_wSt8nwpOU/AAAAAAAAAAI/AAAAAAAAACQ/R_jcIPcegbM/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://WWW.VWVORTEX.COM [2] => [3] => [4] => 3 ) [19] => [20] => [21] => World's largest Volkswagen enthusiast community and blog. )
[3] => Array ( )
)
[2] => Array (
[0] => Array (
[0] => [1] => [2] => 102608018926739248428 )
[1] => Array ( )
[2] => Array (
[0] => Wale [1] => [2] => [3] => [4] => 4b1ded89a3721 [5] => JmRxAk [6] => JmRxAk [7] => [8] => //lh4.googleusercontent.com/-xyeyjc4Avow/AAAAAAAAAAI/AAAAAAAAABU/SY-9EKeDnhw/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://www.ralphfolarin.com/ [2] => [3] => [4] => 6 ) [19] => [20] => [21] => )
[3] => Array ( )
)
[3] => Array (
[0] => Array (
[0] => [1] => [2] => 114161985228080012446 )
[1] => Array ( )
[2] => Array (
[0] => The Opus Rhythm Music Blog [1] => [2] => [3] => [4] => 4b177a5207d09 [5] => IIJj03C4Iog3HIKMIIJz02xEHnRf01ZxFnB [6] => IIJj03C4Iog3HIKMIIJz02xEHnRf01ZxFnB [7] => [8] => //lh5.googleusercontent.com/-4QRl1IgDCLU/AAAAAAAAAAI/AAAAAAAAABI/pVoxTQ7SH8Y/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://www.bacchusentertainment.com [2] => [3] => [4] => 6 ) [19] => [20] => [21] => We are the team music blog of Bacchus Entertainment )
[3] => Array ( )
)
[4] => Array (
[0] => Array (
[0] => [1] => [2] => 114645267718535118440 )
[1] => Array ( )
[2] => Array (
[0] => Jalopnik [1] => [2] => [3] => [4] => 4b12fccb6f809 [5] => DHRxFoK0Cng [6] => DHRxFoK0Cng [7] => [8] => //lh6.googleusercontent.com/-_M1nn9mKyY8/AAAAAAAAAAI/AAAAAAAAABI/aXhkyN7cuuk/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://jalopnik.com/ [2] => [3] => [4] => 3 ) [19] => [20] => [21] => Jalopnik: Drive Free or Die )
[3] => Array ( )
)
[5] => Array (
[0] => Array (
[0] => [1] => [2] => 105503202599719238167 )
[1] => Array ( )
[2] => Array (
[0] => Audi USA [1] => [2] => [3] => [4] => 4b14db7535e99 [5] => 8owhCkGEHmR [6] => 8owhCkGEHmR [7] => [8] => //lh3.googleusercontent.com/-mHHyVhWfARE/AAAAAAAAAAI/AAAAAAAAAC4/Qn0lYbilT8M/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://www.audiusa.com [2] => (800) 822-2834 [3] => [4] => 3 ) [19] => [20] => [21] => Progress is social media, and listening, and fans, and Google+. So here we are. )
[3] => Array ( )
)
[6] => Array (
[0] => Array (
[0] => [1] => [2] => 104108787932235341403 )
[1] => Array ( )
[2] => Array (
[0] => Audi Sport [1] => [2] => [3] => [4] => 4b23243c864b1 [5] => 8owhCkGAGJC8IF [6] => 8owhCkGAGJC8IF [7] => [8] => //lh4.googleusercontent.com/-jGBNL9dbwYs/AAAAAAAAAAI/AAAAAAAAAUA/pgsAqvaX8XM/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://www.facebook.com/AudiSportPage [2] => [3] => [4] => 6 ) [19] => [20] => [21] => Unofficial Audi Sport fan page, not affiliated with or endorsed by Audi AG. )
[3] => Array ( )
)
[7] => Array (
[0] => Array (
[0] => [1] => [2] => 106689856342933829975 )
[1] => Array ( )
[2] => Array (
[0] => Volkswagen USA [1] => [2] => [3] => [4] => 4b20ca9b7fa69 [5] => JJBxDohI8nBjFFGEHmR [6] => JJBxDohI8nBjFFGEHmR [7] => [8] => //lh5.googleusercontent.com/-i3MO9CsymQ8/AAAAAAAAAAI/AAAAAAAAAB4/ddmTW3D8s20/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://www.vw.com [2] => (800) 822-8987 [3] => [4] => 3 ) [19] => [20] => [21] => Take a look around, kick the tires, ask questions and get to know our community. )
[3] => Array ( )
)
[8] => Array (
[0] => Array (
[0] => [1] => [2] => 115425298803319911308 )
[1] => Array ( )
[2] => Array (
[0] => Internal Frequency [1] => [2] => [3] => [4] => 4b177b6d46119 [5] => Co4CAo_08no3BJZjGowjFHhM [6] => Co4CAo_08no3BJZjGowjFHhM [7] => [8] => //lh4.googleusercontent.com/-lZeecuGL3Ig/AAAAAAAAAAI/AAAAAAAAABk/Afv5eGuBzUM/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://www.internalfrequency.com [2] => [3] => [4] => 6 ) [19] => [20] => [21] => The 1st hand ups-and-downs of the CEO of an up-and-coming entertainment label in Southern California )
[3] => Array ( )
)
[9] => Array (
[0] => Array (
[0] => [1] => [2] => 101358795463286919640 )
[1] => Array ( )
[2] => Array (
[0] => Music Think Tank [1] => [2] => [3] => [4] => 4b1947fea8251 [5] => EoxACmg3IIJrFIg3IHS0Dk [6] => EoxACmg3IIJrFIg3IHS0Dk [7] => [8] => //lh4.googleusercontent.com/-B2KTfl4uNyE/AAAAAAAAAAI/AAAAAAAAACM/N955ZhPV08E/photo.jpg [9] => [10] => 1 [11] => [12] => [13] => [14] => [15] => 0 [16] => [17] => Array ( ) [18] => Array ( [0] => [1] => http://www.musicthinktank.com [2] => [3] => [4] => 6 ) [19] => [20] => [21] => Where the music industry speaks out loud. Create the Chaos. )
[3] => Array ( )
)
)
)
) 1
Now that is ALOT of arrays! Not only that, I only require the super nested array [2] where the name starts and of that values [0] & [8] only! Then I get an error when this next line (below) runs but I am less concerned with that, I want to know how can I trim down this data so it doesn't become such a memory hog....
$visiblepeople = $jsondata[2];
To me this looks like JSON ( http://www.json.org ), and thus, you can use any JSON implementation to parse it. For Python, there's a module called ... json ;-) See http://docs.python.org/library/json.html.