0

I am working in PHP with array iteration.

I have a multidimensional array like this:

Array
(
    [1] => stdClass Object
        (
            [id] => 1
            [comments] => Testing the Data
            [stream_context_id] => 5
            [stream_entity_id] => 
            [class_id] => 1
            [parent_id] => 0
            [learnt_count] => 
            [rating_count] => 
            [abused_count] => 
            [created_by] => 1
            [created_datetime] => 
            [stream_context] => comments
            [name] => 
            [upload_path] => 
            [uploadby] => 
            [upload_time] => 
        )

    [2] => stdClass Object
        (
            [id] => 2
            [comments] => Testing the Data
            [stream_context_id] => 5
            [stream_entity_id] => 
            [class_id] => 1
            [parent_id] => 0
            [learnt_count] => 
            [rating_count] => 
            [abused_count] => 
            [created_by] => 1
            [created_datetime] => 
            [stream_context] => comments
            [name] => 
            [upload_path] => 
            [uploadby] => 
            [upload_time] => 
        )

)

Here the first index values i.e. 1 and 2 are the ids mentioned in their corresponding arrays.

I want the same multidimensional array with index values a 0 and 1 and so on.. i.e. the usual format of an array.

3
  • Would be useful to also tell us how you get the data in the first place Commented Jul 27, 2011 at 8:45
  • 1
    BTW, this is not a multidimensional array, it is a normal flat array with objects as values. Commented Jul 27, 2011 at 8:47
  • 2
    First, it's not a multidimensional array, it's an array of objects. Second, if you want to enumerate keys from 0 - n, loop trough the first array (one enumerated 1 - n) and create a new one that you enumerate 0 - n. However, I see no benefit in doing so, as it shouldn't be relevant whether the array key is 0 or 1, as long as the key is unique. Commented Jul 27, 2011 at 8:48

3 Answers 3

7

Don't know if this is what you meant, but maybe...:

$reindexedArray = array_values($yourArray);

If you also want to convert the stdClass objects to arrays, try:

$reindexedAndArrayifiedArray = array_values(array_map(function ($entry) {
    return (array)$entry;
}, $yourArray));
Sign up to request clarification or add additional context in comments.

Comments

1

Using array_merge() with a blank array - will renumber numeric indexes:

$result = array_merge(Array(), $your_array_here) ;

Comments

1

This does look like a multidimentional array as you have a named array holding objects.

Your array is currently:

$a = array('1'=>Object, '2'=>Object);

Instead of:

$a = array('1'=>array('id'=>'1', 'comment'=>'some comment'), '2'=>array());
$b = array();
foreach($a as $key=>$val){
    $b[] = $val;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.