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
Solved

help with session sub array

Posted on 2007-03-22
16
239 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

840 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