Reference an array from a string - php

I've been banging my head against a wall with this one. What I have is a database table which holds references such as {name} etc which map to an array which has been built such as $customer['name']. What I need to do is take the value from the database and get the value from the array.
The value in the database is the same as what would be needed to reference the array (so $customer['name'] would be in the database field).
Can anyone tell me how I would get the array value from this?
Thanks in advance.

I managed to find an answer from this thread - get value from array using string php. What I ended up doing was separating the array name and key with a full stop so $customer['name'] is now stored as customer.name and exploded it to get the array value.

Related

How to structure an array

I have to figure out how to present some data that is capture using gravity forms for wordpress.
There is a field for name, and attributes. Attributes contains a comma delineated list of values.
On a results page I need to display the names, followed by the top ten values. Where I'm stuck is is how to configure an array that gives me: distinct names, and within each distinct name is an array containing all values from the comma list(s).
I can't wrap my head around this to save my life. Given how GF stores enteries I think makes this especially difficult. Any guidance is much appreciated.
<?php
// A two-dimensional array:
$names = array
(
"sam"=>array(1,2,3,4,5,6,7,8,9,10),
"joe"=>array(1,2,3,4,5,6,7,8,9,10),
"bob"=>array(1,2,3,4,5,6,7,8,9,10)
);
?>
Like that?

How do obfuscate JSON response when dealing with a DB query?

[
{
"businesscards_id":"12",
"X_SIZE":"1.75x3",
"X_PAPER":"14ptGlossCoatedCoverwithUV(C2S)",
"X_COLOR":"1002",
"X_QTY":"250",
"O_RC":"NO",
"F_PRICE":"12490",
"UPS_GROUND":"12000",
"UPS_TWODAY":"24000",
"UPS_OVERNIGHT":"36000"
}
]
This JSON encode response is seen in console of Chrome. This array is being returned from a DB query. It is showing my table column names. For security reasons I do not want to show my table column names. How can this JSON object be obfuscated or hashed and/or encoded or dynamically re-written to keep my table col names private?
Don't do anything to your JSON.
If you don't want your column names to be visible, just dont use your column names. Create a new array using new keys to send with JSON and then change that array back into one containing your column names afterwards.
But it really shouldn't be a problem people seeing them. Nobody has access to your database so letting people see column names isn't an issue.
It really depends on how you wish to use the record once it has been received. One strategy might be to return an array of the values only, discarding the keys. Then in your code, use your private knowledge of which array value you need when you process the record. Something like:
var result=[];
Object.keys(record).forEach(function(key){result.push(record[key]);});
And then in your code, use array indices to access the values.
SQL statement:
SELECT `col_name` AS 'something_else'
But also, as everyone else said, don't do this for security. It is pointless.

How to pass an array element to another array to store in database in php

Hi i am facing one problem in passing the array elements to another array...
$order=$this->ro_model->get_ro_details($order_id);
By using above function i will get query results into array name called $order.
Now i had form when it submits i will get some data from that form that i am saving in another name called $order_details.
$order_data=array(
'amount'=>$this->input->post('amount'
);
Now i wanna save both the arrays data in one table in database. For that what i thought was i can pass first array data to second array data and i will save all the data in one array. Then later i will send that array to a function which will insert the data into database.
$this->ro_model->add_amount($order_data);
My problem how to pass that first array data to second array...........
Note: All these are i am doing in codeigniter framework.
Use array_merge() function. Here you go: http://php.net/manual/en/function.array-merge.php
you can do it in 2 ways
array_push Push one or more elements onto the end of array http://in3.php.net/manual/en/function.array-push.php
array_merge Merge one or more arrays http://in3.php.net/manual/en/function.array-merge.php

How do i store a field from a MySQL database as a string in PHP?

One of my fields in my data base is an array which has been converted to a string using the implode() function.
How do I retrieve the contents of this field (LESSONS) from the database and store it to a string when a user entered value is equal to the the value of the field NAME?
Thanks in advance for any help you may be able to provide.
Here you go:
$r = mysql_query('SELECT LESSONS FROM TABLE WHERE NAME=\'user_string\'');
$rows = mysql_fetch_assoc($r);
echo $rows['LESSONS'];
I don't know if I understood your question but... Take a look about Stored Procedures
If you used the implode function to convert your array into a string, then this data has lost any information about the array keys.
As far as you want to convert the data back, use the explode function:
$array = explode(',', $columnData);
But You can therefore not search for array keys within the database.
Next to that, the MySQL database (I assume you're using MySQL) can not search for array keys anyway.
You need to store the data in some other way into the mysql to search for it in an SQL later on.
For example, you can create a table that stores key/value combinations with a grouping index.
However MySQL has some string functions that can help you searching within the (now) string data in the MySQL database.
When searching for a value, before the comparison add a comma at the beginning and one at the end of the string. There is a MySQL string function that can concatenate strings. Then search within that expression for your value with a comma added in front and back as well.
Then you can lookop a single array element within the mysql database. MySQL String Functions.
This is a quick solution only, this won't work on large databases performant. However it might solve your problem w/o changing your database structure.

PHP Script to transfer file text into database

I have a set of textfiles containing personal data. I want to use a script to run through them and write them to a mysql database.
What I have done so far is to read the text file into a string, explode the string at " " and then run through the array to find the required information. There are a couple of problems with that though, so maybe someone knows a better solution alltogether.
The base text file looks like this:
Name: Marius
Address 1: Street
Address 2: Town
Address 3: Country
etc
The problem in this is that I have no way of knowing how long the 'value' of i.e. Street is. Could be just the 'Somestreet' or 'Some Street' or 'Some Street 1337'. This goes for pretty much every field.
Is there maybe a method to turn that text into an array, where for every item the string ending with ":" is used as the key and every following item without ending ":" is considered the value?
As Adam hints in the comments: If there is no tab, cr/lf or similar delimiter, there is not enough information in your string to parse the data by.
If there is only a limited set of keys, you can provide this set as additional information.
Since you want to insert the data into a mysql table, I assume that the keys are limited and unique.
With an array of these key strings, your parser algorithm would work this way:
1 explode the string at ":"
2 loop through the resulting array, where you should find a structure like this:
[some or no value][space or other delimiter][known key]
2.1 remember the key from the previous loop (empty as default)
2.2 since the keys should be unique, loop through the array of known keys
2.2.1 if a key matches the current string structure from the right, you can
strip this key off the right side of the string, trim the string and you
have the current value
2.2.2 if you found a value, put both into the result array:
$arr[previous key] = [current value]
This should do the trick.

Categories