<?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).
Related
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
This is an array,
$array = array(
'one' => 1,
'two' => 2,
'three' $array['one'] + $array['two']
);
It's get an error,
why?
Because $array does not exist before the end of your declaration. You simply cannot define an array on such a recursive manner since you refer to a non-existing resource.
This is a working variant. Indeed, not very convenient, but working:
<?php
$array = [
'one' => 1,
'two' => 2
];
$array['three'] = $array['one'] + $array['two'];
var_dump($array);
The output obviously is:
array(3) {
'one' =>
int(1)
'two' =>
int(2)
'three' =>
int(3)
}
The only really elegant way around this requires quite some effort:
You can implement a class implementing the ArrayAccess interface. That allows you to implement how the properties should be defined internally (for example that sum), whilst still allowing to access these properties via an array notation. So no difference to an array at runtime, only when setting up the object. However this is such a huge effort that I doubt it is worth it in >99% of the cases.
You're using a variable which is being declared, its value value is not know yet. Here is how you should write it:
$array = array(
'one' => 1,
'two' => 2,
);
$array['tree'] = $array['one'] + $array['two'];
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.
I am working with a (highly optimized/adapted version of) CakePHP 2.3 and my application is running on VERY slow hardware (300MHz ARM) so I am still optimizing wherever I can. One method of the framework is called VERY often and not very fast (~1-5ms), but I can not think of a way to improve it (without changing the output) - in total I spend ~10% of the total time in this method:
public static function normalizeObjectArray($objects) {
$normal = array();
foreach ($objects as $i => $objectName) {
$options = array();
if (!is_int($i)) {
$options = (array)$objectName;
$objectName = $i;
}
list(, $name) = pluginSplit($objectName);
$normal[$name] = array('class' => $objectName, 'settings' => $options);
}
return $normal;
}
Does anyone have an idea how to speed this up?
The profiler has the following output for one of the calls - I already asked how to improve pluginSplit in this question:
(Profiling is about 10-15 times slower then normal execution)
Is it the is_int that is that slow or where is that time "lost"?
Optimize by removing the method.
normalizeObjectArray is the method that converts arrays like this:
public $foo = array(
'One',
'Two',
'Three' => array('option' => 1, 'other' => 2)
);
into:
public $foo = array(
'One' => array('className' => 'One', 'settings' => array()),
'Two' => array('className' => 'Two', 'settings' => array()),
'Three' => array('className' => 'Three', 'settings' => array('option' => 1, 'other' => 2))
);
If instead of trying to optimize this code, you refactor the code to not call it and ensure that wherever it's called the array is already in the format required (e.g. component, helper, behavior arrays), the logic is redundant and can simply be removed.
First you can avoid the list. Instead, you can do:
$normal[pluginSplit($objectName)[1]] = ... ;
Secondly, I think (not sure) that ctype_digit() can improve performance a bit.
By the way, could you give an example of content of $objects? It sounds like a weird array...
I'm trying to use array_merge_recursive to merge two data structures.
<?php
$testSite = array(
'name' => 'test site',
'modules' => array(
'foo' => 'true',
'bar' => 'true'
)
);
$testData = array(
'modules' => array(
'bar' => 'false'
)
);
$testSite = array_merge_recursive($testSite, $testData);
Note that I'm using strings instead of booleans for debug printing purposes
I would expect $testSite to be the exact same after this code has ran, except for the modules.bar property, which I'd expect to see being changed to false. What happens instead, as seen in this live example, is that bar is turned into an array containing it's old value and the value false is appended to that.
The documentation page reads that this is what will happen for numeric keys, but these are all strings keys. Can anyone shed some light on this?
I think you want array_replace_recursive.
array_merge_recursive() vs. array_replace_recursive()