Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help with session sub array

Posted on 2007-03-22
16
Medium Priority
?
244 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
Technology Partners: 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

670 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