Jump to content

array help


blinks

Recommended Posts

I think this is probably a fairly simple question, but I just can't see the solution!

 

I have this bit of code -

		$res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);					foreach ($res as $row) {
		if ($row[type'] == 3) { //record
			$pidlist[]['pid'] = $row['pid'];
			$pidlist[]['date'] = $row['date'];
		}
		elseif ($row[type'] == 2){ //collection
			$interim_pidlist[] = $row['pid'];
		}
	}

 

which is yielding the following output for $pidlist-

Array
(
    [0] => Array
        (
            [pid] => changeme:150
        )

    [1] => Array
        (
            [date] => 2008-07-14 12:02:32
        )

    [2] => Array
        (
            [pid] => changeme:152
        )

    [3] => Array
        (
            [date] => 2008-07-14 12:18:17
        )

    [4] => Array
        (
            [pid] => changeme:65
        )

 

But I want the output to be

 

Array

(

    [0] => Array

        (

            [pid] => changeme:150

            [date] => 2008-07-14 12:02:32

        )

 

    [1] => Array

        (

            [pid] => changeme:152

            [date] => 2008-07-14 12:18:17

        ) ...

[/code]

 

I thought the answer would be using such as $pidlist['pid'][], but this yields -

 

Array

(

    [pid] => Array

        (

            [0] => changeme:150

            [1] => changeme:152

            )

 

    [date] => Array

        (

            [0] => 2008-07-14 12:02:32

            [1] => 2008-07-14 12:18:17...

 

Link to comment
https://forums.phpfreaks.com/topic/183097-array-help/
Share on other sites

You can get this working by explicitly using a counter, such as $i = 0.

 

$i = 0;
$res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);               
foreach ($res as $row) {
         if ($row[type'] == 3) { //record
            $pidlist[$i]['pid'] = $row['pid'];
            $pidlist[$i]['date'] = $row['date'];
         }
         elseif ($row[type'] == 2){ //collection
            $interim_pidlist[] = $row['pid'];
         }
    $i++;
      }

Link to comment
https://forums.phpfreaks.com/topic/183097-array-help/#findComment-966307
Share on other sites

Thanks for your speedy help; will give this a go on Monday.

 

I did actually try what you suggested, but I realise now that I had set up multiple instances of $pidlist[$i]['pid'] = $row['pid']; etc throughout the code, re-initialising i back to 0 at each instance.

 

Thanks so much!

Link to comment
https://forums.phpfreaks.com/topic/183097-array-help/#findComment-966310
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.