I want to convert this string: ["5b09e23972929.png", "5b0a7f1361a00.png"] that I get from my MySQL database into an sequential array. The reason is I want to remove single parts of the string. So $string[0] should have the value 5b09e23972929.png is this case.
$result = json_decode('["5b09e23972929.png", "5b0a7f1361a00.png"]');
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I need to store a comma delimited string (array of urls) inside mysql table.
for example:
"https://google.com,https://yahoo.com,https://something.com"
fetching this from the table inside php I'm using explode function to get an array.
$arr = explode(',', $string);
Problem is because some url can contain comma character inside itself and this can produce confusion.
In fact, url can contain any character, escaped or not - it will produce error in explode function.
Any help?
You can use urlencode()
$stupidCommaUrl = 'http://why.com/is,there/a/comma';
echo urlencode($stupidCommaUrl);
// outputs http%3A%2F%2Fwhy.com%2Fis%2Cthere%2Fa%2Fcomma
You can use urldecode() to convert it back.
Your DB design sounds flawed. As Nigel said in the comments above, look into database normalisation.
Try to use a set of characters that you will never find in a url like $&$ to separate your values, that will act as a comma, so when you retreive the values you will do this: $arr = explode('$&$', $string);
You can also build an array and use json_encode and save the string, you will get rid of the explode function, example:
$arrayOfUrls = [your array];
$urlsInJson = json_encode($arrayOfUrls);
Then store $urlsInJson in database
To retrieve your data you will use
$arrayOfUrls = json_decode($yourQueryResult['URLS'])
Hope it helps.
if I have a string like this , $index_string="[1][3]";
How to get value in below array using above string,
$value_I_want_to_get_using_above_string = $multidimensional_array[$index_string];
You can do something like this using eval
$index_string="[1][3]";
$index_string =' $value_I_want_to_get_using_above_string = $multidimensional_array'.$index_string;;
eval($index_string);
Although as we all know eval() is dangerous especially if you are getting that string from another place.
I have a string of data formatted like so:
[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]
The string doesn't have any index/numbers like a regular array would and I'm finding it difficult to extract individual values e.g. with a regular array I could use:
$string[0]["pr_a_w"]
To get the first instance of "pr_a_w" and I could use:
$string[1]["pr_a_w"]
To get the second instance etc.
Is it possible to get single values from this string based on their number?
What you have there is valid JSON (serialized array of objects), so you could use json_decode to translate the serialized data into a native PHP array:
$array = json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
$array will then allow you to do exactly what you stated you'd like to do above.
$array[0]["pr_a_w"]; // will give you 10
$array[1]["pr_a_w"]; // will give you 10
Try like this, No need to access with array index. You will get error if you access wrong index.
$json_arr= json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
foreach($json_arr as $row){
echo $row['pr_a_w']."<br>";
}
I have a column in my database that stores a string of numbers, separated by commas.
,,133,,,,444,,,,555,,,,6,
Rules:
The first number in the string is always preceded by 2 commas
There are always 4 commas between the middle numbers
The last number only has 1 comma after it
The example above is how I always want the string to look..
What happens is when some of these numbers are removed the updated string looks like this:
,,31,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,998,,,,476,,,,,
Making it look messy in the database and sometimes causing trouble with the extra commas when I try to output each number
I've been manually updating each value to follow the format I want but I'd like to make a script that runs each night and takes each of these strings and updates them with the correct format following the rules I listed above.
What can I use to format the string to follow the rules above?
You could create a php script that loads the values from the database, manipulate the rows, and store the manipulated values back to the database. I don't know what database and table structure you use, but the manipulation part is simple:
// load the string from the database into the $value variable
$numbers= preg_split("/,+/", $value); // split the string
$numbers= array_filter( $numbers); // remove empty array elements
$newvalue = implode(',,,,', $numbers); // join the array elements to a string separated by ,,,,
$newvalue = ',,' . $newvalue . ','; // add ,, at the beginning and , at the end of the new value
// store $newvalue in the database
A string is an array of characters, correct? If so, then why does count() not produce the same result as strlen() on a string?
Unless it is an object, count casts its argument to an array, and
count((array) "example")
= count(array("example"))
= 1
A string is an array of characters in C, C++ and Java. In PHP, it is not.
Remember that PHP is a very loose language, you can probobly get a character from a PHP string with the []-selector, but it still dosn't make it an array.
count() counts the number of entries in an Array.
$test = array(1,2,3);
echo count($test);
Output: 3
Why would you want to use count() on a string when strlen() can do that? Are you not sure if your input is a string or an array? Then use is_array() to check that.
How exactly a string is being handled internally by a specific programming language, must not necessarily mean you can handle it equally to therefore "related" data types. What you describe may be possible in plain C. However PHP is not C and so is not following the same characteristics.
Strings are just a series of charactes, and count only counts number of elements in an array.
using $string[$index]; its just a shortcut kinda of thing to help you find Nth character,
you could use count(explode('',$string)); which presumably is what strlen does
so lesson for today is
count == array length
strlen == string length
count gets the number of elements in an array, srtlen gets the length of a string. This is in the docs:
http://php.net/manual/en/function.strlen.php
count is more preferabaly user for array value count
strlen its count only the no of char in str.....