B
B
BonBon Slick2016-07-19 15:40:09
PHP
BonBon Slick, 2016-07-19 15:40:09

Array_map or json_encode?

What is the difference in recording? Where, when and what to apply? Tell me please.

$my_array = array_map( 'strval', $_POST['pic_texts'] );  
    $my_array= json_encode( $_POST['pic_texts'] ));

P.S. $_POST['pic_texts'] = array of input values ​​that go into the menu on the page. In their place can be any array, up to $a = ['1', '2' , '3']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2016-07-19
@BonBonSlick

array_map is used to traverse arrays. The scope is similar to foreach . Theoretically, you can write your own serialization mechanism with it, but in practice this is not necessary.
json_encode is used to serialize any objects (including multidimensional ones) into a string. Just like serialize .
If you try to apply the strval function to an array, you will get "Array to string conversion in ..." warnings, and the string "Array" as the value, regardless of the contents of the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question