lindm Posted November 29, 2008 Share Posted November 29, 2008 I have the following array: $array = array( 'FIGURE1cy'=>100, 'FIGURE1py'=>100, 'FIGURE2cy'=>100, 'FIGURE2py'=>100 , 'somethingelse'=>hello, 'somethingelse2'=>hello2 etc etc ); I am looking for two ways to manipulate this array: 1. Remove all elements where the key ends with cy. I could use unset but there must be a simpler way since the array is fairly large. Some way to iterate through the array with unset?? 2. Perform number 1 and change the name of all keys ending with cy to py. Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/ Share on other sites More sharing options...
Mark Baker Posted November 29, 2008 Share Posted November 29, 2008 Looping through the array and using unset is probably the easiest method, but array_walk() provides you with an alternative. <?php $array = array( 'FIGURE1cy' => 100, 'FIGURE1py' => 100, 'FIGURE2cy' => 100, 'FIGURE2py' => 100 , 'somethingelse' => 'hello', 'somethingelse2' => 'hello2' ); function arrayWalkFunc($item1, $key, $array) { if (substr($key,-2) == 'cy') { unset($array[$key]); } } array_walk($array,'arrayWalkFunc',&$array); echo '<pre>'; print_r($array); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-701854 Share on other sites More sharing options...
lindm Posted November 30, 2008 Author Share Posted November 30, 2008 Thanks! Any suggestion if I want to rename all keys ending with cy to py? Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-702277 Share on other sites More sharing options...
Mark Baker Posted November 30, 2008 Share Posted November 30, 2008 Any suggestion if I want to rename all keys ending with cy to py? You could use the same basic technique. However, that would cause problems with your test array, where you already have a key ending in 'py' that matches one of the 'cy' keys that you want to rename. <?php $array = array( 'FIGURE1cy' => 100, 'FIGURE1py' => 100, 'FIGURE2cy' => 100, 'FIGURE2py' => 100 , 'somethingelse' => 'hello', 'somethingelse2' => 'hello2' ); function arrayWalkFunc($item1, $key, $array) { if (substr($key,-2) == 'cy') { $newkey = substr($key,0,-2).'py'; unset($array[$key]); $array[$newkey] = $item1; } } array_walk($array,'arrayWalkFunc',&$array); echo '<pre>'; print_r($array); echo '</pre>'; ?> And again, a straight loop would be a better approach. Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-702357 Share on other sites More sharing options...
lindm Posted November 30, 2008 Author Share Posted November 30, 2008 Is there a good example with a straight loop? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-702494 Share on other sites More sharing options...
Barand Posted November 30, 2008 Share Posted November 30, 2008 Just one problem Users may not change the array itself from the callback function. e.g. Add/delete elements' date=' unset elements, etc. [/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-702504 Share on other sites More sharing options...
Mark Baker Posted November 30, 2008 Share Posted November 30, 2008 Just one problem Users may not change the array itself from the callback function. e.g. Add/delete elements' date=' unset elements, etc. [/quote'] Except my examples actually work because I'm explicitly passing the third parameter to array_walk() by reference; Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-702514 Share on other sites More sharing options...
lindm Posted December 6, 2008 Author Share Posted December 6, 2008 Would it be possible to use array_map and unset somehow? $array=$_POST; function remove($item) { if (substr($item,-2) == 'py') { unset($item); } } $b = array_map("remove", $_POST); echo json_encode($b); Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-707861 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 function remove(&$item) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { unset($item[$key]); } } } $b = array_map("remove", $_POST); echo json_encode($b); I think that would work. You have to pass it by reference from what I remember.... Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-707899 Share on other sites More sharing options...
lindm Posted December 6, 2008 Author Share Posted December 6, 2008 Tried this: $array = array( 'FIGURE1cy'=>100, 'FIGURE1py'=>100, 'FIGURE2cy'=>100, 'FIGURE2py'=>100 , 'somethingelse'=>hello, 'somethingelse2'=>hello2 ); function remove(&$item) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { unset($item[$key]); } } } $b = array_map("remove", $array); echo json_encode($b); But get the error: Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21 Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21 Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21 Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21 Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21 Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 21 {"FIGURE1cy":null,"FIGURE1py":null,"FIGURE2cy":null,"FIGURE2py":null,"somethingelse":null,"somethingelse2":null} Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-707938 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 function remove($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { unset($item[$key]); } } return $item; } } $b = remove($_POST); echo json_encode($b); Should work as long as $_POST is an array, so make sure you are posting data. Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-707944 Share on other sites More sharing options...
lindm Posted December 6, 2008 Author Share Posted December 6, 2008 Seems to work! Now I have a follow up for another function... I want to rename all keys ending with "cy" to "py". So $array = array( 'FIGURE1cy'=>100, 'FIGURE2cy'=>100, 'somethingelse'=>hello, 'somethingelse2'=>hello2 ); would become $array = array( 'FIGURE1py'=>100, 'FIGURE2py'=>100, 'somethingelse'=>hello, 'somethingelse2'=>hello2 ); My code so far (based on your code above): function rename($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'cy') { XXXXXX } } return $item; } } $b = rename($array); echo json_encode($b); Not sure about the XXXX or if this is the best way to handle the problem? Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-707970 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 function rename($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'cy') { $key = str_replace("cy", "py", $key); $item[$key] = $val; } } return $item; } } $b = rename($array); echo json_encode($b); Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708014 Share on other sites More sharing options...
lindm Posted December 6, 2008 Author Share Posted December 6, 2008 When testing: $array = array( 'FIGURE1cy'=>100, 'FIGURE2cy'=>100, 'somethingelse'=>hello, 'somethingelse2'=>hello2 ); function rename($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'cy') { $key = str_replace("cy", "py", $key); $item[$key] = $val; } } return $item; } } $b = rename($array); echo json_encode($b); Gives the following error: Fatal error: Cannot redeclare rename() in /Library/WebServer/Documents/htdocs/phptest2/script/jsonnewyear.php on line 36 Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708065 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 You have the function defined somewhere else. Comment out the old function or rename that one to replace or something like that Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708071 Share on other sites More sharing options...
lindm Posted December 6, 2008 Author Share Posted December 6, 2008 Excellent! rename is of course an already built in function so I can't use that name. Many thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708094 Share on other sites More sharing options...
lindm Posted December 6, 2008 Author Share Posted December 6, 2008 Had a final follow up of course... If I want to change the value of all keys ending with py to zero how should I do then (see xxx below): function ny($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { xxx } } return $item; } } Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708098 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 Had a final follow up of course... If I want to change the value of all keys ending with py to zero how should I do then (see xxx below): function ny($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { xxx } } return $item; } } You cant figure that out given the code prior....wow. No offense but I would of hoped you could do that by now by looking at prior code and just basics of php arrays. I would read up on arrays if you are going to be using them alot and learn how they work. function ny($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { $item[$key] = 0; } } return $item; } } Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708099 Share on other sites More sharing options...
lindm Posted December 6, 2008 Author Share Posted December 6, 2008 Thank you for the patience, I am fairly new to arrays, loops etc. One strange thing with the code gathered so far (example below): $array = array( 'FIGURE1cy'=>10, 'FIGURE2cy'=>20, 'FIGURE1py'=>30, 'FIGURE2py'=>40, 'somethingelse'=>hello, 'somethingelse2'=>hello2 ); function ny($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { $item[$key] = 0; } if (substr($key,-2) == 'cy') { $key = str_replace("cy", "py", $key); $item[$key] = $val; } if (substr($key,-2) == 'py') { $key = str_replace("py", "cy", $key); $item[$key] = $val; } } return $item; } } $b = ny($array); echo json_encode($b); This returns the following: {"FIGURE1cy":30,"FIGURE2cy":40,"FIGURE1py":0,"FIGURE2py":0,"somethingelse":"hello","somethingelse2":"hello2"} I was hoping it would have been: {"FIGURE1cy":0,"FIGURE2cy":0,"FIGURE1py":10,"FIGURE2py":20,"somethingelse":"hello","somethingelse2":"hello2"} The goal is to change this array: $array = array( 'FIGURE1cy'=>10, 'FIGURE2cy'=>20, 'FIGURE1py'=>30, 'FIGURE2py'=>40, 'somethingelse'=>hello, 'somethingelse2'=>hello2 ); to $array = array( 'FIGURE1cy'=>0, 'FIGURE2cy'=>0, 'FIGURE1py'=>10, 'FIGURE2py'=>20, 'somethingelse'=>hello, 'somethingelse2'=>hello2 ); Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708113 Share on other sites More sharing options...
premiso Posted December 6, 2008 Share Posted December 6, 2008 The 3rd if is overwriting the 0, cause py is being checked again and assigning it $val. Try this: function ny($item) { if (is_array($item)) { foreach ($item as $key => $val) { if (substr($key,-2) == 'py') { $val = 0; } if (substr($key,-2) == 'cy') { $key = str_replace("cy", "py", $key); $item[$key] = $val; } if (substr($key,-2) == 'py') { $key = str_replace("py", "cy", $key); $item[$key] = $val; } } return $item; } } Should work like you want it to. Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708119 Share on other sites More sharing options...
lindm Posted December 7, 2008 Author Share Posted December 7, 2008 Perfect! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/134783-solved-manipulate-array-remove-certain-keys-and-renaming-keys/#findComment-708359 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.