Jump to content

Recommended Posts

I have a question about reordering an array. When I print the array it looks like this:

Array

(

    [1] => Array

        (

            [Cushion] => Cushion 1

            [Fabric] => f-111111

            [FabricPrice] => 0

            [Fill] => Fiber

            [button] => none

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 5

            [WeltSize] =>

            [sKU] => c-111111

            [Edge] => Knife

            [Cap] => N

            [straps] => N

            [Hinged] => N

            [Type] => Boxed Button

            [Price] => 54.25

            [Total] => 271.25

        )

 

    [2] => Array

        (

            [Cushion] => Cushion 1

            [Fabric] => f-111111

            [FabricPrice] => 0

            [Fill] => Fiber

            [button] => none

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 3

            [WeltSize] =>

            [sKU] => c-111111

            [Edge] => Knife

            [Cap] => N

            [straps] => N

            [Hinged] => N

            [Type] => Boxed Button

            [Price] => 54.25

            [Total] => 162.75

        )

 

)

 

Now if I delete an entry or 2 it looks like this:

Array

(

    [3] => Array

        (

            [Cushion] => Cushion 1

            [Fabric] => f-111111

            [FabricPrice] => 0

            [Fill] => Fiber

            [button] => none

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 5

            [WeltSize] =>

            [sKU] => c-111111

            [Edge] => Knife

            [Cap] => N

            [straps] => N

            [Hinged] => N

            [Type] => Boxed Button

            [Price] => 54.25

            [Total] => 271.25

        )

 

    [6] => Array

        (

            [Cushion] => Cushion 1

            [Fabric] => f-111111

            [FabricPrice] => 0

            [Fill] => Fiber

            [button] => none

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 3

            [WeltSize] =>

            [sKU] => c-111111

            [Edge] => Knife

            [Cap] => N

            [straps] => N

            [Hinged] => N

            [Type] => Boxed Button

            [Price] => 54.25

            [Total] => 162.75

        )

 

)

 

Is it possible to get the arrays to reorder so that the values are [1] and [2] again?

 

I tried this:

array_values($_SESSION['Array']);

But it doesn't do anything for some reason

Link to comment
https://forums.phpfreaks.com/topic/227177-reordering-arrays/
Share on other sites

First of all, you are talking about re-indexing the array, not reordering. Secondly, an array "should" start with a 0 index not 1.

 

An easy way to re-index an array so the indexes start at 0 and increment sequantially is to use the function array_values(). But array_values() doesn't affect the array used int eh function - it returns an array (that will be numerically indexed). So, to change the original array you need to assign the returned value to the variable you want affected - like so:

$array = $array_values($array);

 

EDIT:

Looking at what you are trying to do, I would highly recommend you do NOT try to reindex the entire $_SESSION variable as it will affect EVERYTHING in the session variable. Assuming those values above are orders, I would store those then in a sub-array of the $_SESSION variable under $_SESSION['orders'] (or something similar). You could then reindex the orders without affecting anything else storded in the session variable using

$_SESSION['orders'] = $array_values($_SESSION['orders']);

 

Link to comment
https://forums.phpfreaks.com/topic/227177-reordering-arrays/#findComment-1171887
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.