I want to use jQuery UI Autocomplet to make a form field autocomplete grabbing the values from a database:
http://jqueryui.com/demos/autocomplete/#remote
I have copied the code accross, but the example (birds) is an associative array.
$items = array(
"Great Bittern"=>"Botaurus stellaris",
"Little Grebe"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Little Bittern"=>"Ixobrychus minutus");
etc etc
I want to query my database taking two fields, id and author.
However the query returns an multidimensional array, where each returned row from the database is an array.
e.g. ( [0] => Array ( [ID] => 1 [Author] => Higgins ) ) ( [0] => Array ( [ID] => 2 [Author] => Darl) )( [0] => Array ( [ID] => 3 [Author] => Lewis) )
How can I return the query so it is in the format:
"1=>Higgins,
2=>Darl,
3=>etc etc,"
so that i can use the script?
Given that your database array is $dbresult, you can do it like this:
foreach($dbresult as &$arr) {
$completearray[$arr['ID']] = $arr['Author'];
}
var_dump($completearray);
above will output the following array:
1=>Higgins
2=>Darl
3=>Lewis
UPDATE: I've updated the code above so the resulting array is indexed by the ID field.
Related
I have a multidimensional array stored in $accounts variable :
Array
(
[0] => Array
(
[id] => 1
[account] => ACR016
[desc] => Salary
)
[1] => Array
(
[id] => 2
[account] => ACR017
[desc] => Bonuses
)
)
I'd like to find/get the "desc" value by using its "account" inside $accounts,
In SQL i will go like this :
SELECT desc FROM table WHERE account = 'ACR016';
How to do that with PHP without do the SQL query (because the array already stored in variable) and without looping ?
Use https://www.php.net/manual/en/function.array-column.php for this.
As this is a builtin function it should perform better than doing your own loop.
Example:
$accountDescriptions = array_column($accounts,'desc','account');
Will result in:
Array
(
[ACR016] => Salary
[ACR017] => Bonuses
)
Alternatively you can use:
Example:
$accountDescriptions = array_column($accounts,null,'account');
Then you will get all data keyed with account instead of numeric index.
This question already has answers here:
get array of rows with mysqli result
(2 answers)
Closed 4 years ago.
I'm trying to query database A and store results as an array, which I will then insert into database B. I can move simple data directly, but because of the complexity of this particular query (30+ left joins), I have to store it as an array and insert it in a separate query. The examples below contain just two fields, to keep things simple and focus on the core issue.
I have working code to insert an array stored as follows:
$data = array(
array( // record 1, or row 1
"1",
"Value for field 1.2"
),
array( // record 2, or row 2
"2",
"Value for field 2.2"
),
array( // record 3, or row 3
"3",
"Value for field 3.2"
),
// etc...
);
Unfortunately, I can't get my first query to store the data like this. I have used PHP for years, but I never messed with arrays before. I'm not sure what I'm doing wrong. Using "print_r($data), this is what the results look like for how I need the array to be (This is what the $data variable looks like with print_r() ):
Array ( [0] => Array ( [0] => 1 [1] => Value for field 1.2 ) [1] => Array ( [0] => 2 [1] => Value for field 2.2 ) [2] => Array ( [0] => 3 [1] => Value for field 3.2 ) )
My query to create the array doesn't match this pattern. A "print_r($new_array)" command yields this:
Array ( [0] => Array ( [id] => 37 [post_title] => test1 ) [1] => Array ( [id] => 38 [post_title] => test2 ) [2] => Array ( [id] => 35 [post_title] => test3 ) [3] => Array ( [id] => 42 [post_title] => test4 ) [4] => Array ( [id] => 44 [post_title] => test5 ) [5] => Array ( [id] => 46 [post_title] => test6 ) )
This is my MySQL query to put the data into an array:
$test_sql = "SELECT id, post_title FROM wp_posts where post_type LIKE 'test'";
$resultTest = mysqli_query($con, $test_sql);
//$new_array[] = $row;
while ($row = mysqli_fetch_assoc($resultTest)) {
$rows[] = $row;
}
If I understand what is going on, the array I'm creating is making key value pairs for a multidimensional array, but the format I need is not key value, just an array of values. Since I already have the insert query working, I would prefer to use a select query that stores the data in an array without the key value pairs, if that is possible, but I am open to all suggestions.
Thank you in advance for your kindness and help!
All you need to do is change fetch function to fetch_row:
while ($row = mysqli_fetch_row($resultTest)) {
$rows[] = $row;
}
Or to fetch_array with MYSQLI_NUM as second argument:
while ($row = mysqli_fetch_array($resultTest, MYSQLI_NUM)) {
$rows[] = $row;
}
I'm creating an ajax function to retrieve product details from mongoDb.
The array structure is:
Array
(
[PRD20160830063407] => Array
(
[_id] => PRD20160830063407
[tpl] => Array
(
[ProductName] => Adidas Agro-Yellow,Gray
)
)
[PRD20160831104319] => Array
(
[_id] => PRD20160831104319
[tpl] => Array
(
[ProductName] => sera xv001s
)
)
)
Need to pass _id, ProductName in a Select Box value and data part, I do not know how to retrieve the multidimensional array..
Appreciate any help. Thanks!
If I understand the question correctly (I'm not aware of the notation you used to show us the array), you're actually having problems with accessing the associative array because the data is not in array[0], array[1], etc. but in array[PRD20160830063407], etc; which means that you cannot iterate using the common for-loop.
You need Object.keys(array) to iterate over them.
var IDs = [];
Object.keys(array).forEach(key => {
IDs.push(array[key]['_id']);
});
When user submits form the data is stored in array & serialized & stored in the database, when user again submit another form then that data is stored in array & previous serialized array is fetched from database & is unserialized.
Now i want both these array to be multi dimensional, here is what I've tried
$post = array();
$post[] = $co_name = test_input($_POST['co_name1']);
this array is fetched from database
$db = unserialize($db);
$db[] = $post;
print_r($db);
After Printing this is what i get
Array
(
[0] => company_name
[1] => country
[2] => city
[3] => state
[4] => pincode
[5] => 2008
[6] => 01
[7] => 2008
[8] => Array
(
[0] => company_name
[1] => country
[2] => city
[3] => state
[4] => pincode
[5] => 2008
[6] => 01
[7] => 2008
)
)
Now my problem is second array is assigned to 8, how to perfectly create multi dimension array
My desired output is my array should like this
array(
0=>array(
0=>company_name
1=>country
),
1=>array(
0=>company_name
1=>location
)
)
The following will give you a numerically indexed array with your POST data as one value and DB data as another value of a new array. If you var_dump / print_r() this, the output will look similar to your desired output:
$newArray = array($post, $db);
However; Your desired output shows a reduced number of keys for each result:
company_name
country
...(or did you only write these to make it easier to read?)
If you do want just those two keys, consider using PHP's array_filter function which takes your combined array (above: $newArray) and a callback function as arguments. This allows you to manipulate any of the keys and values of the input array, to make the returned array look exactly as you like.
Just pass the info with the correct array number example
$post = array();
$post[0] = $co_name = test_input($_POST['co_name1']);
$post[1] = $co_name = test_input($_POST['co_name1']);
You won't get an answer without more clarification. This problem is likely due to faulty serialization.
How are you getting your data out of the db?
What does the data in the db look like?
I am trying to save a Form Data in Mysql. But nested array is causing problem.
Array ( [name] => Custom Project
[number] => 08883
[key] => Array ( [0] => Server Cost
[1] => Domain Cost
[2] => Design Charges,
)
[value] => Array ( [0] => 098
[1] => 765
[2] => 787
)
)
I am using $this->db->insert('invoice',$array) and it is inserting data fine without the nested array.
So is there a way I can separate this nested array from above array and then save this nested array into another table having only two columns key and value
You could just use array_combine to create the key pair value:
function save_value($array)
{
$key_value = array_combine($array['key'], $array['value']);
$this->db->insert('table_name', $key_value);
}
I used the very simple approach, I pushed one array to another because the result was to be processed in Codeigniter insert method.
//Item description
$description=$_POST['key'];
//item amount
$amount=$_POST['value'];
$big_array=array();
for($i=0; $i<sizeof($description); $i++){
$small_array=array('description'=>$description[$i],'amount'=>$amount[$i]);
array_push($big_array,$small_array);
}
//adding data into model
$this->my_Model->InsertData($big_array);