php cal function with hash table like parameter - php

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']);

Related

how to process my array to be associative on first two levels and on third to have simple value

I'm trying to construct an array where there only strings and the array would look like this
key->key->value
To explain it I attached two screenshots below.
I start with this:
After my code below I'm 90% there, yet there is an array in value on the third level instead of simple value.
Here is some code:
$theme = ThemeHandler::with('sections.settings')->find($activeTheme);
$themeSettings = $theme->sections;
$themeSettings = collect($themeSettings->toArray());
// dd($themeSettings);
$themeSections = [];
foreach ($themeSettings as $key => $value) {
$settings = collect($value['settings']);
$settings = $settings->mapToGroups(function ($item) {
return [$item['key'] => $item['value']];
});
$themeSections[$value['key']] = $settings->toArray();
}
dd($themeSections);
I would like to end up with this structure
key->key->value
and not
key->key->single_element_array->value
I'm not sure how I end up with an array at the bottom level when I do this
return [$item['key'] => $item['value']];
inside the mapToGroups, which is a function found here: https://laravel.com/docs/5.8/collections#method-maptogroups
Maybe I misunderstand how mapToGroups work. Anybody has an idea how to get key->key->value structure output?
Use mapWithKeys() instead of mapToGroups().
You're getting an array instead of the simple value you expect because the value is a group, albeit a group with only one member.
mapToGroups() groups all the values with the same key, but mapWithKeys() will assign a single value to each key.
You can see in the examples in the collection documentation, mapToGroups() produces a result like this:
[
'Sales' => ['John Doe', 'Jane Doe'],
'Marketing' => ['Johnny Doe'],
]
And mapWithKeys() result is like this:
[
'john#example.com' => 'John',
'jane#example.com' => 'Jane',
]

Zend Framework 2 - Sessions containing multidimensional arrays

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.

How to match this value?

So I am writing something to add on to my website.
I have this value stated above:
$settings[] = array(
"name" => "torblock_redirecturl",
"title" => $lang->redirecturl,
"optionscode" => "text",
"disporder" => 1,
"value" => denied.php,
"gid" => $gid
There will be a setting that where someone can enter another url or page. But I have this later in the php file:
die('$torblock_redirecturl');
I want that to change to the value in the setting once the setting is changed.
Aka once a value is entered in the setting, I want the value to change to whatever was entered right in the die.
Thanks for your answers!
You were assigning multidimensional array.
$settings[] = array( "value" => denied.php);
So to get the value access like $settings[0]['value']; the index 0 changes according to number of arrays stored in the $settings
To get expected
die($settings[0]['value']);
Update :
If assigning as single dimensional array.
$settings = array( "value" => denied.php);
Then access it like die($settings['value']);
Try this:
die($settings[0]['name']);
It would be easier to help if you supplied more context, but your syntax error could imply that you wrap an array variable in quotes without escaping, like so:
die('$settings['name']'); // this will break
If you prefer wrapping variables in quotes, you can do it like so:
die("{$settings[0]['name']}");
You may be aware of this, but in your code you're not just assigning an array to the $settings variable:
// this:
$settings[] = array('name' => 'foo');
// is the same as doing this:
$settings = array();
array_push($settings, array('name' => 'foo'));

What is the right way to initialize a php associative array?

I am going to construct a highly complex associative array in php. But first, I need to initialize it.
What is the right way to initialize it? My initialization is as follows;
$ComplexAssociativeArray = [];
Are there better ways?
If you go for
$array = [
"foo" => "bar",
"bar" => "foo",
];
It wont work in php versions before 5.4
but below way work in all versions
$array = array(
"foo" => "bar",
"bar" => "foo",
);
I think that is the main difference.
Quoting from php docs for arrays
As of PHP 5.4 you can also use the short array syntax, which replaces
array() with [].

Are there dictionaries in php?

For example:
$names = {[bob:27, billy:43, sam:76]};
and then be able to reference it like this:
$names[bob]
http://php.net/manual/en/language.types.array.php
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>
Standard arrays can be used that way.
There are no dictionaries in php, but PHP array's can behave similarly to dictionaries in other languages because they have both an index and a key (in contrast to Dictionaries in other languages, which only have keys and no index).
What do I mean by that?
$array = array(
"foo" => "bar",
"bar" => "foo"
);
// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
The following line is allowed with the above array in PHP, but there is no way to do an equivalent operation using a dictionary in a language like Python(which has both arrays and dictionaries).
print $array[0]
PHP arrays also give you the ability to print a value by providing the value to the array
print $array["foo"]
Normal array can serve as a dictionary data structure. In general it has multipurpose usage: array, list (vector), hash table, dictionary, collection, stack, queue etc.
$names = [
'bob' => 27,
'billy' => 43,
'sam' => 76,
];
$names['bob'];
And because of wide design it gains no full benefits of specific data structure. You can implement your own dictionary by extending an ArrayObject or you can use SplObjectStorage class which is map (dictionary) implementation allowing objects to be assigned as keys.
Use arrays:
<?php
$arr = [
"key" => "value",
"key2" => "value2"
];
If you intend to use arbitrary objects as keys, you may run into "Illegal offset type". To resolve this you can wrap the key with the call of spl_object_hash function, which takes any object, and returns its unique hash.
One thing to keep in mind, however, is that then the key itself will be the hash, and thus you will not be able to get a list of the objects used to generate those hashes from your dictionary. This may or may not be what you want in the particular implementation.
A quick example:
<?php
class Foo
{
}
$dic = [];
$a = new Foo();
$b = new Foo();
$c = $a;
$dic[spl_object_hash($a)] = 'a';
$dic[spl_object_hash($b)] = 'b';
$dic[spl_object_hash($c)] = 'c';
foreach($dic as $key => $val)
{
echo "{$key} -> {$val}\n";
}
The output i get is:
0000000024e27223000000005bf76e8a -> c
0000000024e27220000000005bf76e8a -> b
Your hashes, and hashes at different executions may be different.

Categories