aleksy Posted August 16, 2015 Share Posted August 16, 2015 Hello, I have an array which consists of many arrays. <?php $array = array(1 => array(1,10,100,101,102,103), 2 => array(104,105,106,107,108,109), 3 => array(11,110,111,112,113,114), 4 => array(115,116,117,118,119,12), 5 => array(120,121,122,123,124,125), 6 => array(126,127,128,129,13,130), 7 => array(131,132,133,134,135,136), 8 => array(137,138,139,14,140,141), 9 => array(142,143,144,145,146,147), 10 => array(148,149,15,150,151,152), 11 => array(153,154,155,156,157,158), 12 => array(159,16,160,161,162,163), 13 => array(164,165,166,167,168,169), 14 => array(17,170,171,172,173,174), 15 => array(175,176,178,18,19,2), 16 => array(20,21,22,23,28,29), 17 => array(3,30,31,32,33,34), 18 => array(35,36,37,38,39,4), 19 => array(40,41,42,43,44,45), 20 => array(46,47,48,49,5,50), 21 => array(51,6,69,7,8,86), 22 => array(87,88,89,9,90,91), 23 => array(92,93,94,95,96,97), 24 => array(98,99)); ?> How can I get numbers from first array and store them in a new array? I want to do the same with the other arrays. Can somebody help me to write a function where I input $array and get $array1 = array(1,10,100,101,102,103), $array2 = array(104,105,106,107,108,109) etc. so I can use this data on my website? Thank you for your help! Quote Link to comment Share on other sites More sharing options...
requinix Posted August 16, 2015 Share Posted August 16, 2015 (edited) No. Don't do it. That's bad. You have an array already and it has everything you want. Just use $array[1] and [2] and so on. Dare I ask... What's this array, why does it look like you have 1-178 sorted like strings and partitioned into those sub-arrays, and why do you think you need to put them into separate variables? Edited August 16, 2015 by requinix Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 16, 2015 Share Posted August 16, 2015 It appears that you have a series of numbers sorted ALPHABETICALLY and stored in groups of 6 values. What it is and why it is stored that way is a mystery, but apparently necessary. As for re-arranging it - do as Requinix said - don't! You already have a perfectly valid (tho limited) data structure so use it. Instead of $array1 you just reference $array[1] or to get individual values $array[1][0],$array[1][1] and so on. Read the manual on arrays to help understand arrays if you need to. Quote Link to comment Share on other sites More sharing options...
bsmither Posted August 16, 2015 Share Posted August 16, 2015 To actually give you something that exactly answers your desire: function splatter($array) { foreach($array as $key => $val) $GLOBALS['splattered_array'.$key] = $val; } You now have a variable named $GLOBALS['splattered_array17'] or $splattered_array17, for example. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 16, 2015 Share Posted August 16, 2015 facepalm followed by Quote Link to comment Share on other sites More sharing options...
requinix Posted August 17, 2015 Share Posted August 17, 2015 Where's the Dislike This button? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 17, 2015 Share Posted August 17, 2015 Dislike plus 1 here! So the user now has a slew of vars containing his data. Just what he wished for. Now what does he do with them? How does he find anything? Is this original array a static set of data - never to change or be re-sized? With all those var names what kind of (complex) code is he going to have to write to find anything? Moreso - with var names that have nothing to do with the specific value that they hold how WILL he find anything? With an array at least the data is easily accessible. It may still be unrecognizable as to its true meaning but at least it will be find-able. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 17, 2015 Share Posted August 17, 2015 Dislike++ Not to mention unnecessarily creating them as (evil) GLOBALS Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.