Zend Framework 2 - Sessions containing multidimensional arrays - php

Suppose i have the following set of data:
$foobar = array(
"foo" => array (
"foo1" => 1,
"foo2" => 2,
"foo3" => 3
),
"bar" => array (
"bar1" => 1,
"bar2" => 2,
"bar3" => 3,
),
);
In standard PHP, i could do the following:
$_SESSION['foobar'] = $foobar;
Then, to call values, by example bar2:
$_SESSION['foobar']['bar']['bar2'];
But what about doing this in Zend Framework 2?
I have already set bootstrap with all parameters for session manager, and container has been set with it. Sessions get created. So, if i do, by example:
$session = new Container('foobar');
and put a value in there:
$session->foo1 = 1;
this works. Same if i decide to put an array as session variable:
//placing the $foobar array defined before
$session->foobar = $foobar;
But i don't know how can i call values. Supposing i want foo2, i'd do
echo $session->foobar->foo->foo2;
expecting it would output '2', but i get an error instead:
So i tried doing
echo $session->foobar['foo']['foo2'];
but this returns another error.
So now i don't know what should i do to gather those data, or how could i store session variables differently. I need this to make a shopping cart, so foo and bar are different products. How could i do this?

Solved. First of all i created the parent offset this way:
$session->offsetSet("foobar", new ArrayObject());
(you need use Zend\Stdlib\ArrayObject; on top of your script).
Now i can create anything from there:
$session->foobar->foo = "foo1";
$session->foobar->bar = "bar1";
and so going on.
To get them, it's as easy as it should:
echo $session->foobar->foo; //returns foo1
I hope this will help someone.

Related

How to make PhpStorm suggest / autocomplete array keys

<?php
function test(){
return array(
'one' => 1,
'two' => 2
);
}
$test = test();
echo $test[''];
I would like to put my cursor in the last line in between the single quotes, and have it suggest one or two. How can I do this?
The below works fine, so why doesn't it work inside of a function:
$test = array(
'one' => 1,
'two' => 2
);
echo $test[''];
// Suggests 'one' and 'two'
The below works fine, so why doesn't it work inside of a function:
Because it's implemented only for variables/class properties.
How can I do this?
Just install deep-assoc-completion plugin. It can do even more than that (e.g. help with completion of array parameter -- what keys are possible (if you bother to describe those keys, of course) etc).

is there a standard PHP function for transfering an associative array elements to another associative array

Is there a standard PHP function that changes an associative array's index names?
$a1 = array('one'=>1,'two'=>2,'three'=>3);
$new_index_names = array('one'=>'ono','two'=>'dos','three'=>'tres');
$a2 = change_index_names($a1,$new_index_names);
print_r($a2);
// $a2 should have the index names changed accordingly and look like this:
// array('ono'=>1,'dos'=>2,'tres'=>3)
EDIT
Please note that the function needs to know the mappings to the new index names. Meaning, in $new_index_names array provides the mappings. So again, it needs to know that 'ono' is the new index name for 'one'.
EDIT
I know you guys can come up with your own solution, i was wondering there is a standard PHP function that already does this.
EDIT
There are several situations where changing index names would help:
1) separates post value names to generic/internal names so you can separate
your backend code from front-end code.
2) say for instance you have two arrays from post that need to go through the
same exact process, except both arrays although mean/contain same exact type
of values/order/structure, they're index names are different. So when
passing to a function/method that goes by only a set of index names, you'll
need to convert the index names before passing them to that function/method.
You don't need array_values To get your desired output you can just use
array_combine($new_index_names,$a1);
not in just 1 function, but you could use array_values to get the values of the first array, and array_combine to set the new keys
Assuming both arrays has equal count, one way is with array_combine() and array_values()
$a2 = array_combine(array_values($new_index_names), $a1);
The previous answers does not consider the order of $a1 and $new_index_names, so I put my solution following:
$a1 = array('one' => 1, 'two' => 2, 'three' => 3, 'zero' => 0);
$new_index_names = array('zero' => 'zero', 'one' => 'ono', 'two' => 'dos', 'three' => 'tres');
array_combine(
$new_index_names,
str_replace(array_keys($a1), array_values($a1), array_keys($new_index_names))
);
Array
(
[zero] => 0
[ono] => 1
[dos] => 2
[tres] => 3
)
The best answer I've seen thus far:
foreach ($a1 as $k => $v) $a2[$new_index_names[$k]] = $v;
Credits go to jh1711

