News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Array problem

Started by Cadish, July 30, 2005, 05:38:05 AM

Previous topic - Next topic

Cadish

Hi,

If I have something in my MySQL-database like this:

indexlabel
0,0this
0,1is
1a
2test

Is it possible to make something like this?

$array[0][0] = "this";
$array[0][1] = "is";
$array[1] = "a";
$array[2] = "test";

mediman

you have to explode the indexnummers

$bla = explode(',',$row[0];
$bla[0] = $ind1;
$bla[1] = $ind2;

array['$ind1']['$ind2'] = $row[1];

something like this ...

mediman
My Projects: http://ticker-oase.de 
Please do not PM me with support requests.

Cadish

And if you don't know the size of the possible arrays?
The size of the arrays is dynamic... :/

mediman

count($array) is your friend
My Projects: http://ticker-oase.de 
Please do not PM me with support requests.

Cadish

You don't get it...

The possible "index" can be "0,1", but can also be "0,1,2,0,1,0,4,21,5"...

That's the tricky part of it

Cadish

I've found a piece of code to do it:

You have a syntax like 'a|b|c|d' which represents the array structure, and you want to insert a value X into the array at the position $array['a']['b']['c']['d'] = X.

<?
   
function array_path_insert(&$array$path$value)
   {
       
$path_el split('\|'$path);
      
       
$arr_ref =& $array;
      
       for(
$i 0$i sizeof($path_el); $i++)
       {
           
$arr_ref =& $arr_ref[$path_el[$i]];
       }
      
       
$arr_ref $value;
   }

   
$array['a']['b']['f'] = 4;
   
$path  'a|b|d|e';
   
$value 'hallo';
  
   
array_path_insert($array$path$value);

   
/* var_dump($array) returns:

   array(1) {
     ["a"]=>
     &array(1) {
       ["b"]=>
       &array(2) {
         ["f"]=>
         int(4)
         ["d"]=>
         &array(1) {
           ["e"]=>
           string(5) "hallo"
         }
       }
     }
   */

?>

mediman

your code is not ready for php 4.4.x
My Projects: http://ticker-oase.de 
Please do not PM me with support requests.

Cadish

How can I solve this then?

And how can I get the variable if I know the path?

Advertisement: