Page 1 of 1

Create PHP array with dynamic data

Posted: Wed May 30, 2012 6:34 am
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

Re: Create PHP array with dynamic data

Posted: Wed May 30, 2012 6:39 am
by pbs
Try this code in while loop

$new_array[] = array("country" => COUNTRY_DB_FIELD, "visits" => VISIT_DB_FIELD);

Re: Create PHP array with dynamic data

Posted: Wed May 30, 2012 6:47 am
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

Re: Create PHP array with dynamic data

Posted: Wed May 30, 2012 8:21 am
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?

Re: Create PHP array with dynamic data

Posted: Thu May 31, 2012 12:14 am
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

Re: Create PHP array with dynamic data

Posted: Thu May 31, 2012 7:45 pm
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);
}