How to sort an array of items as numbers? - php
I have a number of files I am loading into a slideshow, I have them numbered by the client in the order that they'd like to be displayed.
I'm grabbing them with glob $pics = glob("images/slideshow/*.jpg", GLOB_NOSORT); but for some reason I'm getting the old Windows sorting bug, of 11 being sorted above 2.
For example,
Array
(
[0] => images/slideshow/1.jpg
[1] => images/slideshow/14.jpg
[2] => images/slideshow/15.jpg
[3] => images/slideshow/16.jpg
[4] => images/slideshow/18.jpg
[5] => images/slideshow/2.jpg
[6] => images/slideshow/20.jpg
[7] => images/slideshow/21.jpg
[8] => images/slideshow/22.jpg
[9] => images/slideshow/23.jpg
[10] => images/slideshow/24a.jpg
[11] => images/slideshow/25.jpg
[12] => images/slideshow/26.jpg
[13] => images/slideshow/29.jpg
[14] => images/slideshow/3.jpg
[15] => images/slideshow/36.jpg
[16] => images/slideshow/38.jpg
[17] => images/slideshow/4.jpg
[18] => images/slideshow/40.jpg
[19] => images/slideshow/41.jpg
[20] => images/slideshow/5.jpg
[21] => images/slideshow/6.jpg
[22] => images/slideshow/7.jpg
)
I've run it through asort() and I can't seem to find out why this isn't working, short of it being something about them being strings, rather than strictly numbers.
Check out natsort:
This function implements a sort algorithm that orders alphanumeric
strings in the way a human being would while maintaining key/value
associations.
$array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
print_r($array2);
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)
Related
PHP - How would one go about combining 3 different arrays into one
So i have 3 different ( yet they share some similarities ) and i would like to combine these into one to perform some calculations Array ( [0] => Array ( [0] => Agent [1] => Answered [2] => Missed [3] => Contribution [4] => Per Hour [5] => Total Ring Time [6] => Mean Ring Time [7] => Total Talk Time [8] => Mean Talk Time [9] => Total Wrap Time [10] => Mean Wrap Time [11] => Total Session Time [12] => Mean Session Time [13] => Number of Sessions [14] => % Util [15] => Agent Hang-Ups [16] => Caller Hang-Ups [17] => Agent Hang-Ups Percent [18] => Caller Hang-Ups Percent ) [1] => Array ( [0] => Amber [1] => 16 [3] => 2.0% [4] => 0.2 [5] => 28.6 [6] => 1.8 [7] => 1861.1 [8] => 116.3 [9] => 0.0 [10] => 0.0 [11] => 234862.2 [12] => 3403.8 [13] => 69.0 [14] => 0.8% [15] => 10 [16] => 6 [17] => 62.5% [18] => 37.5% ) [2] => Array ( [0] => Amie [1] => 106 [3] => 13.5% [4] => 3.0 [5] => 721.7 [6] => 6.8 [7] => 12268.0 [8] => 115.7 [9] => 0.0 [10] => 0.0 [11] => 127011.0 [12] => 6350.5 [13] => 20.0 [14] => 9.7% [15] => 54 [16] => 52 [17] => 50.9% [18] => 49.1% ) ) Array 2 Array ( [0] => Array ( [0] => Agent [1] => Answered [2] => Missed [3] => Contribution [4] => Per Hour [5] => Total Ring Time [6] => Mean Ring Time [7] => Total Talk Time [8] => Mean Talk Time [9] => Total Wrap Time [10] => Mean Wrap Time [11] => Total Session Time [12] => Mean Session Time [13] => Number of Sessions [14] => % Util [15] => Agent Hang-Ups [16] => Caller Hang-Ups [17] => Agent Hang-Ups Percent [18] => Caller Hang-Ups Percent ) ) Array 3 [0] => Array ( [0] => Agent [1] => Answered [2] => Missed [3] => Contribution [4] => Per Hour [5] => Total Ring Time [6] => Mean Ring Time [7] => Total Talk Time [8] => Mean Talk Time [9] => Total Wrap Time [10] => Mean Wrap Time [11] => Total Session Time [12] => Mean Session Time [13] => Number of Sessions [14] => % Util [15] => Agent Hang-Ups [16] => Caller Hang-Ups [17] => Agent Hang-Ups Percent [18] => Caller Hang-Ups Percent ) [1] => Array ( [0] => (7312 [1] => 1 [3] => 0.0% [4] => 459.8 [5] => 0.0 [6] => 0.0 [7] => 0.4 [8] => 0.4 [9] => 0.0 [10] => 0.0 [11] => 7.8 [12] => 2.6 [13] => 3.0 [14] => 5.5% [15] => 1 [17] => 100.0% [18] => 0.0% ) [2] => Array ( [0] => Amber [1] => 414 [2] => 9 [3] => 9.3% [4] => 6.3 [5] => 1880.1 [6] => 4.4 [7] => 65209.8 [8] => 157.5 [9] => 240.4 [10] => 0.6 [11] => 234862.2 [12] => 3403.8 [13] => 69.0 [14] => 27.8% [15] => 290 [16] => 124 [17] => 70.0% [18] => 30.0% ) So in this example i have 3 arrays, all have the same first initial array. But these arrays can differ in size and they may not share the same people in in. People can also be in different places so i.e. Array 1 : Common -> Amber -> Amie Array 2 : Common -> Array 3 : Common -> 7312 -> Amber ->Amie Now the final array should look like Array 4 : Common -> 7312 -> Amber -> Amie All the values from all arrays should also be combined so i.e: Amber 1st array / 1st value : 16 Amber 2nd array / 2nd value : 414 Amber final array value : 414 + 16 = 430 Hopefully i have made question clear.
Basically, you sum up all the info you got, remove the headers. Then rebuild a new array with the info consolidation. //merge all arrays, remove the first element, it is headers. $my_total_array = array_merge( array_splice($array1,1), array_splice($array2,1), array_splice($array3,1) ); $myagents = array(); foreach($my_total_array as $agent){ if(!isset($myagents[$agent[0]])){ //Never met the agent, add them. $myagents[$agent[0]] = $agent; }else{ //We already seen the agent, do maths. $myagents[$agent[0]][1]+=$agent[1]; //TODO: Apply consolidation rules for other fields. } } //Put the headings back at the beggining. array_unshift($myagents,$array1[0]); print_r($myagents); I hope this helps. EDIT: From PHP Docs Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Get common values from a single multidimensional array in php
I have a array called $array which is multidimensional array. It consits of keys like brandname, productsubcategory (Keys are not static they can be anything and even number of keys are not fixed). I want all the common values from the array Current array: $array = array ( [brandname] => Array ( [0] => Array ( [0] => sony_brandname [1] => touch screen_type [3] => 2.8_size [4] => gsm + gsm_sim_support [5] => 2_primary_camera [6] => 64 mb_internal_memory [7] => 32_expandable_memory [8] => 1200_standard_battery_type [9] => 3_size [10] => 1000_standard_battery_type [11] => touch and type_type ) ) [productsubcategory] => Array ( [1] => Array ( [0] => karbonn_brandname [1] => touch and type_type [2] => micromax_brandname [3] => 2.8_size [4] => gsm_sim_support [5] => 3.15_primary_camera [6] => 52 mb_internal_memory [7] => 8_expandable_memory [8] => 1000_standard_battery_type [9] => nokia_brandname [10] => symbian s40_os_clean [11] => 5_primary_camera [12] => 128 mb_ram [13] => 256 mb_internal_memory [14] => 32_expandable_memory [15] => 1110_standard_battery_type [16] => gsm + gsm_sim_support [17] => 2_primary_camera [18] => 32 mb_ram [19] => 10 mb_internal_memory [20] => no_expandable_memory [21] => 1020_standard_battery_type [22] => 680 mhz_processor [23] => 64 mb_ram [24] => 128 mb_internal_memory [25] => 860_standard_battery_type [26] => blackberry_brandname [27] => 2.45_size [28] => 1 ghz_processor ) ) ); Desired array : $array = array ( [0] => sony_brandname [1] => touch and type_type [2] => 2.8_size [8] => 1000_standard_battery_type [16] => gsm + gsm_sim_support [17] => 2_primary_camera )
Use array_intersect(): $result = array_intersect($array["brandname"][0], $array["productsubcategory"][1]); print_r($result);
PHP array_filter() not working
Goal here is to take text from a log file, plain text delimited by returns with each line delimited by a varying number of spaces. Getting it into a multi-dimensional is easy enough, getting rid of blank spaces is not. I suppose there are a lot of messy ways to go about this but isn't it one of the reasons for array_filter()? $alarms = array( "1530 1545 Place_4 Fault_1", "1617 1622 Place_1 Fault_2", "1634 1640 Place_2 Fault_1" ); foreach ($alarms as $data) { $subArr = explode(" ", $data); array_filter($subArr); print_r($subArr); echo "<br /><br />"; } Output: Array ( [0] => 1530 [1] => [2] => [3] => [4] => [5] => 1545 [6] => [7] => [8] => [9] => [10] => Place_4 [11] => [12] => [13] => [14] => [15] => Fault_1 ) Array ( [0] => 1617 [1] => [2] => [3] => [4] => [5] => 1622 [6] => [7] => [8] => [9] => [10] => Place_1 [11] => [12] => [13] => [14] => [15] => Fault_2 ) Array ( [0] => 1634 [1] => [2] => [3] => [4] => [5] => 1640 [6] => [7] => [8] => [9] => [10] => Place_2 [11] => [12] => [13] => [14] => [15] => Fault_1 ) Want it to be: Array ( [0] => 1530 [1] => 1545 [2] => Place_4 [3] => Fault_1 ) Array ( [0] => 1617 [1] => 1622 [2] => Place_1 [3] => Fault_2 ) Array ( [0] => 1634 [1] => 1640 [2] => Place_2 [3] => Fault_1 ) Not sure what's wrong... Cheers.
From the manual, array_filter: Return values: Returns the filtered array. Emphasis on returns. Currently you ignore the return value.
Rather than explode(" ",$something), try this: $parts = preg_split("/ +/",$something); This will split on variable numbers of spaces.
updated code $alarms = array( "1530 1545 Place_4 Fault_1", "1617 1622 Place_1 Fault_2", "1634 1640 Place_2 Fault_1" ); foreach ($alarms as $data) { $subArr = explode(" ", $data); $subArr = array_filter($subArr); print_r($subArr); echo "<br /><br />"; }
How can I process this string so I end up with an array that just contains the full name & the associated jpg URL for each entity?
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.
How can I insert an associative array into sql?
This is my array: [abominado] => Array ( [0] => réprobo [1] => réprobo [2] => abominado [3] => banido [4] => condenado [5] => detestado [6] => odiado [7] => precito [8] => renegado [9] => repudiado ) [abominar] => Array ( [0] => repelir [1] => repelir [2] => abominar [3] => afastar [4] => afugentar [5] => arredar [6] => desalojar [7] => desviar [8] => detestar [9] => empuxar [10] => escorraçar [11] => espinafrar [12] => execrar [13] => exercer [14] => expulsar [15] => grimpar [16] => impugnar [17] => odiar [18] => rebater [19] => rechaçar [20] => recusar [21] => rejeitar [22] => relegar [23] => repudiar ) how can I insert it into sql ?
foreach ($abominado as $key=>$str) { $string .= "$key:$str\n"; } mysql_query("INSERT INTO strings VALUES ('".mysql_real_escape_string($string)."')");
You can use serialize to cast it to string and unserialize when you get it
First, these arrays happen to be sequential. Second, in a SQL table, any column can be used as a search criteria, so just insert the data directly and set up an index on any searchable column.