How build nested associative array based of a one-dimensional array [closed] - php

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

Related

I have two array in php. Need to combing in one at following way [closed]

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 7 months ago.
Improve this question
I have two arrays:
$array1 = array('104', '104', '104', '51', '228', '228');
$array2 = array('12121', '12120', '12119', '11821', '11788', '11787');
I need to create an array with two dimensions consisting of the elements of these two arrays in a specific way:
$array3 = array('104'=>array('12121', '12120', '12119'),'51'=>array('11821'),'228'=>array('11788', '11787'));
Array1 and Array2 always have the same number of elements.
How can I do this?
You can do it this way:
<?php
$array1 = ['104', '104', '104', '51', '228', '228'];
$array2 = ['12121', '12120', '12119', '11821', '11788', '11787'];
$result = [];
$index = 0;
foreach( $array1 as $key => $value ){
$result[$value][] = $array2[$index];
$index++;
}
Result:

Use substr() in array without loop? [closed]

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
Is there any way to use substr() function on all of an array's elements without using a loop?
Looping is sometimes the most simple and readable solution. But if you want to apply a function to every element of an array you can also use array_map.
Here an anonymous function is a simple wrapper for substr, which is used to return the first letter of each name in the given array:
<?php
$names =
[
'Gaga',
'Joplin',
'Vega'
];
$first_letters =
array_map(
function($str) { return substr($str, 0, 1); },
$names
);
var_export($first_letters);
Output:
array (
0 => 'G',
1 => 'J',
2 => 'V',
)
Short arrow functions introduced in Php 7.4. are slightly tidier:
array_map(
fn($str) => substr($str, 0, 1),
$names
);
The foreach/loop approach:
foreach($names as $key => $name)
$result[$key] = substr($name, 0, 1);
Mutating the original array:
foreach($names as &$name)
$name = substr($name, 0, 1);
unset($name);

Make top of position from associative array [closed]

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

Get the index ,while inserting a element into php numeric index array [closed]

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

How to compare one array key value to another array index value? [closed]

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
I have an array like
$a=array([0]=>0 [1]=>3)
$b=array([0]=>image [1]=>profile [2]=>password [3]=>login)
i want to compare array a's key value i.e 0 to array b's index value 0
Use this
$a = array(0, 3);
$b = array(0 => 'image', 1 => 'profile', 2 => 'password', 3 => 'login');
$c = array_intersect_key($b, array_flip($a));
Results
Array
(
[0] => image
[3] => login
)
Use inarray into foreach
<?php
$a = array(0,3);
$b= array('image','profile','password','login');
foreach($b as $key=>$value){
if(in_array($key, $a)) {
echo $value."<br>";
}
}
?>
Output
image
login
try array_intersect
array_intersect($a,$b);
or try === operator to compare the value in the array
<?php
$a=array(0,3);
$b=array(image,password);
foreach($a as $k=>$v){
if($a[$k]===$b[$k]){
echo "$k index is Same<br>";
}else{
echo "$k index is different<br>";
}
}
output
0 index is different
1 index is different

Categories