1

I'm trying to build a multi array with this structure:

Array
(
    [2] => Array //this is the user's unique ID
        (
            [name] => Jack
            [location] => Somerville, Massachusetts, United States
            [cars] => Array
                (
                    [10] => Toyota //this is the car's unique ID
                    [11] => Porsche
                )
        )

    [5] => Array
        (
            [name] => Luke
            [location] => Schwelm, North Rhine-Westphalia, Germany
            [cars] => Array
                (
                    [1] => Honda
                    [2] => VW
                    [5] => Maserati
                )
        )

    [1] => Array
        (
            [name] => Jabba
            [location] => Denver, Colorado, United States
            [cars] => Array
                (
                    [3] => Tesla
                )
        )
)

I am using this foreach loop but am stuck in getting the cars array embedded within the search data array.

Each user may have more than one car so I would need to loop through all cars for each user, generate that array, and put it in the original foreach loop.

    $search_data = array();
    foreach ($query->result() as $row) {
        $search_data[$row->id] = array(

            'name' => $row->name,
            'location' => $row->location,
            'cars' => array($row->car_id), //this is where I need to insert another array
        );
    }
    return $search_data;

Any suggestions how to do this?

Thanks for helping!

SAMPLE TABLE DATA

USER    NAME    LOCATION    CARS
2       JACK    A           TOYOTA
2       JACK    A           PORSCHE
5       LUKE    B           HONDA
5       LUKE    B           VW 
5       LUKE    B           MASERATI
1       JABBA   C           TESLA
3
  • shouldent $row->car_id be an array already? Commented May 22, 2011 at 4:13
  • i just put it there to show where i need help - leaving it like that will only return a single car, not the array of cars a user may have -- i need to loop thru all cars for each user Commented May 22, 2011 at 4:24
  • it's hard to put cars into an array if it's separated on different row, because when you put it in an array, the next result of the same query goes waste, you got my idea? Commented May 22, 2011 at 4:34

1 Answer 1

2

It seems that you are creating the array through a database table. So can you please give 2 or more sample data. it'll be easier to give an answer if sample data is there.

Edit: Well this might not be the best code but I think you'll be able to figure out a better way after seeing this

$id = $row['id'];
    if (!isset($search_data[$id])){
        $search_data[$id] = array();
    }
    $search_data[$id]['name'] = $row['name'];
    $search_data[$id]['location'] = $row['location'];
    if (isset($search_data[$id]['cars'])) {
        array_push($search_data[$id]['cars'],$row['cars']);
    }else{
        $search_data[$id]['cars'] = array($row['cars']); //this is where I need to insert another array
    }
Sign up to request clarification or add additional context in comments.

3 Comments

just put some sample data in the OP - let me know what you think, thanks
@torr This is the array I got with this solution Array ( [2] => Array ( [name] => Jack [location] => A [cars] => Array ( [0] => Porsche [1] => Toyota ) ) [5] => Array ( [name] => Luke [location] => B [cars] => Array ( [0] => Honda [1] => VW [2] => Maserati ) ) [1] => Array ( [name] => Jabba [location] => C [cars] => Array ( [0] => Tesla ) ) )
actually your idea to use array_push works very nicely for this situation - not sure if other solutions would be more efficient than this - thanks for helping!

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.