Creating an associative array from an array, based on the array - PHP - php

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.

Related

PHP - Remove associative array element and assign it to a variable in one action?

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

php array creation like (''=>'')

What is difference between $prefix=array(''=>''); and $prefix=array();
what exactly $prefix=array(''=>''); using for ?
There isn't really a difference, both are arrays. The difference is, the latter has an array key.
For instance,
$test1=array(1,2,3,4,5);
$test2=array('name'=>'bob','lastname'=>'fossil');
will return;
print_r($test1[0]);
//1
print_r($test2['name']." ".$test2['lastname']);
//bob fossil
Basically, it gives the value a name
key=>val
can be used like this
foreach($test2 as $val){
echo$val;
}
//bob
//fossil
It's used for creating associative arrays
No difference. first option creates an array with some elements at once, the second creates an empty array.

How get size_of $_GET

I want to get the number of values that were submitted via my $_GET. I'm going to be using a for loop and every 7 are going to be saved to a class. Does anyone know how to find the Size_of the $_GET array? I looked through this info $_GET Manual, but don't see what I'm looking for.
This is a code snippet that I started in the class that I go to after the submit.
for($i=0;$i<$_GET<size>;$i++) {
$ID = $_GET["ID"][$i];
$Desc = $_GET["Desc"][$i];
$Yevaluation = $_GET["Yevaluation"][$i];
$Mevaluation = $_GET["Mevaluation"][$i];
$Cevaluation = $_GET["Cevaluation"][$i];
$Kevaluation = $_GET["Kevaluation"][$i];
$comment = $_GET["comment"][$i];
//saving bunch to class next
}
$_GET is an array. Like any other array in PHP, to find the number of elements in an array you can use count() or its alias sizeof().
count($_GET["ID"])
could work. However, then you have to be sure that each index contains the same number of elements.
More secure would be
min(count($_GET["ID"]), count($_GET["Desc"]), ...)
With array_map this can probably be shortcut to something like:
min(array_map(count, $_GET))
Try thinking through the last one, if it seems difficult to you. It's called functional programming and it's an awesome way to shortcut some stuff. In fact array_map calls another function on each element of an array. It's actually the same as:
$filteredGet = array();
foreach ($element in $_GET) { // this is the array_map part
$filteredGet[] = count($element);
}
min($filteredGet);
Even better would be a structure where $i is the first level of the array, but I think this is not possible (built-in) with PHPs $_GET-parsing.

Create associative array from numeric array and function?

I have a numeric array with codes like this: array('123', '333', '444');
I also have a function that given a code returns a name, so myFunc('123') would return 'soap'
I'd like to generate an associative array containing codes as keys and names as values. Is there any function that would allow me to do this? I know a foreach loop would do it but I wonder if there's some made function for this. Saw some methods like array_map but they don't seem to fit my needs.
array_combine($arr, array_map('myFunc', $arr))
But two functions, not one ;-) Still oneliner though

How does php interpret the syntax $var[] = $val?

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

Categories