Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Most likely I'm doing this wayyyyyy too complicated. But I'm in the need of converting multiple arrays to multidimensional array key's. So arrays like this:

Array //$original
(
    [0] => 500034
    [1] => 500035 //these values need to become 
                  //consecutive keys, in order of array
)

Needs to become:

Array
(
    [50034][50035] => array()
)

This needs to be done recursively, as it might also require that it becomes deeper:

Array
(
    [50034][50036][50126] => array() //notice that the numbers 
                    //aren't necessarily consecutive, though they are 
                    //in the order of the original array
)

My current code:

$new_array = array();

foreach($original as $k => $v){ //$original from first code

    if((gettype($v) === 'string' || gettype($v) === 'integer') 
    && !array_key_exists($v, $original)){ //check so as to not have illigal offset types

        $new_array =& $original[array_search($v, $original)];

        echo 'In loop: <br />';
        var_dump($new_array);
        echo '<br />';
    }
}

echo "After loop <br />";
var_dump($new_array);
echo "</pre><br />";

Gives me:

In loop: 
int(500032)

In loop: 
int(500033)

After loop 
int(500033)

Using this code $new_array =& $original[array_search($v, $original)]; I expected After loop: $new_array[50034][50035] => array().

What am I doing wrong? Been at this for hours on end now :(

EDIT to answer "why" I'm trying to do this I'm reconstructing facebook data out of a database. Below is my own personal data that isn't reconstructing properly, which is why I need the above question answered.

[500226] => Array
    (
        [own_id] => 
        [entity] => Work
        [name] => Office Products Depot
        [500227] => Array
            (
                [own_id] => 500226
                [entity] => Employer
                [id] => 635872699779885
            )

        [id] => 646422765379085
    )

[500227] => Array
    (
        [500228] => Array
            (
                [own_id] => 500227
                [entity] => Position
                [id] => 140103209354647
            )

        [name] => Junior Programmer
    )

As you can see, the ID [500227] is a child of [500226], however, because I haven't got the path to the child array, a new array is created. The current parentage only works to the first level.

[own_id] is a key where the value indicates which other key should be its parent. Which is why the first array ([500226]) doesn't have a value for [own_id].

share|improve this question
2  
Why do you need to do this?! –  Amal Murali Nov 18 '13 at 13:52
    
To reconstruct fragmented database data, but the question isn't why, it's how ;) But I'll put in an edit to show what I'm trying to do. Give me a few mins ;) –  Nukeface Nov 18 '13 at 14:06
1  
The question usually is why behind the scenes, hidden in a "how" ala-XY problems. :) –  Mike Nov 18 '13 at 14:09
    
Fair point @Mike, I've added the "why" to the question. –  Nukeface Nov 18 '13 at 14:14

1 Answer 1

If you want to do something recursively, do it recursively. I hope that's what you meant to do.

public function pivotArray($array, $newArray)
    {
        $shifted = array_shift($array);
        if( $shifted !== null )
        {
            return $this->pivotArray($array, array($shifted=>$newArray));
        }
        return $newArray;
    }

$array = array(432, 432532, 564364);
$newArray = $this->pivotArray($array, array());

Edit: After the question's edit it doesn't seem to be very relevant. Well, maybe someone will find it useful anyway.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.