How to read a string inside an array?

0

I would like to read a field of type varchar in a MySql database, which contains several dates in the following format:

"20171012 20171102 20171115 20171120 20171225" 

all within an array of type, like this one below:

Data[1]="20171012"

Data[2]="20171102"

Data[3]="20171115"

Data[4]="20171120"

Data[5]="20171225"

How could you do this? If you can help me, thank you very much.

    
asked by anonymous 16.10.2017 / 23:45

2 answers

0

Based on in this answer I have adapted a script for you, see:

#!/bin/bash

IFS=' ' read -r -a array <<< "20171012 20171102 20171115 20171120 20171225"

echo "${array[4]}"

Output:

  

20171225

    
17.10.2017 / 00:10
0
Step 1 - Create a PHP page that reads the string in the database, saves it to a variable (eg, $datas ) and returns that string. Note: The "return" I am talking about is NOT done with return $datas , but with echo $datas , because this way the "return" of the page can be received by the requester (In this case AJAX).

Step 2 - Create a Javascript function (Preferably using Jquery) that makes a request to the PHP page you created. When you mount the AJAX request, you will have to pass a callback function that will be responsible for handling the request response (The answer PHP returns with echo $datas ).

Step 3 - In the callback function you created in step 2, you will eventually have a string (eg datas ) with dates in raw format. Then you will use the split function (which is applicable to strings) to break the string and save the result to a vector. The split function separates the string into several pieces, so this separation is controlled by the parameter you pass ( Learn more about to split here ).

Ex. 1:

var datas = "O rato roeu a roupa"
var vetor = datas.split(" "); // Separa o conteúdo de ***datas*** usando ***espaço***
// vetor = {"O", "rato", "roeu", "a", "roupa"}

Ex. 2:

var datas = "O rato roeu a roupa"
var vetor = datas.split("a"); // Separa o conteúdo de ***datas*** usando ***a***
// vetor = {"O r", "to roeu ", " roupa"}

I do not know your level of knowledge and, of the things I put up, I do not know what you know. So I could have been very wordy and have written unnecessary things (which would be good, because that would be a sign that you already know) or I may have been very superficial and did not explain everything very well. Anyway, if the second hypothesis is true, I advise you to make a rejoinder listing all the points you did not understand and / or do not know how to do. This will make it easier for the people to help you.

DoeSangue

HelpNext

    
17.10.2017 / 00:27