Create PHP associative array using explode Codeigniter - php

I am a newbie in Codeigniter and created a form to input list of website from user where user either can insert website urls in a textarea separated by line or upload csv file contain a header named websites. I am using codeigniter library CSVReader to read data from csv and create an array like this:
Array ( [0] => Array ( [websites] => www.google.com ) [1] => Array ( [websites] => www.bing.com ) )
while if I try to convert array from PHP explode function (if user input websites through textarea) Array looks like this:
Array(www.google.com,www.bing.com)
Code of my CSVReader is as:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
/**
* Parse a file containing CSV formatted data.
*
* #access public
* #param string
* #param boolean
* #return array
*/
function parse_file($p_Filepath, $p_NamedFields = true) {
$content = false;
$file = fopen($p_Filepath, 'r');
if($p_NamedFields) {
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
}
while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {
if( $row[0] != null ) { // skip empty lines
if( !$content ) {
$content = array();
}
if( $p_NamedFields ) {
$items = array();
// I prefer to fill the array with values of defined fields
foreach( $this->fields as $id => $field ) {
if( isset($row[$id]) ) {
$items[$field] = $row[$id];
}
}
$content[] = $items;
} else {
$content[] = $row;
}
}
}
fclose($file);
return $content;
}
}
Could someone please help me create array in same styles?

I don't think you need a whole big CSV reader to do that. Also, CSV is meant more for tabular data (if you had multiple columns of data), you're just building a flat list of websites - which greatly simplifies things. I would recommend something like this;
$websites = "foo,bar\nbaz,qux"; // Input
$output = preg_split('#[\n\r\t,]+#', $websites); // Split by newlines, feeds, tabs, and commas
foreach ($output as &$o)
$o = array('website' => trim($o));
// Or a shorter form for PHP 5+
foreach ($output as &$o)
$o = ['website' => trim($o)];
If you're getting the data from a form, you can completely strip out the CSV code unless you expect to be handling quotes or CSV file uploads. In that case, you only need one of the loops to make the array associative.
If the data is a multidimensional array of values you want to make associative, you can use the following loop (I made up the columns, they can be whatever you want):
// For PHP 4-
foreach ($output as &$o)
$o = array( 'website' => $o[0], 'email' => $o[1], 'foo' => $o[2], 'bar'=>$o[3] );
// Or a shorter form for PHP 5+
foreach ($output as &$o)
$o = [ 'website' => $o[0], 'email' => $o[1], 'foo' => $o[2], 'bar'=>$o[3] ];
EDIT: Added trimming in first sample code.

You want both arrays to match, but you did not specify which style you want the arrays to end up in.
If you want to convert the CSV array do the following:
foreach($csv_array as $website) {
$website_array[] = $website['websites'];
}
If you want to convert the textarea array do the following:
$count = 0;
foreach($textarea_input as $website) {
$website_array[$count]['websites'] = $website;
$count++;
}
I think the first one is easier to manage, there's not reason to add a second layer to your array, better to extract your data into a simpler array from the array that has multiple layers.

Related

How to convert few columns as JSON array in php while csv import

