{"id":228920,"date":"2022-10-13T13:13:00","date_gmt":"2022-10-13T10:13:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=228920"},"modified":"2022-11-09T04:48:27","modified_gmt":"2022-11-09T01:48:27","slug":"pezzo-di-matrice","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/it\/pezzo-di-matrice\/","title":{"rendered":"Pezzo di matrice"},"content":{"rendered":"\n<p>Usando array chunk (<code>array_chunk<\/code>) possiamo facilmente dividere un array in un array multidimensionale (un array di array).<\/p>\n<p>La funzione di blocco dell&#8217;array potrebbe essere utilizzata per suddividere un array in parti pi\u00f9 piccole, forse per semplificarne l&#8217;elaborazione.<\/p>\n<h2>Esempio di base<\/h2>\n<p>Supponendo di impostare un array come nell&#8217;esempio di codice seguente (<code>$array<\/code>sulla riga 1), possiamo suddividere questo array in un nuovo array multidimensionale usando la <code>array_chunk<\/code>funzione sulla riga 9.<\/p>\n<p>L&#8217;esecuzione di questo codice risulterebbe nella matrice multidimensionale alla riga 15.<\/p>\n<pre><code>$array = [\n    'Post 0', \n    'Post 1', \n    'Post 2', \n    'Post 3', \n    'Post 4', \n    'Post 5',\n];\n$chunked_array = array_chunk( $array, 2 );\nprint_r( $chunked_array ); \/\/ Output the new array.\n\n\/**\n * Output:\n *\n * Array\n * (*   [0] =&gt; Array\n * (*          [0] =&gt; Post 0\n *          [1] =&gt; Post 1\n *) *   [1] =&gt; Array\n * (*          [0] =&gt; Post 2\n *          [1] =&gt; Post 3\n *) *   [2] =&gt; Array\n * (*           [0] =&gt; Post 4\n *           [1] =&gt; Post 5\n *) *)\n **\/\n<\/code><\/pre>\n<h2>Composizione<\/h2>\n<pre><code>$chunked_array = array_chunk( $array, $chunk, $preserve_keys );\n<\/code><\/pre>\n<p>La <code>array_chunk<\/code>funzione accetta tre parametri, <code>$array<\/code>e <code>$chunk<\/code>( <code>$preserve_keys<\/code>facoltativo, false per impostazione predefinita). Questi sono i seguenti:<\/p>\n<ol>\n<li>Il <code>$array<\/code>parametro accetta un array ed \u00e8 l&#8217;array iniziale che desideriamo trasformare.<\/li>\n<li>Accetta un numero <code>$chunk<\/code>intero ed \u00e8 il numero che usiamo per dividere <code>$array<\/code>in parti di questo valore.<\/li>\n<li>Il parametro facoltativo <code>$preserve_keys<\/code>accetta un valore booleano e manterr\u00e0 le chiavi dell&#8217;array se <code>true<\/code>.<\/li>\n<\/ol>\n<h2>Caratteristiche ed esempi specifici<\/h2>\n<h3>Conserva le chiavi<\/h3>\n<p>Esplorando il <code>$preserve_keys<\/code>parametro, il nostro esempio precedente avvia l&#8217;indice di ogni array nidificato a 0. Tuttavia, se dovessimo passare <code>true<\/code>a questo parametro, l&#8217;indice originale dell&#8217;array iniziale verrebbe mantenuto intatto:<\/p>\n<pre><code>$array = [\n    'Post 0', \n    'Post 1', \n    'Post 2', \n    'Post 3', \n    'Post 4', \n    'Post 5',\n];\n$chunked_array = array_chunk( $array, 2, true );\nprint_r( $chunked_array ); \/\/ Output the new array.\n\n\/**\n * Output:\n *\n * Array\n * (*   [0] =&gt; Array\n * (*          [0] =&gt; Post 0\n *          [1] =&gt; Post 1\n *) *   [1] =&gt; Array\n * (*          [2] =&gt; Post 2\n *          [3] =&gt; Post 3\n *) *   [2] =&gt; Array\n * (*           [4] =&gt; Post 4\n *           [5] =&gt; Post 5\n *) *)\n **\/\n<\/code><\/pre>\n<p>Nell&#8217;output le chiavi sono ora sequenziali, come sarebbero nell&#8217;array originale (0, 1, 2, 3\u2026).<\/p>\n<h3>Conserva le chiavi quando l&#8217;array originale ha chiavi con nome<\/h3>\n<p>Se il nostro array originale ha chiavi con nome, possiamo conservare le chiavi per mantenerle nel nostro array a blocchi.<\/p>\n<p>L&#8217;esempio seguente si diverte un po&#8217; con questo. Diciamo che in qualche modo abbiamo ottenuto un array con alcuni ID post e nomi di post mescolati insieme.<\/p>\n<p>Potremmo usare <code>array_chunk<\/code>con il <code>$preserve_keys<\/code>parametro impostato su <code>true<\/code>per dividerli nei singoli post.<\/p>\n<pre><code>$array = [\n    'id_0'   =&gt; '10', \n    'name_0' =&gt; 'Post 1', \n    'id_1'   =&gt; '20', \n    'name_1' =&gt; 'Post 2', \n    'id_2'   =&gt; '30', \n    'name_2' =&gt; 'Post 3',\n];\n$chunked_array = array_chunk( $array, 2, true);\nprint_r( $chunked_array ); \n<\/code><\/pre>\n<p>Ancora divertendosi con questo array, immaginiamo come potremmo scorrere il <code>$chunked_array<\/code>, e produrre solo i nomi.<\/p>\n<pre><code>foreach( $chunked_array as $key =&gt; $nested_array) {\n    echo $nested_array[ 'name_'. $key ]. PHP_EOL;\n}\n<\/code><\/pre>\n<h3>Chunking di un array di grandi dimensioni per l&#8217;elaborazione.<\/h3>\n<p>Se abbiamo un array molto grande, possiamo usarlo <code>array_chunk<\/code>per suddividerlo in blocchi pi\u00f9 piccoli che potremmo quindi archiviare separatamente. Vediamo come potremmo farlo in WordPress:<\/p>\n<pre><code>$original_array = [...]; \n$post_id        = get_the_ID();\n$chunked_array  = array_chunk( $original_array, 10, true );\n\nforeach( $chunked_array as $key =&gt; $array) {\n    update_post_meta( \n        $post_id,\n        '_wc_chunked_array_partial_'. $post_id,\n        $array\n    );\n}\n<\/code><\/pre>\n<h2>Ulteriori letture<\/h2>\n<p>Per ulteriori letture puoi consultare il <a href=\"https:\/\/www.php.net\/manual\/en\/function.array-chunk.php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">manuale PHP<\/a>.<\/p>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">Fonte di registrazione:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/wholesomecode.ltd\" class=\"external external_icon\">wholesomecode.ltd<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usando il blocco dell&#8217;array (array_chunk) possiamo facilmente dividere un array in un array multidimensionale (un array di array). La funzione del blocco dell&#8217;array potrebbe essere utilizzata per suddividere un array in piccoli &#8230;<\/p>\n","protected":false},"author":1,"featured_media":224072,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[918,896,751,804,720,844],"tags":[1168],"class_list":["post-228920","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-altro","category-codice","category-open-source-projektmanagement-3","category-php-6","category-sviluppatore","category-tutorial","tag-affiai-it"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts\/228920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/comments?post=228920"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts\/228920\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/media\/224072"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/media?parent=228920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/categories?post=228920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/tags?post=228920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}