Transform Array and String

Asked

Viewed 400 times

2

How to transform an array similar to this and Strings to write each line in the Mysql database.

I will only need the data of each array ( date, site and Status )

print_r($arrayOut);

Array
(
    [1] => Array
        (
            [date] => 15/12/2016 17:11
            [site] => AC SAO MIGUEL DOS CAMPOS - Sao Miguel Dos Campos/AL
            [status] => Endereço insuficiente para entrega
            [isOrigin] => 1
        )

    [2] => Array
        (
            [date] => 15/12/2016 17:11
            [site] => Objeto em devolução ao remetente  
            [status] => Endereço insuficiente para entrega
            [isOrigin] => 
        )

    [3] => Array
        (
            [date] => 15/12/2016 09:53
            [site] => Sao Miguel Dos Campos/AL
            [status] => Saiu para entrega ao destinatário
            [isOrigin] => 1
        )

    [4] => Array
        (
            [date] => 09/12/2016 17:08
            [site] => AGF JARDIM MARILIA - Sao Paulo/SP
            [status] => Postado
            [isOrigin] => 1
        )

)
  • 1

    Isn’t it easier to use the key? ex $array[0]['date'] or within a foreach $array['date']

  • You’re using PDO, how you’re connecting??

1 answer

3


You can use the json_encode to turn into string.

$dataString = json_encode($arrayOut);

In case you want to take some fields, before using the json_encode make a loop and take out the fields you don’t want:

foreach($arrayOut as $arr) {

    unset($arr['isOrigin']);

}

If you want to save one at a time in the database, just take advantage of the foreach:

foreach($arrayOut as $arr) {

    unset($arr['isOrigin']);
    $dataString = json_encode($arr);
    // inserir $dataString na coluna correspondente.

}

json_encode

  • {"1":{"date":"15/12/2016 17:11","site":"AC SAO MIGUEL DOS CAMPOS - Sao Miguel Dos Campos/AL","status":"Address u00e7o insufficient for delivery | ","isOrigin":true},"2":{"date":"15/12/2016 17:11","site":"Object in devolu u00e7 u00e3o to sender ","status":"Insufficient addressu00e7o for delivery | ","isOrigin":false},"3":{"date":"12/15/2016 09:53","site":"Sao Miguel Dos Campos/AL","status":"Went out for delivery to destinat u00e1rio | ","isOrigin":true},"4":{"date":"12/09/2016 17:08","site":"AGF JARDIM MARILIA - Sao Paulo/SP","status":"Posted | ","isOrigin":true}}

  • So he left with Json Ncode, as I separate ?

  • yes, that’s a string, if you want to take a look at the documentation: http://php.net/manual/en/function.json-encode.php

  • You can save so in the bank, and then to use just bring this value and use the json_decode, that this will be transformed into array again!

  • I understood, I was already wanting to record at once straight on each bank line, I will try to work with explode and try to separate, but this is already very helpful,

  • I edited the answer @Marcospaulo, see if it helps you!

  • 1

    Perfect, now and only work here, thank you!!!

  • Just note that after saving so in the bank you will not be able to filter by field @Marcospaulo

  • @bfavaretto - I am working with the strings before saving, so only what I need will be saved!

  • @Marcospaulo what he is saying is that you will not be able to assemble querys based on these data, understood?

  • @Juniornunes, I get it, the way you gave the tip there is perfect for me, I’m already able to filter and this recording in the bank only what I need!!

  • @Juniornunes - a doubt, can I get only the last status of this array?

Show 7 more comments

Browser other questions tagged

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