{"id":576,"date":"2015-08-10T10:00:00","date_gmt":"2015-08-10T14:00:00","guid":{"rendered":"https:\/\/wpscholar.com\/?p=576"},"modified":"2015-08-10T16:50:43","modified_gmt":"2015-08-10T20:50:43","slug":"reindex-array-php","status":"publish","type":"post","link":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/","title":{"rendered":"Reindex an Array in PHP"},"content":{"rendered":"<p>Occasionally, you may need to arbitrarily reindex an array of data in PHP.  WordPress doesn&#8217;t really provide a function for doing this, so I put together this code snippet:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/wpscholar\/8939717.js\"><\/script><\/p>\n<p>This utility function can be useful for any PHP-based project. It comes in handy when you want to take an indexed array and convert it into an associative array.<\/p>\n<p>For example, taking a collection of posts and indexing them by ID becomes super simple:<\/p>\n<pre><code class=\"php\">$posts = get_posts();\n$posts_by_id = index_by( 'ID', $posts );\n<\/code><\/pre>\n<p>This function will work equally well with an array of objects or an array of arrays.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Occasionally, you may need to arbitrarily reindex an array of data in PHP.  WordPress doesn&#8217;t really provide a function for doing this, so I put together this code snippet.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"#webdev #tipoftheday Reindex an Array in #PHP","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[43],"tags":[],"class_list":["post-576","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Reindex an Array in PHP - WP Scholar<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wpscholar.com\/blog\/reindex-array-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reindex an Array in PHP - WP Scholar\" \/>\n<meta property=\"og:description\" content=\"Occasionally, you may need to arbitrarily reindex an array of data in PHP. WordPress doesn&#039;t really provide a function for doing this, so I put together this code snippet.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpscholar.com\/blog\/reindex-array-php\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Scholar\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-10T14:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-08-10T20:50:43+00:00\" \/>\n<meta name=\"author\" content=\"Micah\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@wpscholar\" \/>\n<meta name=\"twitter:site\" content=\"@wpscholar\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Micah\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/\"},\"author\":{\"name\":\"Micah\",\"@id\":\"https:\\\/\\\/wpscholar.com\\\/#\\\/schema\\\/person\\\/200df91380bc4a7ba50477689b8ae03d\"},\"headline\":\"Reindex an Array in PHP\",\"datePublished\":\"2015-08-10T14:00:00+00:00\",\"dateModified\":\"2015-08-10T20:50:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/\"},\"wordCount\":95,\"commentCount\":0,\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/\",\"url\":\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/\",\"name\":\"Reindex an Array in PHP - WP Scholar\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpscholar.com\\\/#website\"},\"datePublished\":\"2015-08-10T14:00:00+00:00\",\"dateModified\":\"2015-08-10T20:50:43+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/wpscholar.com\\\/#\\\/schema\\\/person\\\/200df91380bc4a7ba50477689b8ae03d\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wpscholar.com\\\/blog\\\/reindex-array-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/wpscholar.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Reindex an Array in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wpscholar.com\\\/#website\",\"url\":\"https:\\\/\\\/wpscholar.com\\\/\",\"name\":\"WP Scholar\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wpscholar.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/wpscholar.com\\\/#\\\/schema\\\/person\\\/200df91380bc4a7ba50477689b8ae03d\",\"name\":\"Micah\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?s=96&r=g\",\"caption\":\"Micah\"},\"sameAs\":[\"https:\\\/\\\/wpscholar.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reindex an Array in PHP - WP Scholar","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/","og_locale":"en_US","og_type":"article","og_title":"Reindex an Array in PHP - WP Scholar","og_description":"Occasionally, you may need to arbitrarily reindex an array of data in PHP. WordPress doesn't really provide a function for doing this, so I put together this code snippet.","og_url":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/","og_site_name":"WP Scholar","article_published_time":"2015-08-10T14:00:00+00:00","article_modified_time":"2015-08-10T20:50:43+00:00","author":"Micah","twitter_card":"summary_large_image","twitter_creator":"@wpscholar","twitter_site":"@wpscholar","twitter_misc":{"Written by":"Micah"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/#article","isPartOf":{"@id":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/"},"author":{"name":"Micah","@id":"https:\/\/wpscholar.com\/#\/schema\/person\/200df91380bc4a7ba50477689b8ae03d"},"headline":"Reindex an Array in PHP","datePublished":"2015-08-10T14:00:00+00:00","dateModified":"2015-08-10T20:50:43+00:00","mainEntityOfPage":{"@id":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/"},"wordCount":95,"commentCount":0,"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wpscholar.com\/blog\/reindex-array-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/","url":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/","name":"Reindex an Array in PHP - WP Scholar","isPartOf":{"@id":"https:\/\/wpscholar.com\/#website"},"datePublished":"2015-08-10T14:00:00+00:00","dateModified":"2015-08-10T20:50:43+00:00","author":{"@id":"https:\/\/wpscholar.com\/#\/schema\/person\/200df91380bc4a7ba50477689b8ae03d"},"breadcrumb":{"@id":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpscholar.com\/blog\/reindex-array-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpscholar.com\/blog\/reindex-array-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpscholar.com\/"},{"@type":"ListItem","position":2,"name":"Reindex an Array in PHP"}]},{"@type":"WebSite","@id":"https:\/\/wpscholar.com\/#website","url":"https:\/\/wpscholar.com\/","name":"WP Scholar","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpscholar.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpscholar.com\/#\/schema\/person\/200df91380bc4a7ba50477689b8ae03d","name":"Micah","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?s=96&r=g","caption":"Micah"},"sameAs":["https:\/\/wpscholar.com"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6tTMc-9i","jetpack-related-posts":[{"id":580,"url":"https:\/\/wpscholar.com\/blog\/check-multiple-array-keys-exist-php\/","url_meta":{"origin":576,"position":0},"title":"Easily Check if Multiple Array Keys Exist in PHP","author":"Micah","date":"August 14, 2015","format":false,"excerpt":"A simple utility function that checks to see if an array contains multiple keys.","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/wpscholar.com\/blog\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":579,"url":"https:\/\/wpscholar.com\/blog\/filter-multidimensional-array-php\/","url_meta":{"origin":576,"position":1},"title":"Filtering Multi-Dimensional Arrays in PHP","author":"Micah","date":"August 13, 2015","format":false,"excerpt":"A simple utility function that enables recursive filtering of multi-dimensional arrays.","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/wpscholar.com\/blog\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":980,"url":"https:\/\/wpscholar.com\/blog\/creating-better-wordpress-loop\/","url_meta":{"origin":576,"position":2},"title":"Creating a Better WordPress Loop","author":"Micah","date":"March 15, 2018","format":false,"excerpt":"The WordPress loop is ubiquitous. It is one of the first things you learn as a WordPress developer and it is something you must know the intricacies of in order to avoid causing problems. I've dreamt of a day when the WordPress loop and all its various implementations are no\u2026","rel":"","context":"In &quot;WordPress Development&quot;","block_context":{"text":"WordPress Development","link":"https:\/\/wpscholar.com\/blog\/category\/wordpress-development\/"},"img":{"alt_text":"A Better WordPress Loop","src":"https:\/\/i0.wp.com\/wpscholar.com\/wp-content\/uploads\/2018\/03\/Loop_the_Loop_Luna_Park_Coney_Island-1.jpg?fit=1024%2C784&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/wpscholar.com\/wp-content\/uploads\/2018\/03\/Loop_the_Loop_Luna_Park_Coney_Island-1.jpg?fit=1024%2C784&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/wpscholar.com\/wp-content\/uploads\/2018\/03\/Loop_the_Loop_Luna_Park_Coney_Island-1.jpg?fit=1024%2C784&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/wpscholar.com\/wp-content\/uploads\/2018\/03\/Loop_the_Loop_Luna_Park_Coney_Island-1.jpg?fit=1024%2C784&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":578,"url":"https:\/\/wpscholar.com\/blog\/php-array-object-access-via-dot-notation\/","url_meta":{"origin":576,"position":3},"title":"Heterogeneous Multi-dimensional Data Access in PHP","author":"Micah","date":"August 12, 2015","format":false,"excerpt":"Easily fetch deeply nested data from a heterogenous multi-dimensional collection in PHP using dot notation... or whatever that means.","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/wpscholar.com\/blog\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24,"url":"https:\/\/wpscholar.com\/blog\/ie-conditional-stylesheets-wordpress\/","url_meta":{"origin":576,"position":4},"title":"Loading IE Conditional Stylesheets in WordPress","author":"Micah","date":"August 8, 2014","format":false,"excerpt":"Most web developers are familiar with the standard IE conditional comments that allow you to load a stylesheet only in Internet Explorer. \u00a0For example: https:\/\/gist.github.com\/4970393 Many new WordPress developers \/ themers tend to just hardcode these conditional comments directly into their theme's header.php file. \u00a0This approach might work well for\u2026","rel":"","context":"In &quot;WordPress Development&quot;","block_context":{"text":"WordPress Development","link":"https:\/\/wpscholar.com\/blog\/category\/wordpress-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":43,"url":"https:\/\/wpscholar.com\/blog\/wordpress-user-login-redirect\/","url_meta":{"origin":576,"position":5},"title":"WordPress User Login Redirect","author":"Micah","date":"February 12, 2013","format":false,"excerpt":"WordPress sites that offer personalized or private content typically want to keep their users on the front end of the site. \u00a0However, WordPress automatically takes users to the back end dashboard after they login. \u00a0Obviously, we need to be able to redirect these users to the front end of the\u2026","rel":"","context":"In &quot;WordPress Development&quot;","block_context":{"text":"WordPress Development","link":"https:\/\/wpscholar.com\/blog\/category\/wordpress-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/posts\/576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/comments?post=576"}],"version-history":[{"count":0,"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/posts\/576\/revisions"}],"wp:attachment":[{"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/media?parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/categories?post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpscholar.com\/wp-json\/wp\/v2\/tags?post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}