Solved

help with session sub array

Posted on 2007-03-22
16
243 Views
Last Modified: 2008-03-06
please explain this.in my php i have


if(isset($_GET['id']))
{
      $_SESSION['a']['b'] = $test->get_test($_GET['id']);
}

dumping this returns...

Array
(
    [1] => Array
        (
            [data] = as expected
            [data] = as expected
            [data] = as expected
           ....
}


for my function to work correctly I need the dump to be at the root so when I var_dump as above it actually looks like this:


Array
(
      [data] = as expected
      [data] = as expected
      [data] = as expected
      [data] = as expected
0
Comment
Question by:jimbona27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 18771756
Perhaps you mean like this:

var_dump($_SESSION['a'])

This will dump the value of $_SESSIOn['a'], which should match the value you expect.
0
 

Author Comment

by:jimbona27
ID: 18771931
tried this.
if(isset($_GET['id']))
{
      $_SESSION['a']['b'] = $package->get_test($_GET['id']);
      _d($_SESSION['a']);
}

and I get this

rray
(
    [data] => Array
        (
            [1] => Array
                (
                    [data]

0
 

Author Comment

by:jimbona27
ID: 18778280
any suggestions?  thanks
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:jimbona27
ID: 18808440
heres another example that might help..

var_dump($test_cats);

returns...

Array
(
    [a] => Array
        (
            [1] => hello
            [2] => there
            [3] => today
        )

    [b] => Array
        (
            [5] => another example

...


using this array how can I look through each sub, i.e.


foreach($test_cats as $k=>$v)
{
      echo $k . $v;
}

doesnt work but I want to display..


hello
there
today

thank you.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18808508
foreach ($test_cats as $key=>$subarray)
{
      print "<h1>$key</h1>";
      foreach ($subarray as $subkey=>$value)
      {
            print $value . "<br />";
      }
}
0
 

Author Comment

by:jimbona27
ID: 18808676
ok that works great, how can I store each sub array?

$array1 = ('hello','there',''today);
$array2 = (...);
...


thanks
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18808709
$test_cats['a'] = array('hello', 'there', 'today');
$test_cats['b'] = array('another example');
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18808715
Or, if the sub-arrays already exist:

$test_cats['a'] = $array1;
$test_cats[b'] = $array2;
0
 

Author Comment

by:jimbona27
ID: 18808757

foreach ($test_cats as $key=>$subarray)
{
    print "<h1>$key</h1>";      
      
    foreach ($subarray as $subkey=>$value)
      {            
              print $value . "<br />";
              
            $test_cats[$key] = $value;
    }      
}

like this?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18808777
I guess I am not sure what you are trying to do. The values already exists in $test_cats, so you don't need to set the value there again. Are you trying to create a new array with the values of the sub-array?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18808863
<?php

$test_cats = array();
$test_cats['a'] = array('hello', 'there', 'today');
$test_cats['b'] = array('another example');

foreach ($test_cats as $key=>$subarray)
{
      foreach ($subarray as $subkey=>$value)
      {
            if (!isset(${'array_'.$key}))
            {
                  print "Creating variable \$array_$key.<br />";
                  ${'array_'.$key} = array();
            }
            ${'array_'.$key}[] = $value;
      }
}

var_dump($array_a);

print "<br />";

var_dump($array_b);

?>

This creates a dynamic variable for each array. I would not recommend doing this, because dynamic variable names can get messy, but if you must, this method should work.
0
 

Author Comment

by:jimbona27
ID: 18808899
this is the structure as above.

Array
(
    [a] => Array
        (
            [1] => hello
            [2] => there
            [3] => today
        )

    [b] => Array
        (
            [5] => another examplei.e.

........


i need to


getOptions($id)


if I pass 'a' as the parameter

it needs to return

hello
there
today

is that clear?

0
 
LVL 24

Accepted Solution

by:
glcummins earned 50 total points
ID: 18808942
Why not just this? Just return the entire subarray:

function getOptions($id, $test_cats)
{
      return $test_cats[$id];
}
0
 

Author Comment

by:jimbona27
ID: 18809257
in this example do I pass

getOptions('a',$test_cat);

doesnt work?

thanks
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18809276
getOptions('a',$test_cat);

Should be:

getOptions('a',$test_cats);

In the future, rather than "doesn't work", you might provide the actual error message that is given. That will help us resolve the problem in a more timely manner.
0
 

Author Comment

by:jimbona27
ID: 18809808
ok will test it out tomorrow morning as leaving now.  think thats it
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question