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']);
});
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 is more of like requiring an architecture and correct approach, then actual code.
Stating it so that people shouldn't flag it as "We cannot code for you..." etc.
I get a response from the API in JSON which I converted to an associated array using json_decode as:
Array
(
[statusCode] => 200
[data] => Array
(
[objects] => Array
(
[0] => deals
[1] => contacts
[2] => accounts
)
[deals] => Array
(
[0] => dealName
[1] => ApprovedBy
[2] => ApprovedDate
[3] => CloseDate
)
[contacts] => Array
(
[0] => contectName
[1] => email
[2] => firstName
[3] => lastName
)
[accounts] => Array
(
[0] => accountName
[1] => creationDate
[2] => ApprovedDate
[3] => accountNumber
)
)
)
So the structure is as:
In response's data array against key "objects", I have an array of objects i.e deals, contacts, accounts.
And for each element in the objects array as the key, an array of fields exist.
I have to show the objects in a drop down say "Objects" and upon selection of a value say "deals" I want to show elements of deals array in a second drop-down "Fields".
[DONE] Also for the options in the dropdowns, values should be the same as actual elements and not numeric values.
json_decode(string, true) gives associated array but with numeric keys. So keys should be same as their associated values like:
[deals] => deals
[contacts] => contacts
[accounts] => accounts
In the view, I have a table with a number of rows, each row having its own pair of these drop-downs.
If each row has its own drop-down, then I also have to take care avoiding updating second drop-down in wrong places. As each drop-down is an array of objects[] & object_fields[], should I use the index of this array in DOM, get next sibling in DOM, or to assign a unique id to each row for the population of correct drop-down at correct row?
How should I achieve these, what should be the correct way?
I cannot use ajax/getJson() as to reduce the number of requests to API and I do have all the required data available. Just need to use it but needed meticulous approach regarding architecture.
Should I pass on Json object to view and process it in javascript in view? Will this be a better approach?
Thanks
UPDATE:
Point 2 of the question is DONE.
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);
I'm having trouble handling a nested array I get as result from an API. Print_r($result, true); returns an array looking like this (only much longer):
Array
(
[success] => 1
[return] => Array
(
[sellorders] => Array
(
[0] => Array
(
[sellprice] => 0.00000059
[quantity] => 1076.00000000
[total] => 0.00063484
)
[1] => Array
(
[sellprice] => 0.00000060
[quantity] => 927.41519000
[total] => 0.00055645
)
)
[buyorders] => Array
(
[0] => Array
(
[buyprice] => 0.00000058
[quantity] => 6535.77328102
[total] => 0.00379075
)
[1] => Array
(
[buyprice] => 0.00000057
[quantity] => 118539.39620414
[total] => 0.06756746
)
)
)
)
I need to grab the 3 values (sellprice/buyprice, quantity, total) from the first index of both arrays (sellorders and buyorders) and store them in variables ($sellprice, $sellquantity, $selltotal).
The full example php script I'm using can be found on the bottom of this page. Could anyone help me figure this out?
In php, arrays can more or less have infinite dimensions. You can go deeper within an array's dimensions by adding another set of square brackets. For example,
$array['deep']['deeper']['deepest'][0];
Assuming the indexes in the sellorders and buyorders are the same in your array, you could do
$sellprice = $result['return']['sellorders'][0]['sellprice'];
$sellquantity = $result['return']['sellorders'][0]['quantity'];
$selltotal = $result['return']['sellorders'][0]['total'];
The value should look something like this:
$sellprice = $array['return']['sellorders'][0]['sellprice']
You might want to think about how you iterate over these nested arrays in order to pick out all the values. Furthermore, if you have control over the output I might be better to use a different data structure to enable easier processing.
You can access the values of the nested arrays by adding another pair of square brackets with the appropriate index at the end:
$array['outer']['inner'];
It's up to you to transfer this knowledge to your specific array.
If you want to go thru all of those arrays... try this:
for($i=0; $i<count($array['return']['sellorders']); $i++) {
$this_array = $array['return']['sellorders'][$i];
var_dump($this_array); // it includes sellprice, quantity and total for each entity now.
}
use the same method as above for buyorders as well.
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.