Create PHP array with dynamic data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gpdissanayake
Forum Newbie
Posts: 3
Joined: Wed May 30, 2012 6:32 am

Create PHP array with dynamic data

Post by gpdissanayake »

Dear Sirs

I have an data set as follows

while($row = oci_fetch_array($stid05, OCI_BOTH)) {
echo 'country '. $row[0];
echo 'visits'.$visits[1];
}
I need to create following array dunamiccaly

$series = array( array( country => "TAVGA",
visits => 256,
),
array( country => "hawar",
visits => 257,
),
array( country => "awesar",
visits => 258,
),
array( country => "zhinar",
visits => 259,
),
array( country => "BBB",
visits => 259,
),
);
Kindly assist me, how to generate

Thsnks
Gayan
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Create PHP array with dynamic data

Post by pbs »

Try this code in while loop

$new_array[] = array("country" => COUNTRY_DB_FIELD, "visits" => VISIT_DB_FIELD);
gpdissanayake
Forum Newbie
Posts: 3
Joined: Wed May 30, 2012 6:32 am

Re: Create PHP array with dynamic data

Post by gpdissanayake »

Hellow PBS

I tried. I am getting following array
Array ( [0] => Array ( [country] => AAA [visits] => 1 ) [1] => Array ( [country] => BBB [visits] => 2 ) [2] => Array ( [country] => CCC [visits] => 3 ) )

any idea to get the same as following array
$series = array( array( country => "TAVGA",
visits => 256,
),
array( country => "hawar",
visits => 257,
),
array( country => "awesar",
visits => 258,
),
array( country => "zhinar",
visits => 259,
),
array( country => "BBB",
visits => 259,
),
);

Please help
TylerH4
Forum Newbie
Posts: 10
Joined: Sat May 26, 2012 8:45 pm

Re: Create PHP array with dynamic data

Post by TylerH4 »

Did you try something like this?

Code: Select all

$series = Array();
while($row = oci_fetch_array($stid05, OCI_BOTH)) {
	// Assuming that $row[0] is indeed country and $row[1] is indeed the visit count
	$series[] = Array("country"=>$row[0], "visits"=>$row[1]);
}
That should do it. When you were echoing $row[0] and $row[1] were you getting the correct values?
Last edited by TylerH4 on Thu May 31, 2012 7:39 pm, edited 1 time in total.
gpdissanayake
Forum Newbie
Posts: 3
Joined: Wed May 30, 2012 6:32 am

Re: Create PHP array with dynamic data

Post by gpdissanayake »

Yes Sir, I tried but I am getting something like below

Array ( [0] => Array ( [country] => AAA [visits] => 1 ) [1] => Array ( [country] => BBB [visits] => 2 ) [2] => Array ( [country] => CCC [visits] => 3 ) )
--------------------------------------
when I hard coaded country / visit array, then I am getting results perfecly (kindly refer below) :D

$series = array( array( country => "TAVGA",
visits => 256,
),
array( country => "hawar",
visits => 257,
),
);

But through my code i need to craete same array , (same patten) as above
kindly find code as below

$query05 = "select country,visits from country WHERE ROWNUM < 4";
$stid05 = oci_parse($conn, $query05);
$r05 = oci_execute($stid05);

while($row = oci_fetch_array($stid05, OCI_BOTH)) {

// code here

}

Please fill, inside the loop to generate my array

Thank a lot
Gayan
TylerH4
Forum Newbie
Posts: 10
Joined: Sat May 26, 2012 8:45 pm

Re: Create PHP array with dynamic data

Post by TylerH4 »

Are you sure there are no errors in your query and the query is returning data? Also, can you run the following code and post the exact output?

Code: Select all

$query05 = "select country,visits from country WHERE ROWNUM < 4";
$stid05 = oci_parse($conn, $query05);
$r05 = oci_execute($stid05);

while($row = oci_fetch_array($stid05, OCI_BOTH)) {
	var_dump($row);
}
Post Reply