i have a csv file, please see this below image of my CSV
and this is my table
i need a help to split few columns and convert as an array and insert in to table, lets say i want to exclude the first column which is 'product_id' from the array and remaining all columns as array and insert in to table, so the expected query will be like insert into process_detail (product_id,product_attributes) values ($product_id,'[{"color":"blue","style":"classic","material_type":"etc","length":"35 cm","price":"11.25","product_description":"etc"}]';
note: my csv headers which are in the first row in CSV file will match exactly with Mysql table headers
this is my php
function convert($string)
{
return htmlspecialchars($string,ENT_QUOTES);
}
$columnArray = array();
$dataArray = array();
$firstRule = true;
while ($data = fgetcsv ($source, 1000, ","))
{
if($firstRule)
{
foreach($data as $columnName)
{
$columnArray[] = $columnName;
}
$firstRule = false;
}
else
{
$rule = array();
for($i = 0; $i < count($data) ; $i++)
{
$rule[$columnArray[$i]] = $data[$i];
}
$dataArray[] = $rule;
}
}
foreach($dataArray as $data)
{
// here i am stuck, i would want to json_encode of few columns and insert into table
}
echo $product_id; var_dump($bound_values);
mlarray(2) { ["product_id"]=> string(2) "ml" ["product_attributes"]=> string(1588) "{"cv_id":"weight_2_kg","sv_id":"label_3","collection_id":"length_3_cm","season":"width_3_cm","hit":"height_3_cm","tier":"diameter_3_cm","style_no":"size_3_ml","base_product_id":"weight_3_kg","product_title":"label_4","introduction":"length_4_cm","color_name":"width_4_cm","price_a":"height_4_cm","color_price_a":"diameter_4_cm","sku_price_a":"size_4_ml","price_b":"weight_4_kg","color_price_b":"label_5","price_c":"length_5_cm","color_price_c":"width_5_cm","sku_price_c":"height_5_cm","product_availability_b":"diameter_5_cm","product_availability_c":"size_5_ml","product_availability_d":"weight_5_kg","returnable_a":"label_6","returnable_b":"length_6_cm","returnable_c":"width_6_cm","collect_a":"height_6_cm","collect_b":"diameter_6_cm","collect_c":"size_6_ml","brand":"weight_6_kg","group_name":"label_7","department":"length_7_cm","class_name":"width_7_cm","sub_class":"height_7_cm","category_1":"diameter_7_cm","category_2":"size_7_ml","category_3":"weight_7_kg","category_4":"label_8","category_5":"length_8_cm","copy_writing_agency":"width_8_cm","photographer_name":"height_8_cm","photography_date":"diameter_8_cm","event":"size_8_ml","status":"weight_8_kg","style":"label_9","p_family":"length_9_cm","pf_name":"width_9_cm","feature_1":"height_9_cm","primary_material":"diameter_9_cm","secondary_material":"size_9_ml","type_name":"weight_9_kg","material_finish_1":"label_10","material_finish_2":"length_10_cm","for_name":"width_10_cm","gender":"height_10_cm","care_instructions":"diameter_10_cm","fragile":"size_10_ml","assembly_required":"weight_10_kg","unit_components":"segment"}" } 2536789array(2) { ["product_id"]=> string(7) "2536789" ["product_attributes"]=> string(1570) "{"cv_id":"","sv_id":"","collection_id":"","season":"JJ17","hit":"JJ17","tier":"TIER1","style_no":"11DD DV","base_product_id":"","product_title":"something","introduction":"","color_name":"blue","price_a":"169","color_price_a":"","sku_price_a":"","price_b":"189","color_price_b":"","price_c":"1.5","color_price_c":"","sku_price_c":"","product_availability_b":"","product_availability_c":"","product_availability_d":"","returnable_a":"","returnable_b":"","returnable_c":"","collect_a":"","collect_b":"","collect_c":"","brand":"brand a","group_name":"group b","department":"something else","class_name":"something","sub_class":"","category_1":"","category_2":"","category_3":"","category_4":"","category_5":"","copy_writing_agency":"","photographer_name":"","photography_date":"","event":"exclusive","status":"Active","style":"Basic","p_family":"something","pf_name":"something else","feature_1":"","primary_material":"Glass","secondary_material":"Glass","type_name":"something","material_finish_1":"something","material_finish_2":"","for_name":"","gender":"","care_instructions":"","fragile":"Yes","assembly_required":"","unit_components":"","unit_pack_length_cm":"24","unit_pack_width_cm":"16","unit_pack_height_cm":"32","unit_pack_weight_g":"","length_cm":"","width_cm":"","height_cm":"","weight_kg":"","size_ml":"250","diameter_cm":"","set_includes":"","label_1":"","length_1_cm":"","width_1_cm":"","height_1_cm":"","diameter_1_cm":"","size_1_ml":"","weight_1_kg":"","label_2":"","length_2_cm":"","width_2_cm":"","height_2_cm":"","diameter_2_cm":"","size_2_":"","":""}" }INSERT INTO process_detail (process_id,product_id,product_attributes) values(1,'2536789','[{"cv_id":"","sv_id":"","collection_id":"","season":"JJ17","hit":"JJ17","tier":"TIER1","style_no":"11DD DV","base_product_id":"","product_title":"something","introduction":"","color_name":"blue","price_a":"169","color_price_a":"","sku_price_a":"","price_b":"189","color_price_b":"","price_c":"1.5","color_price_c":"","sku_price_c":"","product_availability_b":"","product_availability_c":"","product_availability_d":"","returnable_a":"","returnable_b":"","returnable_c":"","collect_a":"","collect_b":"","collect_c":"","brand":"brand a","group_name":"group b","department":"something else","class_name":"something","sub_class":"","category_1":"","category_2":"","category_3":"","category_4":"","category_5":"","copy_writing_agency":"","photographer_name":"","photography_date":"","event":"exclusive","status":"Active","style":"Basic","p_family":"something","pf_name":"something else","feature_1":"","primary_material":"Glass","secondary_material":"Glass","type_name":"something","material_finish_1":"something","material_finish_2":"","for_name":"","gender":"","care_instructions":"","fragile":"Yes","assembly_required":"","unit_components":"","unit_pack_length_cm":"24","unit_pack_width_cm":"16","unit_pack_height_cm":"32","unit_pack_weight_g":"","length_cm":"","width_cm":"","height_cm":"","weight_kg":"","size_ml":"250","diameter_cm":"","set_includes":"","label_1":"","length_1_cm":"","width_1_cm":"","height_1_cm":"","diameter_1_cm":"","size_1_ml":"","weight_1_kg":"","label_2":"","length_2_cm":"","width_2_cm":"","height_2_cm":"","diameter_2_cm":"","size_2_":"","":""}]')
this is my csv content
product_id,cv_id,sv_id,collection_id,season,hit,tier,style_no,base_product_id,product_title,introduction,color_name,price_a,color_price_a,sku_price_a,price_b,color_price_b,price_c,color_price_c,sku_price_c,product_availability_b,product_availability_c,product_availability_d,returnable_a,returnable_b,returnable_c,collect_a,collect_b,collect_c,brand,group_name,department,class_name,sub_class,category_1,category_2,category_3,category_4,category_5,copy_writing_agency,photographer_name,photography_date,event,status,style,p_family,pf_name,feature_1,primary_material,secondary_material,type_name,material_finish_1,material_finish_2,for_name,gender,care_instructions,fragile,assembly_required,unit_components,unit_pack_length_cm,unit_pack_width_cm,unit_pack_height_cm,unit_pack_weight_g,length_cm,width_cm,height_cm,weight_kg,size_ml,diameter_cm,set_includes,label_1,length_1_cm,width_1_cm,height_1_cm,diameter_1_cm,size_1_ml,weight_1_kg,label_2,length_2_cm,width_2_cm,height_2_cm,diameter_2_cm,size_2_ml,weight_2_kg,label_3,length_3_cm,width_3_cm,height_3_cm,diameter_3_cm,size_3_ml,weight_3_kg,label_4,length_4_cm,width_4_cm,height_4_cm,diameter_4_cm,size_4_ml,weight_4_kg,label_5,length_5_cm,width_5_cm,height_5_cm,diameter_5_cm,size_5_ml,weight_5_kg,label_6,length_6_cm,width_6_cm,height_6_cm,diameter_6_cm,size_6_ml,weight_6_kg,label_7,length_7_cm,width_7_cm,height_7_cm,diameter_7_cm,size_7_ml,weight_7_kg,label_8,length_8_cm,width_8_cm,height_8_cm,diameter_8_cm,size_8_ml,weight_8_kg,label_9,length_9_cm,width_9_cm,height_9_cm,diameter_9_cm,size_9_ml,weight_9_kg,label_10,length_10_cm,width_10_cm,height_10_cm,diameter_10_cm,size_10_ml,weight_10_kg,segment
2536789,,,,JJ17,JJ17,TIER1,11DD DV,,something,,blue,169,,,189,,1.5,,,,,,,,,,,,brand a,group b,something else,something,,,,,,,,,,exclusive,Active,Basic,something,something else,,Glass,Glass,something,something,,,,,Yes,,,24,16,32,,,,,,250,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
thanks in advance.
There are a handful of ways to approach this problem, however this is the way that I would solve it.
Note that this is untested code, so please either troubleshoot or else ask in comments if there's some issue / error.
NOTE: the size limit in the fgetcsv is optional, and not necessary, and in this case, causing problems - so remove the limit per the code below.
I've commented the code below to tell you what's going on at each step:
// declare an empty array to contain the column keys
$keys = [];
// load the keys into the $keys array
// load the first row
$data = fgetcsv( $source );
// loop over it, adding the keys into the array
foreach( $data AS $value ) {
if ( $value ) {
$keys[] = $value;
}
}
/**
* $keys should now be an array that looks like so:
*
* $keys = array(
* 0 => 'product_id',
* 1 => 'color',
* 2 => 'style',
* 3 => 'type',
* ..etc
* );
*/
// now process the rest of the csv file
while ($data = fgetcsv ( $source ) ) {
// reset the attributes array, where we'll store the (future) JSON attributes
$attributes = [];
// reset product_id to ensure pick up the new / correct value
$product_id = NULL;
// loop over the columns in the row
foreach( $data as $index => $value ) {
// set the key (based on the keys array established above)
$key = $keys[ $index ];
// load the values into the attributes array (unless product id, where we put that in the $product_id variable)
if ( 'product_id' != $key ) {
$attributes[ $key ] = $value;
} else {
$product_id = $value;
}
}
if ( empty( $attributes ) || ! $product_id ) {
continue;
}
// prepare the values for a PDO database insert
$bound_values = [
'product_id' => $product_id,
'product_attributes' => json_encode( $attributes )
];
// insert into the database
$stmt = $db->prepare( "INSERT INTO product_table SET product_id = :product_id, product_attributes = :product_attributes" );
$stmt->execute( $bound_values );
}

Adding a second dimension to an exisiting PHP array

Language: PHP 5
Framework: Joomla 3
I have a single dimension array that I get from get_file_array(). I need to break down the names of the files and eventually query the database for more information. For ease of iteration, I was hoping to add a new dimension to my array rather than create parallel arrays. I thought of iterating through the existing array and adding it one node at a time to a new, specifically multidimensional, perhaps associative array, but this seems inelegant. Is there a way to add a dimension to an existing array in PHP? Here's what I tried last, which obviously doesn't work but conveys the spirit of what I want to accomplish:
require_once "../components/com_esms/models/officelookup.php";
class filearray extends JApplicationCli
{
var $dir = null;
//var_dump($dir);
//error_log("filecopy CWD: ".getcwd());
//error_log("filecopy files: ".print_r($dir, true));
function __construct()
{
$this->dir = scandir("../esms_reports/", 0);
parent::__construct();
}
public function get_file_array()
{
//$this->out('Hello World');
unset($this->dir[0]);
unset($this->dir[1]);
$fa = array_values($this->dir);
return $fa;
}
}
$arr_filearray_obj = new filearray();
$dir = $arr_filearray_obj->get_file_array();
//error_log("filecopy files: ".print_r($dir, true));
/*
foreach($dir as $filename)
{
$fa_underscore = explode("_", $filename);
error_log("filecopy lotid: ".$fa_underscore[1]);
}
*/
//$officeid = array();
for($i = 0; $i < count($dir); $i++)
{
$fa_underscore = explode("_", $dir[$i]);
//error_log("filecopy lotid: ".$fa_underscore[1]);
//error_log("filecopy number of lots: ".$i);
$fa_dash = explode("-", $fa_underscore[1]);
$dir[i][1] = $fa_dash[1];
}
error_log("filecopy officeids: ".print_r($dir, true));
//$result = file_get_contents("http://192.168.1.250/systemname/index.php/option=com_esms&view=officelookup&format=json&officeID=".$officeid[0]);
$officelookup = new EsmsModelofficelookup();
for($o = 0; $o < count($dir); $o++)
{
$result = $officelookup->get_offices($dir[$o][1]);
}
error_log("filecopy JSON: ".$result);
echo("DONE!");
EDIT: Here's an example of the file names I am manipulating. The point is to get the client id, query it, check if it has a parent client id and make a folder structure based on that, using the client names, not IDs.
DDR_1426112931-429_031215.pdf or typeofreport_lotid-clientid_date.pdf
What I would want to add to the array would be the results of my database query, which is a JSON encoded structure containing the client's information and the parent client information, if it exists. It looks something like this:
Array
(
[id] => 123
[name] => Dummy
[parent] => 321
)
Array
(
[id] => 321
[name] => DummyParent
[parent] =>
)

What is the best way to search through an array to return the key of a sub value

I'm trying to filter an array (derived from a json object), so as to return the array key based on the value. I'm not sure if array search $key = array_search($value, $array); is the best way to do this (I can't make it work), and I think there must be a better way.
So far I've got this, but it isn't working. Grateful for any help!
public function getBedroomData(array $data,$num_beds = null,$type) {
$data = (array) $data;
if($num_beds > 0) {
$searchstring = "avg_".$num_beds."bed_property_".$type."_monthly";
} else {
$searchstring = "avg_property_".$type."_monthly";
}
$avg_string = array_search($data, $searchstring);
return $avg_string;
}
The array consists of average property prices taken from the nestoria api as follows:
http://api.nestoria.co.uk/api?country=uk&pretty=1&action=metadata&place_name=Clapham&price_type=fixed&encoding=json
This returns a long json object. My problem is that the data isn't consistent - and I'm looking for the quickest (run time) way to do the following:
$data['response']['metadata']['0'] //= data to return, [0] unknown
$data['response']['metadata']['0']['metadata_name'] = "avg_1bed_property_rent_monthly" //= string I know!
$data['response']['metadata']['1'] //= data to return, [1] unknown
$data['response']['metadata']['1']['metadata_name'] = "avg_1bed_property_buy_monthly" //= string I know!
$data['response']['metadata']['2'] = //= data to return, [2] unknown
$data['response']['metadata']['2']['metadata_name'] = "avg_2bed_property_buy_monthly" //= string I know!
.....
.....
.....
$data['response']['metadata']['10'] = avg_property_rent_monthly
$data['response']['metadata']['11'] = avg_property_buy_monthly
$data['response']['metadata'][most_recent_month] = the month reference for getting the data from each metadata list..
It isn't possible to filter the initial search query by number of bedrooms as far as I can work out. So, I've just been array slicing the output to get the information I've needed if bedrooms are selected, but as the data isn't consistent this often fails.
To search inside that particular json response from nestoria, a simple foreach loop can be used. First off, of course call the json data that you need. Then, extract the whole data, the the next step if pretty straightforward. Consider this example:
$url = 'http://api.nestoria.co.uk/api?country=uk&pretty=1&action=metadata&place_name=Clapham&price_type=fixed&encoding=json';
$contents = file_get_contents($url);
$data = json_decode($contents, true);
$metadata = $data['response']['metadata'];
// dummy values
$num_beds = 1; // null or 0 or greater than 0
$type = 'buy'; // buy or rent
function getBedroomData($metadata, $num_beds = null, $type) {
$data = array();
$searchstring = (!$num_beds) ? "avg_property_".$type."_monthly" : "avg_".$num_beds."bed_property_".$type."_monthly";
$data['metadata_name'] = $searchstring;
$data['data'] = null;
foreach($metadata as $key => $value) {
if($value['metadata_name'] == $searchstring) {
$raw_data = $value['data']; // main data
// average price and data points
$avg_price = 0;
$data_points = 0;
foreach($raw_data as $index => $element) {
$avg_price += $element['avg_price'];
$data_points += $element['datapoints'];
}
$data_count = count($raw_data);
$price_average = $avg_price / $data_count;
$data_points_average = $data_points / $data_count;
$data['data'][] = array(
'average_price' => $price_average,
'average_datapoints' => $data_points_average,
'data' => $raw_data,
);
}
}
return $data;
}
$final = getBedroomData($metadata, $num_beds, $type);
print_r($final);

Create new array from existing array in php

Good Day
I have an array containing data seperated by a comma:
array (
[0]=>Jack,140101d,10
[1]=>Jack,140101a,15
[2]=>Jack,140101n,20
[3]=>Jane,141212d,20
[4]=>Jane,141212a,25
[5]=>Jane,141212n,30
)
There is a lot of data and I would like the data to be set out as:
array(
[Jack]=>
[140101]
=>[d] =>10
=>[a] =>15
=>[n] =>20
)
My code:
foreach ($out as $datavalue) {
$dat = str_getcsv($datavalue,',');
$datevalue = substr($dat[1],2,-1);
$shiftvalue = substr($dat[1],-1);
$totalvalue = $dat[2];
$sval[$shiftvalue] = $totalvalue;
$dval[$datevalue] = $sval;
$opvalue = $dat[0];
$final[$opvalue] = $dval;
}
Now it seems the array is populated even if there is no data from the original string, so my output shows results for Jack on the other dates even though there was no data for him originally. Hope this makes sense. Could anyone point out or suggest a solution please?
As mentioned in the comments, explode is what you need. See this working here.
<?php
$input = array (
0 => 'Jack,140101d,10',
1 => 'Jack,140101a,15',
2 => 'Jack,140101n,20',
3 => 'Jane,141212d,20',
4 => 'Jane,141212a,25',
5 => 'Jane,141212n,30',
);
$result = array();
foreach ($input as $key => $value) {
$valueParts = explode(',',$value); // now valueparts is an array like ('Jack','140101d','10')
$namePart = $valueParts[0];
$idPart = substr($valueParts[1],0,-1); // we need to strip the letter from the id
$charPart = substr($valueParts[1],-1); // and the id from the letter
$nrPart = $valueParts[2]; // you could use intval() to make this an integer rather than a string if you want
// Now we fill the array
if(!array_key_exists($namePart, $result)) {
$result[$namePart] = array();
}
if(!array_key_exists($idPart, $result[$namePart])) {
$result[$namePart][$idPart] = array();
}
if(!array_key_exists($charPart, $result[$namePart][$idPart])) {
$result[$namePart][$idPart][$charPart] = $nrPart;
}
}
var_dump($result);

Converting array into multidimensional array in PHP when result is send from Oracle

Here is example how my array should look like:
$library = array(
'book' => array(
array(
'authorFirst' => 'Mark',
'authorLast' => 'Twain',
'title' => 'The Innocents Abroad'
),
array(
'authorFirst' => 'Charles',
'authorLast' => 'Dickens',
'title' => 'Oliver Twist'
)
)
);
When I get results from oracle database:
$row = oci_fetch_array($refcur, OCI_ASSOC+OCI_RETURN_NULLS);
But when I execute my code I only get one row.
For example: <books><book></book><name></name></books>
But I want all rows to be shown in xml.
EDIT:
This is my class for converting array to xml:
public static function toXml($data, $rootNodeName = 'data', &$xml=null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set ('zend.ze1_compatibility_mode', 0);
}
if (is_null($xml))
{
$xml = simplexml_load_string("<".key($data)."/>");
}
// loop through the data passed in.
foreach($data as $key => $value)
{
// if numeric key, assume array of rootNodeName elements
if (is_numeric($key))
{
$key = $rootNodeName;
}
// delete any char not allowed in XML element names
$key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value))
{
// create a new node unless this is an array of elements
$node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml;
// recrusive call - pass $key as the new rootNodeName
ArrayToXML::toXml($value, $key, $node);
}
else
{
// add single node.
$value = htmlentities($value);
$xml->addChild($key,$value);
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
// determine if a variable is an associative array
public static function isAssoc( $array ) {
return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
}
}
?>
Now with below responde I have tried problem is I get following output: <book>...</book> tags after each row.. then I tried 3 dimensional array now I get: <book><book>...</book></book> on the proper place but I have 2 of them.
This is the line where I have determine which is root on that array and that's why I get this output. But don't know how to change it : $xml = simplexml_load_string("<".key($data)."/>");
Thank you.
oci_fetch_array() will always return a single row, you need to call it until there are no more rows to fetch in order to get all of them:
while ($row = oci_fetch_array($refcur, OCI_ASSOC+OCI_RETURN_NULLS))
{
$library['book'][] = $row;
}

Categories