Jump to content

Recommended Posts

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! :)

Link to comment
https://forums.phpfreaks.com/topic/297813-multidimensional-arrays/
Share on other sites

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 by requinix

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.