jimbona27
asked on
help with session sub array
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
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
ASKER
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]
if(isset($_GET['id']))
{
$_SESSION['a']['b'] = $package->get_test($_GET['
_d($_SESSION['a']);
}
and I get this
rray
(
[data] => Array
(
[1] => Array
(
[data]
ASKER
any suggestions? thanks
ASKER
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.
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.
foreach ($test_cats as $key=>$subarray)
{
print "<h1>$key</h1>";
foreach ($subarray as $subkey=>$value)
{
print $value . "<br />";
}
}
{
print "<h1>$key</h1>";
foreach ($subarray as $subkey=>$value)
{
print $value . "<br />";
}
}
ASKER
ok that works great, how can I store each sub array?
$array1 = ('hello','there',''today);
$array2 = (...);
...
thanks
$array1 = ('hello','there',''today);
$array2 = (...);
...
thanks
$test_cats['a'] = array('hello', 'there', 'today');
$test_cats['b'] = array('another example');
$test_cats['b'] = array('another example');
Or, if the sub-arrays already exist:
$test_cats['a'] = $array1;
$test_cats[b'] = $array2;
$test_cats['a'] = $array1;
$test_cats[b'] = $array2;
ASKER
foreach ($test_cats as $key=>$subarray)
{
print "<h1>$key</h1>";
foreach ($subarray as $subkey=>$value)
{
print $value . "<br />";
$test_cats[$key] = $value;
}
}
like this?
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?
<?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.
$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.
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
in this example do I pass
getOptions('a',$test_cat);
doesnt work?
thanks
getOptions('a',$test_cat);
doesnt work?
thanks
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.
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.
ASKER
ok will test it out tomorrow morning as leaving now. think thats it
var_dump($_SESSION['a'])
This will dump the value of $_SESSIOn['a'], which should match the value you expect.