Solved

help with session sub array

Posted on 2007-03-22
16
240 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

749 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