spenceddd Posted February 10, 2010 Share Posted February 10, 2010 Hello, I am just trying to work out how replace the contents of the following array: Array ( [0] => Array ( ) [1] => Array ( [0] => 18 [1] => 7 [2] => 5 [3] => 4 ) [2] => Array ( [0] => 48 [1] => 7 ) [3] => Array ( [0] => 7 [1] => 5 [2] => 4 ) [4] => Array ( [0] => 48 [1] => 18 [2] => 7 [3] => 5 [4] => 4 ) ) With a new array which replaces the sub array items with one item that is equal to the count of each sub array.. does that make sense? so the resulting array in this case would look like: Array ( [0] => Array ( ) [1] => Array ( [0] => 4 ) [2] => Array ( [0] => 2 ) [3] => Array ( [0] => 3 ) [4] => Array ( [0] => 5 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/ Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 does that make sense? Not in particularly. Your formatting doesn't really help either. Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/#findComment-1009952 Share on other sites More sharing options...
teamatomic Posted February 10, 2010 Share Posted February 10, 2010 Your array is a bit messy looking but... see if this gives you what you expect $new_array=array(); $i=0; foreach($array as $sub) { $num=count($sub); $new_array[$i]=$num; $i++; } HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/#findComment-1009957 Share on other sites More sharing options...
spenceddd Posted February 10, 2010 Author Share Posted February 10, 2010 Sorry for the nonsensicalness. I was rushing earlier. Here goes again: I have an array which contains a subset of arrays: Array( [0]=> Array( ) [103] => Array( [0]=> 18 [1] => 7 [2] => 5 [3] => 4 ) [112] => Array ( [0] => 48 [1] => 7 ) [123] => Array ( [0] => 7 [1] => 5 [2] => 4 ) [140] => Array ( [0]=> 48 [1] => 18 [2] => 7 [3] => 5 [4] => 4 ) ) What I am trying to is create another array which is derived from this. The subset arrays of the new array would contain just one entry which reflects the count of the original arrays subset counterpart. So the new array would look like this: Array( [100]=> Array( ) [103] => Array( [0]=> 4 ) [112] => Array ( [0] => 2 ) [123] => Array ( [0] => 3 ) [140] => Array ( [0]=> 5 ) ) Thanks for the reply teamatomic, I am just working through your suggestion now. Spencer Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/#findComment-1009990 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 If your first array is called $a. $new = array(); foreach ($a as $k => $v) { $new[$k] = array(count($v)); } Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/#findComment-1009999 Share on other sites More sharing options...
sasa Posted February 10, 2010 Share Posted February 10, 2010 or foreach ($a as $k => $v) { $a[$k][] = count($v); } Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/#findComment-1010002 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 or foreach ($a as $k => $v) { $a[$k][] = count($v); } All that does is add a new element to each inner array containing the count before it was added. Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/#findComment-1010005 Share on other sites More sharing options...
spenceddd Posted February 10, 2010 Author Share Posted February 10, 2010 Great thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/191595-altering-an-array/#findComment-1010046 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.