How to read a string within an array?

Asked

Viewed 234 times

0

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

"20171012 20171102 20171115 20171120 20171225" 

all into an array of the type, like this one below:

Data[1]="20171012"

Data[2]="20171102"

Data[3]="20171115"

Data[4]="20171120"

Data[5]="20171225"

How could I do that? If you can help me, I really appreciate it.

  • Do you want to extract the dice from the string? It would be that that you need?

  • The string in the database contains a list of holidays in the example format "20171012 20171102 20171115 20171120 20171225". Hence I wanted to extract each date from this and put in a variable array.

  • I put an example in the link above, uses the explode(' ', $str); and returns an array. See if it solves the problem.

  • Sorry, true, now that I’ve seen your Link ... would be just that, only in Ubuntu bash script.

  • I’m sorry, I answered in the wrong tag, PHP rsrs

  • Marco I edited the answer, see there.

  • opa !! I saw your answer below, and it was perfect !!! it was that !! thank you very much!

Show 2 more comments

2 answers

0


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

#!/bin/bash

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

echo "${array[4]}"

Exit:

20171225

  • That’s right!! I tested it here and it worked well !!!

  • Thank you very much!!!

0

Step 1 - Create a PHP page that reads the string (Raw form) in the database, save to a variable (Ex.: $datas) and "return" that string. Note: The "return" I’m talking about is NOT done with return $datas, but rather with echo $datas, because thus the "return" of the page can be received by the person who made the request (in this case, AJAX).

Step 2 - Create a Javascript function (Preferably using Jquery) that requests 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 response that PHP will return with echo $datas).

Step 3 - In the callback function you created in step 2, finally you will have a string (Ex.: datas) with the dates in the raw format. Here you will use the function split (Which is applicable to strings) to break the string and save the result to an array. The function split separates the string into several pieces, so that this separation is controlled by the parameter you pass (Learn more about the 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 don’t know your level of knowledge, and the things I put up, I don’t know what you know. Therefore, I may have been very wordy and I may have written unnecessary things (which would be good, because it would be a sign that you already know) or I may have been very superficial and not explained each thing very well. Anyway, if the second hypothesis is true, I advise you to make a reply listing all the points you did not understand and/or do not know how to do. It’ll make it easier for the guys to help you.

Blood disease

Helpnext

  • Just so you guys don’t think I’m crazy, the initial category of the question was WEB (PHP, Jquery, Javascript, etç). Anyway, stay there for those who want to see.

  • That’s nothing, Alan. Thanks for the tips.. I’m new here, and honestly.. I don’t even remember putting the "Web" tag.. sure I had put Mysql, because the data I will extract from it.

  • Anyway, in a moment laaaaaaa in front, I should use PHP in the cake ... so I’ll save this tip of yours :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.