Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this array:
$top = array( 'John' => '23.4', 'Andrew' => '12.3' , 'Eric' => '15', 'Will' => '10');
How I can get position by numeric value?
Ex: John will get position 4 because have high value
Eric get position 3.....
I want to find position of key by value!
Another way to do it like below,
<?php
function find_rank($name){
$top = array( 'John' => '23.4', 'Andrew' => '12.3' , 'Eric' => '15', 'Will' => '10');
asort($top);
return array_search($name,array_keys($top))+1; # array index starts from zero that's why added extra 1
}
echo find_rank('John');
WORKING DEMO: https://3v4l.org/8rp95
This is not correct way to handle this but if you really have to deal with it here is a dirty painful to read code
asort($top);
$top = array_flip(array_values(array_flip($top)));
echo $top['Eric']; // will result 2 (list starts from 0 maybe you would like to +1 to result)
Another option:
asort($top);
$top = array_combine(range(1, count(array_keys($top))), array_keys($top));
echo $eric= array_search('Eric', $top); // return 3
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array that needs to be enhanced dynamically from the values of several strings.
$paths = array(
'1/4/6',
'1/2/4/12/4'
);
// desired result
$target = array(
1 => array(
2 => array(
4 => array(
12 => array(
4 => 'somevalue'
)
)
),
4 => array(
6 => 'somevalue'
)
)
);
Question is: how would I get from $paths to $target?
Thank you
Explode on / for a path say '1/4/6'. Now, you have 1,4 and 6.
Keep assigning them iteratively to the previous parent key. In the below code, I have made use of & to edit the same address location of the child.
<?php
$paths = array(
'1/4/6',
'1/2/4/12/4'
);
$target = array();
foreach($paths as $path){
$temp = &$target;
foreach(explode("/",$path) as $key){
if(!isset($temp[$key])) $temp[$key] = array();
$temp = &$temp[$key];
}
$temp = 'some value';
}
print_r($target);
Demo: https://3v4l.org/P3VQB
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an array that looks like this:
And I need to translate it into this:
Some things to know about the $startArray is that it is dynamic based on the amount of people submitted on a form. Each person always has just those three fields though (custom_12, custom_13, custom_14 aka fName, lName, email). So if there were 5 members, the 5th members fName would be the key custom_12-5 in the start array.
I've been looking around on stackoverflow for a question just like this and I was not able to find one. What would be the steps taken and possible array functions or anything else for creating the $endArray ?
There's actually a builtin function for this: https://www.php.net/manual/en/function.array-chunk.php
For example, array_chunk($startArray, 3) will give you the base for your new array, you'll just need to then iterate through it and rename the keys.
Alternatively, just iterate through the array yourself and add the values to a new array depending on the index of the current iteration.
Thanks to Charlie's advice, I came up with this.
$startArray = array(
'custom_12' => 'john',
'custom_13' => 'johny',
'custom_14' => 'john#johny.com',
'custom_12-2' => 'bob',
'custom_13-2' => 'bobby',
'custom_14-2' => 'bob#bobby.com',
'custom_12-3' => 'don',
'custom_13-3' => 'donny',
'custom_14-3' => 'don#donny.com'
);
$middleArray = array_chunk($startArray, 3);
$endArray = array_map(function($val) {
return array(
'fName' => $val[0],
'lName' => $val[1],
'email' => $val[2]
);
}, $middleArray);
echo "<pre>";
print_r($endArray);
echo "</pre>";
And the output is exactly what I wanted:
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new at PHP and struggling with given problem.
I have to write a function winner_generator($parameter, $random) that would pick and show random name from given array:
<p><?php echo winner_generator(array(
array('name' => 'Bob'),
array('name' => 'Donald'),
array('name' => 'Peter'),
array('name' => 'Nick')
),rand()); ?></p>
Any ideas on how I should start solving this problem? Many thanks for all your help, looking forward.
So the idea is generating a random number between 0 and the length of the array - 1 (because the index of an array starts at 0). Then you pick the candidate at whatever number the random returns. As you want to echo the winner you need to select the 'name' key from the candidate array.
<?php
function winner_generator(array $candidates){
$rand = rand(0, sizeof($candidates)-1);
return($candidates[$rand]['name']);
}
echo winner_generator(array(
array('name' => 'Bob'),
array('name' => 'Donald'),
array('name' => 'Peter'),
array('name' => 'Nick')
));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have one-dimensional array, e.g.
$arr = ['foo', 'bar', 'baz'];
I want convert it as follow(var_dump output):
array (size=1)
'foo' =>
array (size=1)
'bar' =>
array (size=1)
'baz' => string '' (length=0)
I can use only loop(for and/or foreach). Recursive functions is not allowed. PHP as primary programming language.
Please, help me write code for this transformation.
$r = array('a', 'b', 'c');
$res = array();
foreach (array_reverse($r) as $i) {
$tmp = $res;
$res = array();
$res[$i] = $tmp;
}
echo '<pre>', print_r($res);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The example code like here.
<?php
$new_room_result = array();
$rooms = array('single room' , 'delux room' , 'president room' , 'seavie room');
foreach($rooms as $room){
$new_room_result[] = $room;
// the next step - i want to get the index of after last inserment's index.
}
?>
Does anyone have any ideas?
foreach($rooms as $room){
$new_room_result[] = $room;
end($new_room_result); // move the internal pointer to the end of the array
$key = key($new_room_result);
var_dump($key);
}
I have find it out by array_keys() and array_pop() functions.
Whether there is a more easy way to reach what i want.
All you need to do is to get the length of the array:
$index = count($new_room_result);
after inserting, into $new_room_result its content will be
array (size=4)
0 => string 'single room' (length=11)
1 => string 'delux room' (length=10)
2 => string 'president room' (length=14)
3 => string 'seavie room' (length=11)
Last index is 3, and the index after is 4 which is the size of the array