i have an array as below :
test[
0 => 0,
1 => 2 ,
2 => 0,
3 => 2
]
the above array values are a representation on index of a larger array , so i need to covert the values to index of another array which will map the larger array as to achive below :
test2[0][2][0][2]
i tried :
$test3= array_flip ( $test )
works ok but is not convinient as to collisions as i dont have controll of the array , any help ?
so after head i came up with below function which served my purpose :
$mainarray = [
0 => [
0 =>[
0 => [
0 => 'one' ,
1 => 'two' ,
2 => 'three' ,
3 => 'four'
],
1 => [
0 => 'one' ,
1 => 'two' ,
2 => 'three' ,
3 => 'four'
]
]
]
]
Then this array below has the values as the index of the $mainarray
above of what i want to get lets say one
as above ,
$arraywithseacrhindex = [
0 => 0 ,
1 => 0 ,
2 => 0 ,
3 => 0 ,
]
Solution :
$array = $mainarray ;
$arr = $arraywithseacrhindex ;
$counter= count($arr)-1 ;
$result = array() ;
for($i=0; $i< $counter; $i++) {
if(empty( $result ) )
{
// first loop to put $result at per with $array
$result[$arr[$i]] = $array[$arr[$i]] ;
}else{
// this is the trick of the solution
$cResult = current($result);
unset($result);
$result = array() ;
$result[$arr[$i]] = $cResult[$arr[$i]] ;
}
}
var_dump($result);
Hope it helps someone in future .