I'm trying to access an array within another array (basically, a two dimensional array). However, the usual way of doing this ($myarray[0][0]) doesn't work for me.
The reason is because I want to create a recursive function which, in each call, should dive deeper and deeper into a array (like, on first call it should look at $myarray[0], on second call it should look at $myarray[0][0] and so on).
Is there any alternative way of accessing an array within array?
Thanks.
Traverse the array by passing always a subarray of it to the recursive function.
function f(array &$arr)
{
// Some business logic ...
// Let's go into $arr[0]
if(is_array($arr[0]))
f($arr[0]);
}
f($myarray);
Related
Say you have a method that receives an array as a parameter. That method does some manipulation with that array and then outputs a different array.
public function createArrayForX(array $myArray){
return $modifiedArray;
}
In order for me to test that the output has some values I need to create some sample cases for my input. It is a tedious work to create arrays manually, especially big ones.
public function testCreateArrayForX(){
$myArray = []; // **how do I easily create myArray here**
$this->assertContains("String that I want", $this->sampleClass->createArrayForX($myArray));
}
I tried something like var_dumping my array in the program, copying the result from var_dump and transforming it back to array (Convert var_dump of array back to array variable) and then edit it to make different tests. Is there a simple way or am I doing things wrong if I need this?
So my question is how do you easily create a skeleton for you sample input that you can then change to test different scenarios?
print(var_export($myArray, true));
in the program gave me what I need.
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
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
I'm programming in PHP and I have a huge array which contains a lot of ID's. I need all these ID's to be variables so I can use these in another function. I have done a lot of research on loops in PHP but I can find one which "converts" the arrays into variables that I can use in another function. So far I have a foreach loop which processes the whole array and divided into $persons. But when I use $persons in the next function it only uses the last array. My code is as follows:
$retrieved_id_array=explode(",",$retrieved_id_string);
foreach($retrieved_id_array as $persons)
$retrieved_string=file_get_contents("https://HDXLfansite.com/$persons");
So the problem is how do I make a loop which provides me with several variables I can use in another function? Or should I use another method/code?
thats because you are overwriting your variable $retrieved_string in loop, you could do:
foreach($retrieved_id_array as $persons) {
//add it in array
$retrieved_string[] =file_get_contents("https://HDXLfansite.com/$persons");
}
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.