0

How can I add to a multidimensional array also the index number of the current added item?

$data_array[] = array('data_array_index' => *the index number of the this array on $data_array*, 'stuff' => 'stuff', etc.)

So when I:

print_r($data_array);

Array(
  [0] => Array(
            data_array_index => 0
            stuff => stuff
         )
  [25] => Array(
            data_array_index => 25
            stuff => stuff
         )
  etc.

Thank you

EDIT

Should this work?

$data_array[] = array('data_array_index' => end($data_array)+1, 'stuff' => 'stuff', etc.)
4
  • you can't "add", you have to rebuild the array. array(0=>array('data_array_index'=>0,etc...)) Commented Oct 29, 2015 at 14:25
  • I'm inside a foreach loop, who rebuilds the array at each loop with the added data. BTW, this is not answering my question.... Commented Oct 29, 2015 at 14:31
  • Just a question, why do you need the key in data_array_index inside the array if you'll probably not be able to access it directly since you won't have the $data_array key to it? Commented Oct 29, 2015 at 14:40
  • I need later to access all data associated with key 'data_array_index' in all multidimensional arrays inside $data_array Commented Oct 29, 2015 at 14:44

5 Answers 5

1

You could do this:

$array = [
   0 => [ "data_array_index" => 0, "stuff" => "stuff" ],
   25 => [ "data_array_index" => 25, "stuff" => "stuff" ]
];

$array[] = array('data_array_index' => 0, 'stuff' => 'stuff')

end($array);
$last_id = key($array);

$array[$last_id]['data_array_index'] = $last_id;

I don't know why you want data_array_index in the array because if you put it in a foreach loop you can get the key without needing the variable.

Example:

foreach($key => $data) {
        ^^^^ The same as `data_array_index`
}
Sign up to request clarification or add additional context in comments.

3 Comments

Inefficent for very large arrays
@Perocat, For as far as i know this is the fastest method.
@Perocat, it is O(1), and can hardly be improved. Could you elaborate in which way it is inefficient ?
0

Suppose you have this array:

$data_array = [
   0 => [ "data_array_index" => 0, "stuff" => "stuff" ],
   25 => [ "data_array_index" => 25, "stuff" => "stuff" ]
];

Now to set a key (note the $data_array[100]):

$data_array[100] = [ "data_array_index" => 100, "stuff" => "stuff" ];

4 Comments

I guess he wanted some kind of "automatism", as he doesn't know the key 100 in advance.
@Jan Then I miss understood.
@Jan yes it's like you describled, it should be "automatic"
I guess there are only two possible solutions for an index: either the end (this can be calculated by count()) or if you splice the array. But in both ways you will end up knowing the index in advance, or am I wrong?
0

try this once

$arr=array(12=>array("stuff"=>"stuff1"),15=>array("stuff"=>"stuff2"));

foreach($arr as $key=>$val){
$arr[$key]['data_array_index']=$key;    
}

echo "<pre>"; print_r($arr);

1 Comment

Inefficent for very large arrays
0

For my solution see the code below. Beware that this is a very rudimentary function now. It does not provide any fail safe or fallback. If you delete a key it will to fill the space etc.

<?php

// data array
$data_array = [];

// additional info for the array
$info_a = "Lorem Ipsum";
$info_b = "Lorem Ipsum";

// call function
addElement($data_array, $info_a);
addElement($data_array, $info_b);

// display output
echo '<pre>';
print_r($data_array);
echo '</pre>';

function addElement(&$array, $info)
{
    // add info to the array
    $array[] = [
        'stuff'=>$info
    ];

    // get the key of the current array
    end($array);
    $key = key($array);

    // add this information to the array
    $array[$key]['data_array_index'] = $key;
}
?>

Output would be

Array
(
    [0] => Array
        (
            [stuff] => Lorem Ipsum
            [data_array_index] => 0
        )

    [1] => Array
        (
            [stuff] => Lorem Ipsum
            [data_array_index] => 1
        )

)

Comments

0

Use array_walk

array_walk($data_array,function($value,$key)use($new_array) {
    $value['data_array_index'] = $key;
    $new_array[$key]=$value;
});

working demo: http://phpfiddle.org/main/code/p937-7cue

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.