Solved

help with session sub array

Posted on 2007-03-22
16
232 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now