PHP For Each Append Array

Question!

Example code array: https://eval.in/639002

A guest with (user_id = 1) bought multiple tickets (ticket_id = 1 & 2) On event (event_id = 11).

I am expecting the result format:

[ticket] => Array
(
  [1] => Ticket Name, ***
  [2] => Ticket Name
)

Code example below:

$data_db = array(
    array(
        'id' => 1,
        'user_id' => 1,
        'event_id' => 11,
        'ticket_id' => 1,
        'user_name' => 'guest 1'
    ),
    array(
        'id' => 2,
        'user_id' => 2,
        'event_id' => 11,
        'ticket_id' => 1,
        'user_name' => 'guest 2'
    ),
    array(
        'id' => 3,
        'user_id' => 3,
        'event_id' => 22,
        'ticket_id' => 1,
        'user_name' => 'guest 3'
    ),
    array(
        'id' => 4,
        'user_id' => 1,
        'event_id' => 11,
        'ticket_id' => 2,
        'user_name' => 'guest 1'
    )
);

$output = [];

foreach ( $data_db as $key => $row ) {

    $output[ $row['event_id'] ]['event'] = 'Event Name';    
    $output[ $row['event_id'] ]['attendee'][ $row['user_id'] ] = $row;

    $output[ $row['event_id'] ]['attendee'][ $row['user_id'] ]['ticket'][ $row['ticket_id'] ] = 'Ticket Name';

}

print_r($output);

Current result

Array
(
    [11] => Array
        (
            [event] => Event Name
            [attendee] => Array
                (
                    [1] => Array
                        (
                            [id] => 4
                            [user_id] => 1
                            [event_id] => 11
                            [ticket_id] => 2
                            [user_name] => guest 1
                            [ticket] => Array
                                (
                                    [2] => Ticket Name
                                )

                        )

                    [2] => Array
                        (
                            [id] => 2
                            [user_id] => 2
                            [event_id] => 11
                            [ticket_id] => 1
                            [user_name] => guest 2
                            [ticket] => Array
                                (
                                    [1] => Ticket Name ***
                                )

                        )

                )

        )

    [22] => Array
        (
            [event] => Event Name
            [attendee] => Array
                (
                    [3] => Array
                        (
                            [id] => 3
                            [user_id] => 3
                            [event_id] => 22
                            [ticket_id] => 1
                            [user_name] => guest 3
                            [ticket] => Array
                                (
                                    [1] => Ticket Name
                                )

                        )

                )

        )

)
By : tonoslfx


Answers

First of you could start by sorting out an array of tickets of which ticket belongs to which user:

$output = [];

foreach($data_db as $key=>$row) {
  // Define new $tickets Array E.g ['guest_name']=>['ticket_id']
  $tickets[$row['user_name']][] = $row['ticket_id'];
  $output [] = [
               'id'=>$row['id'], 
               'user_id'=>$row['user_id'], 
               'event_id'=>$row['event_id'], 
               'user_name'=>$row['user_name'],
               'tickets'=>[]
              ];
}

Your output of $tickets would look like:

Array
(
    [guest 1] => Array
        (
            [0] => 1
            [1] => 2
        )
    [guest 2] => Array
        (
            [0] => 1
        )
    [guest 3] => Array
        (
            [0] => 1
        )
)

Then you can combine them, like this:

foreach($output as $k=>$v){
 foreach($tickets as $guest=>$ticket){

    if($v['user_name'] == $guest){
      $output[$k]['tickets'] = $ticket;
    }

  }
}

Your output is:

Array
(
    [0] => Array
        (
            [id] => 1
            [user_id] => 1
            [event_id] => 11
            [user_name] => guest 1
            [tickets] => Array
                (
                    [0] => 1
                    [1] => 2
                )

        )

    [1] => Array
        (
            [id] => 2
            [user_id] => 2
            [event_id] => 11
            [user_name] => guest 2
            [tickets] => Array
                (
                    [0] => 1
                )
              ........


If n is not an ol element, self::ol yields an empty node set. What is n.xpath(...) supposed to return when the result of the expression is an empty node set?

An empty node set is "falsy" in XPath, but you're not evaluating it as a boolean in XPath, only in Python. Is an empty node set falsy in Python?

If that's the problem, you could fix it by changing the if statement to

if n.xpath('boolean(self::ol)'):

or

if n.xpath('count(self::ol) > 1'):
By : LarsH


The descriptions are mandatory for any content you or any frameworks you link against attempt to access. The errors are generated upon an attempt to access the content if a usage description was not supplied, so if you're getting those errors your app must be requesting them. You should discover why your app or its frameworks require these and add appropriate usage descriptions to your app's info.plist.

Or more ideally, if you don't need access, see if there's a way to not request it (or use frameworks that do unnecessarily).



This video can help you solving your question :)
By: admin