How to dupe first index

here’s my array dump:

Array
(
    [0] => Array
        (
            [id] => 2854
            [drop_item_and_location] => 0000-00-00 00:00:00
          
        )

    [1] => Array
        (
            [id] => 2
            [drop_item_and_location] => 0000-00-00 00:00:00
            
        )
)

I need to dupe the first index and place it in the [0] position like this:

Array
(
    [0] => Array
        (
            [id] => 2854
            [drop_item_and_location] => 0000-00-00 00:00:00
            
        )

    [1] => Array
        (
            [id] => 2854
            [drop_item_and_location] => 0000-00-00 00:00:00
          
        )

    [2] => Array
        (
            [id] => 2
            [drop_item_and_location] => 0000-00-00 00:00:00
           
        )
)

using $itemandsize = array_merge($itemandsize[0], $itemandsize);

this is as close as I come:

Array
(
    [id] => 2854
    [drop_item_and_location] => 0000-00-00 00:00:00
  
    [0] => Array
        (
            [id] => 2854
            [drop_item_and_location] => 0000-00-00 00:00:00
           
        )

    [1] => Array
        (
            [id] => 2
            [drop_item_and_location] => 0000-00-00 00:00:00
           
        )
)

How do I turn the first 3 key-values pairs into index [0] and index [0] into [1] and [1] into [2]?

this did the trick:

$var = array_slice($itemandsize,0,1);
$var2 = array_merge($var, $itemandsize);
var_dump($var2);

array _slice allows me to keep the index structure

1 Like

Just wondering, what is the actual problem you are trying to solve with this code?

Thought by duplicating the the first index I could trick a loop I was writing into the display I wanted.

Learned that I didn’t know off the top of my head how to get my array properly indexed even with google’s help. So, I took a detour down a rabbit whole and learned something new about PHP arrays via array_slice().

Ultimately, had to debug the loop, any way, when my short cut failed.

I usually track down knowledge I thought I had, but don’t. This was one of those exercises. I love learning new things.

Thanks for asking.

Ok, you got close to what I was asking. Tell me more about the “display” you want and where exactly is this array coming from.

OK.

It comes from my model that selects over 1000 products and sends that data to my view that collapses those products into about 50 descriptions and aggregates their ids into a form input that’s set to display:none; and eliminates duplicate ids.

It’s part a a new path that gives users fewer choices when they want that feature without sacrificing selectability.

Thanks for asking.

Great, now we are on the right track. Still missing some of the overall what and why’s. Perhaps a description from the users point of view might help. It is not clear what “collapses” specifically means or why you ‘aggregate id’s’ or why display is set to none and what it is that is set to none and how you end up with duplicate id’s.

The more information you can provide will allow me/us to give you the best answer to what you are doing. I am pretty sure your current approach is not it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.