1

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 ?

4
  • You're trying to turn array of elements into arrays. In other words you're trying to create a nested array. There is no built in php function to dot his. You have to write it yourself. Commented Mar 24, 2016 at 18:04
  • 1
    test2[test[0]][test[1]][test[2]][test[3]] not work? Commented Mar 24, 2016 at 18:08
  • @JamesPaterson that will work but i dont know the lenght of array as its dynamic Commented Mar 24, 2016 at 18:15
  • 1
    Ok - Working on a function Commented Mar 24, 2016 at 18:23

2 Answers 2

1
function arrayToIndex($array,$index) {
        $element = $array;
            for ($i = 0; $i < count($index); $i++) {
                    $element = $element[$index[$i]];
            }
            return $element;
    }
$test = [[[[1],[2]],[[3],[4]]],[[[5],[6]],[[7],[8]]]];
$index = [0,1,0,0];
echo arrayToIndex($test,$index);

Here's a function that implements the behaviour you asked for. $array is the array to search into, and $index is the array of indexes. I suggest you play around with this example and see how each value is retrieved.

Live example: http://ideone.com/fork/BzCjcK

Sign up to request clarification or add additional context in comments.

3 Comments

returns 3 , my end game is to get an array index representation as in $index in your example
This is working off the test data $test and $index. You'll need to copy just the function and call the function as arrayToIndex($test2,$test) in your OP example. It returns three because that is the element at $test[0][1][0][0].
again , $test2 is the end result of values of $test1 , i have know idea what $test2 is i need to get $test2 from values of $test
0

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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.