NotionCommotion Posted November 3, 2017 Share Posted November 3, 2017 (edited) How can I get [4=>[],5=>[],8=>[]] from $arr? Guess I will just walk it, but thought there would be a more intuitive way to do so. Not a big deal, but $arr is still needed so I will need to create a copy. Thanks $arr=[4=>[],5=>['a','b','c'],8=>[1,3,4]]; $arr1=array_fill_keys($arr,[]); $arrCopy=$arr; array_walk($arrCopy,function(&$v){$v=[];}); Edited November 3, 2017 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/305537-set-array-values-and-preserve-keys/ Share on other sites More sharing options...
Solution kicken Posted November 3, 2017 Solution Share Posted November 3, 2017 Do you want to keep the keys, but reset all their values to empty array's? Not really sure what you're asking/trying to do. If you just want the keys + empty array values then you could just do this: $arr2 = array_fill_keys(array_keys($arr), []); 1 Quote Link to comment https://forums.phpfreaks.com/topic/305537-set-array-values-and-preserve-keys/#findComment-1553351 Share on other sites More sharing options...
NotionCommotion Posted November 4, 2017 Author Share Posted November 4, 2017 Perfect! Thanks. I was giving array_fill_keys() an array who's keys instead of values were what I desired. Quote Link to comment https://forums.phpfreaks.com/topic/305537-set-array-values-and-preserve-keys/#findComment-1553380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.