Is this the correct way of pushing an array into another array? Also, do all array pushes require 2 arguments?
$edge = array( "nodeTo" => "$to");
array_push( $node["adjacencies"], $edge);
The documentation is quite clear on this:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function
The function definition lists a two-argument requirement. You need the pushee and something to push at least.
array_push is really designed to be used to push/append multiple elements simultaneously.
array_push will add whatever you give it as a new element on the end of the target array.
So your example will add a new array as the last element of $node["adjacencies"] which will be your node connection array. For your example I believe you would like to use
$node["adjacencies"] += $edge
to correctly compose an adjacency map
Related
I'm trying to find out if there's a way of removing an array element and at the same time storing that value in a variable.
i.e.
$array = [
'foo' => 'a',
'bar' => 'b'
];
// Perform the following with one action?
$var = $array['foo'];
unset($array['foo']);
Edit: I mean if it can be done without a custom function.
There is but it's slow and ugly.
$var = array_splice($array, array_search('foo', array_keys($array)), 1)['foo'];
I'd stick with the 2-liner.
It can be done under certain circumstances.
There are two functions which do what sort of what you want.
array_pop($stack);
array_shift($stack);
But array_pop gets and removes only the last element and array_push the first of the array which can lead to unexxpected behaviour of your code if you are using an associative array.
However if you can restructure your array to fit for these functions without increasing the complexity (which would make this whole thing pointless) it can be done.
The two links for the functions are:
array_pop
array_shift
An entire reference of the array function can be found here. Maybe you find something that serves you the way you need it to:
Array functions reference
When working with existing code, it takes one array and places it into another in the fashion shown below.
I believe the empty brackets are the same thing as simply pushing it and appending it to the first available index.
$g['DATA'][] = $p;
After this is done, I have my own array that I would like to append to this as well. I tried using array_merge() with $g['DATA'][]as a parameter, but this is invalid for obvious reasons.
My only thought is to create a foreach loop counter so I can figure out the actual index it created, however I have to assume there is some cleaner way to do this?
Just simply use the count() of your $g["DATA"] array as index and then you can merge it like this:
$g['DATA'][count($g["DATA"])-1] = array_merge($g['DATA'][count($g["DATA"])-1], $ownArray);
//^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
// -1 Because an array is based index 0 -> means count() - 1 = last key
I have a unique issue that I have never heard of this being possible to do before:
So essentially I have a function that takes in a an array of arguments such as:
function someFunction(array $arguments){}
and gives me an array back as such:
array('option1', 'option2', 'options3', ...);
I then need to take that array and loop through it creating an associative array such as:
array('option1' => call_come_method('option1'), .... );
heres the kicker, you will never know how many arguments a user passes into the function, yet each one needs to created into a key=>value arrangement as seen above.
Now i did some research and I was told the $argv command in php, how ever where I am stumped is how to implement it in this case.
So if any one can give me any pointers I would be appreciative.
This is a lot easier than you think. First use array_flip to switch the array's keys and values.
$newArray = array_flip($arguments);
Then loop though it and call the method:
foreach($newArray as $key=>&$val){
$val = call_come_method($key);
}
The &, makes it a reference, so the array value is updated.
DEMO: http://codepad.org/giL1KPA3
UPDATE: You don't even need array_flip, you just need a for loop.
$newArray = array();
foreach($arguments as $val){
$newArray[$val] = call_come_method($val);
}
DEMO: http://codepad.org/AQ1gWrou
you will never know how many arguments a user passes into the function
FYI, you get to know it with func_get_args().
This way you don't need to provide your function with an $arguments parameter, but you just leave it empty.
I was looking through some inherited PHP code when I found the following:
$emails=array();
foreach($usrs as $usr){
$emails[]=$usr['email'];
}
It's clear that this is trying to extract the 'email' property of each object in a list of users and hold them in an array. That's what I want it to do. Does this do that? I've never seen such a thing work this way. I replaced it with
array_push($emails, $usr['email']);
since I know that that does what I intend it to.
The two are the same, almost.
It will add an item to the end of the array, just as array_push does. The only difference is array_push returns the new number of elements in the array. The empty bracket notation does not return anything, obviously.
You should get comfortable with this notation, it's easier to type and read.
array_push on PHP docs mentions the use of this notation and covers two other differences as well.
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
When you want to push multiple elements to an array, then you could use array_push.
array_push($emails, $var1, $var2, $var3, ...);
Otherwise, if you only push one elements to an array, just use:
$emails[] = $var;
which saves you one function call.
Yes, it does the same thing. Assigning to $array[] is equivalent to pushing an element to $array.
Also, it looks prettier, so use it ;)
let's assume we have an array like this
$arr=array(array('a'=>1,'b'=>2),array('c'=>3,'d'=>4));
and a reference to one of its elements
$element=&$arr[1]['c'];
My question is is it possible to get back to the original array using the reference alone?
That is to get back to the parent array in some way without knowing it by name... This would be useful to me in a more complex scenario.
No, it's certainly not possible. Being a "reference" (as PHP calls it; it's actually a copy inhibitor) doesn't help at all in that matter. You'll have to store the original array together with the element.
$elArrPair = array(
"container" => $arr,
"element" => &$arr[1]['c'],
);
This way you can change the element with $elArrPair["element"] = $newValue and still be able to access the container.
You cannot get from $element to $arr. You can use in_array() of course, but nothing about $element contains a reference to $arr.
You copy the contect from one variable to another, nothing else, there is no connection between the two variables.