Set array value to a value from the same array on creation PHP

I'm trying to see if you can assign an array value during creation to a value in the same array.
If I'm declaring and initializing an array like this:
$this->array = array(...);
Can I do something like this?
$this->array = array
(
'value1' => 'hello',
'value2' => $this->array['value1'],
);
I've tried it already and I get back
Notice: Undefined index: value1 in /Sites/website/config.php on line 329
I've gotten around this problem already but now out of interest I want to see if theres actually a way to do this.
It has to be during creation and declared like above, not through
$array['value1'] = 'foo';
$array['value2'] = $array['value1'];
or
$common = 'foo';
$array = array('value1' => $common, 'value2' => $common);
Is this technically impossible or is there any way?
I don't think it's possible. The array has to be parsed before it can be assigned, so at the time $this->array['value1'] is being calculated, $this->array is still null.
Basically, $this->array['value1'] cannot be be accessed before $this->array['value2'] is set, which needs to access $this->array['value1'], which cannot be accessed before . . .
The reason you're getting Notice: Undefined index: value1 in /Sites/website/config.php on line 329 is because when you're defining your array the value you're trying to access has not yet been defined because you haven't reached the end of the initial function.
In the following code:
$this->array = array
( //this is the start of the array
'value1' => 'hello',
'value2' => $this->array['value1'],
); //this is the end of the array statement.
The array is not defined until you reach the end of the statement.
The reason the other methods work is because you're using multiple steps to access an already existing variable.
I think that is not possible, assign the element when the array starts
you can set as false and before the action that set the value as your code.
example :
$common = 'foo';
$array = array(
'value1' => $common,
'value2' => $common
);
or you can use the function array_combine

php cal function with hash table like parameter

I defined a function which I want to use hash table like parameter inside.
function Foo($param)
{
// Here, I should get fener as key, and bahce as value.
}
Foo('fener' => 'bahce'); // Is there a way like .net's lambda expression ?
And I don t want to use Foo(array('fener' => 'bahce')) // it is possible I know..
One way or the other you will have to declare your array with array():
$args = array('fener' => 'bahce');
Foo($args);
or directly:
Foo(array('fener' => 'bahce'));
Edit
As of PHP 5.4 you can also do (from the manual):
// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
So you might get away with:
Foo(['fener' => 'bahce']);

PHP possible to initial array with value from same array?

I want to save current path information in an Array and one field is a part of another. Can I access a field of the same array during initialization?
$this->path = array
(
'rel_image' => '/images',
'document_path' => '/a/file/path',
'path' => $this->path['document_path'].$this->path['rel_images']
);
or do I have to initial them one by one?
The array still is undefined while you're defining it. However you can define other (temporary) variables to do so on the fly:
$this->path = array
(
'rel_image' => $r = '/images',
'document_path' => $p = '/a/file/path',
'path' => $p.$r
);
However that normally should not be needed, as you're duplicating data within the array. Just saying, you can do whatever you want :)
You have to initialize them one by one.
It is best to think of array as a constructor. The array itself doesn't completely exist until after the function call is complete, and you can't access something which doesn't completely exist in most circumstances.
yes, you have to initialize one by one, beacuse $this->path is being filled after array() function is done.
As far as I know, the assignment you're trying to do isn't a functional one.
Code:
<?php $array = array('foo' => 'bar', 'bar' => $array['foo']); ?>
<pre><?php print_r($array); ?></pre>
...renders the following:
Array
(
[foo] => bar
[bar] =>
)
As the array is created at one time, not once per element, it will not be able to reference the values in the same statement as the assignment.

Categories