Home › Forums › Bug Reports › ACF update_field() reorders my data › Reply To: ACF update_field() reorders my data
Ahah, found it.
It was because the array wasn’t using a standard [0] [1] [2] key sequence. It saved it in the database as expected, but when it output it on the front-end / admin area that it rearranged it by key into ascending numerical order.
Luckily I didn’t need to keep the keys, so I reset them by using array_values() before putting through the update_field() function.
So…
`var_dump($array);
update_field(‘content_blocks’, $array, $post_id);`
… gives us…
array(5) { [5956]=> int(7299) [5107]=> int(3368) [5954]=> int(3514) [5955]=> int(3515) [5957]=> int(7300) }
…and it saves the following into the database:
a:5:{i:5956;s:4:"7299";i:5107;s:4:"3368";i:5954;s:4:"3514";i:5955;s:4:"3515";i:5957;s:4:"7300";}
…and that outputs on the front end in the wrong order.
My fix was to reset the array keys to a basic version like:
`var_dump($array);
$cleaned_array = array_values($array);
update_field(‘content_blocks’, $cleaned_array, $post_id);`
…which gives the same output, but saves the following into the database:
a:5:{i:0;s:4:"7299";i:1;s:4:"3368";i:2;s:4:"3514";i:3;s:4:"3515";i:4;s:4:"7300";}
…which does output in the correct order.
That’s enough to fix the issue in my case. Thanks for your time!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.