Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Tricky PHP Array Problem

Featured Replies

PHP

 

I've been trying to figure out how to do this, but my brain hurts now so I thought I would pass it out to the community. It's along the lines of cycling through an array and operating on the data, but for the life of me I don't know how to do it.

 

I have an array of arrays as follows:

 

Array

(

[0] => Array

(

[0] => 75

[1] => selNumAdultTickets

[2] => 1

)

 

[1] => Array

(

[0] => 75

[1] => selNumChildTickets

[2] => 1

)

 

[2] => Array

(

[0] => 75

[1] => selNumConcTickets

[2] => 1

)

 

[3] => Array

(

[0] => 76

[1] => selNumAdultTickets

[2] => 2

)

 

[4] => Array

(

[0] => 76

[1] => selNumChildTickets

[2] => 2

)

 

[5] => Array

(

[0] => 76

[1] => selNumConcTickets

[2] => 2

)

 

)

 

If you are wondering - the [0] represents the ID of a Live Event, [1] tells me whether its an adult, child or concession ticket and [2] tells me the quantity of the tickets

 

I need to condense this down into an array thus :

 

Array

(

[0] => Array

(

[0] => 75

[1] => selNumAdultTickets

[2] => 1

[3] => selNumChildTickets

[4] => 1

[5] => selNumConcTickets

[6] => 1

)

[1] => Array

(

[0] => 76

[1] => selNumAdultTickets

[2] => 2

[3] => selNumChildTickets

[4] => 2

[5] => selNumConcTickets

[6] => 2

)

)

 

I need to do this so it's all neat and tidy when I need to input it into the mysql database. I could condense it further by removing the 'selNumAdultTickets', 'selNumChildTickets', 'selNumConcTickets' from the array but for now I'll keep it in there so it's easier to understand what's going on.

 

Does what I'm asking make sense because I've found it quite difficult to define the problem - which is probably why I can't come up with the answer! I've basically got to cycle through the top array and group all the arrays that have the same event ID.

 

Any solutions I would be amazingly appreciative (and have uber respect) !

Gotcha.

 

Best thing would be not to try and condense the array, but build a new array, in the condensed manner.

 

Build your new array using the event IDs as the index... Remember empty [] will auto increment arrays in PHP, and you do not need to start at 0.

 

$newArray['75'][] = selNumChildTickets

$newArray['75'][] = 2

$newArray['75'][] = selNumAdultTickets

$newArray['75'][] = 2

 

would work. now to make it dynamic you can use a foreach for your un-condensed array

 

foreach($originalarray as $key => $value)

{

// $value[0] would be the event id, and so on

$newArray[$value[0]][] = $value[1];

$newArray[$value[0]][] = $value[2];

}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.