0

I have an array that looks like this:

array(
    0 => object //ticket,
    1 => object //user,
    2 => object //employee,
    3 => object //ticket,
    4 => object //user
    5 => object //ticket,
    6 => object //employee
);

From this you can see that the ticket object is always there, whereas the employee and user objects are each optional. What I'd like to do is loop through them and organize them like so:

array(
    [0] => array(
        [0] => object //ticket,
        [1] => object //user,
        [2] => object //employee,
    )
)

What I'm having trouble with is since the user and employee are optional I'm not sure how to correctly index based on the above model since occasionally I will hit one that doesn't have an employee or user (in the case that it doesn't, I'd want that index to be null). Any ideas?

EDIT: Example:

for ($i = 0; $i < count($result); $i++) {
    if ($result[$i] instanceof Ticket) {
        continue;
    } else {
        $newResult[$i][] = $result[$i]; //maybe I'm brainfarting, but cannot figure how to identify the last ticket index
    }
}
4
  • 1
    Can you post an example of what you have tried? That or a theoretical (non-working) example of the code you would like to have would be helpful in understanding exactly what you want to do. code > words :) Commented Jun 27, 2013 at 21:04
  • Well, as soon as you hit another ticker, you stop filling the current array and start using the new one. Commented Jun 27, 2013 at 21:05
  • The objects are associated based on their ids, the objects themselves are doctrine entities and are being retrieved via a query builder join. If there was some way doctrine could do this automatically, I'd be much happier. But for whatever reason it returns them as separate indexes in the array. Commented Jun 27, 2013 at 21:07
  • Why don't you make the subarrays associative, so you don't have to worry about setting indexes to null? Commented Jun 27, 2013 at 21:22

4 Answers 4

1

This is similar to your own answer, but doesn't require reindexing $newResult when it's done.

$newIndex = -1;
$newResult = array();
foreach ($result as $object) {
    if ($object instanceof Ticket) {
        $newResult[] = array($object);
        $newIndex++;
    } else {
        $newResult[$newIndex][] = $object;
    }
}

However, your original question mentioned setting the unused elements of the subarrays to null. Your answer doesn't do that, so I didn't either.

Sign up to request clarification or add additional context in comments.

Comments

0

You can check with instanceof which class' instance current array element is, and then group it as you prefer :)

Example

if( $array[0] instanceof ticket ) {
    // do some magic in here
}

http://php.net/manual/en/internals2.opcodes.instanceof.php

1 Comment

Yup I'm doing that. My problem is how do I lump it in with the last ticket index?
0

Yeah, I definitely brainfarted. Sorry for wasting anyones time, here's the loop:

$lastTicketIndex = 0;
    for ($i = 0; $i < count($result) - 1; $i++) {
        if ($result[$i] instanceof Ticket) {
            $newResult[$i][] = $result[$i];
            $lastTicketIndex = $i;
            continue;
        } else {
            $newResult[$lastTicketIndex][] = $result[$i];
        }
    }

1 Comment

When you're done you should do $newResult = array_values($newResult) to renumber the indexes consecutively.
0

To avoid needing to keep track of indexes or the size of the result array, maintain a reference array for each occurrence of a Ticket instance and push non-Ticket objects into the preceding reference array. Demo

class Ticket { }
class User { }
class Employee { }

$array = [
    new Ticket(),  // Ticket 1
    new User(),    // User 1
    new Employee(),// Employee 1
    new Ticket(),  // Ticket 2
    new User(),    // User 2
    new Ticket(),  // Ticket 3
    new Employee() // Employee 3
];

$result = [];
foreach ($array as $obj) {
    if ($obj instanceof Ticket) {
        unset($ref);
        $ref = [$obj];
        $result[] = &$ref;
    } else {
        $ref[] = $obj;
    }
}
var_export($result);

Output:

array (
  0 => 
  array (
    0 => 
    \Ticket::__set_state(array(
    )),
    1 => 
    \User::__set_state(array(
    )),
    2 => 
    \Employee::__set_state(array(
    )),
  ),
  1 => 
  array (
    0 => 
    \Ticket::__set_state(array(
    )),
    1 => 
    \User::__set_state(array(
    )),
  ),
  2 => 
  array (
    0 => 
    \Ticket::__set_state(array(
    )),
    1 => 
    \Employee::__set_state(array(
    )),
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.