phorcon3 Posted January 12, 2008 Share Posted January 12, 2008 here's the code I have: <?php foreach($something as $item => $value) { if($value[1] == 'MN') { /*Here I already need to know the total number of arrays this code will output*/ echo '<td>'.$value[0].'</td>'; } } ?> I tried using: <?php $total_arrays = count($value[0]); ?> or <?php $total_arrays = array_count($value[0]); ?> but I just can't figure it out how to do it. I have to know the total number of arrays so I know when to insert a line break. I hope this makes sense. I'd appreciate any help/comments on this. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/ Share on other sites More sharing options...
Ken2k7 Posted January 12, 2008 Share Posted January 12, 2008 $total_arrays = count($something); That it? Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437397 Share on other sites More sharing options...
phorcon3 Posted January 12, 2008 Author Share Posted January 12, 2008 yah, but the problem is, i'm using the if function and i only need the total number of arrays meeting that specific requirement $total_arrays = count($something); will output the total of arrays, even those not meeting the if-requirement, which is for example <?php if($value[1] == 'MN') ?> Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437401 Share on other sites More sharing options...
phorcon3 Posted January 12, 2008 Author Share Posted January 12, 2008 because what I am trying to do is, to create 3 columns. this code above might output 900 values meeting the requirement that the $value[1] == 'MN' and then I want to insert a line break at 300, so it will create 3 columns with the exact number of values... if that makes any sense lol Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437406 Share on other sites More sharing options...
Ken2k7 Posted January 12, 2008 Share Posted January 12, 2008 Can't remember this...still kinda sleepy. $total_arrays = count($value); or $total_arrays = count($item); Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437408 Share on other sites More sharing options...
phorcon3 Posted January 12, 2008 Author Share Posted January 12, 2008 lol sorry bout that, didnt mean to bug u that early in the mornin.. nah, that doesnt do it either.. $total_arrays = count($value); will output the number of slots in an array <?php $something = array( '1' => array('slot1', 'slot2', 'slot3'), ); ?> in my case 3 and $total_arrays = count($item); will just output 1 but thanks for ur help;) i might as well just try to come up with a better way to do this... Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437413 Share on other sites More sharing options...
Barand Posted January 12, 2008 Share Posted January 12, 2008 echo count ($something[1]); Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437427 Share on other sites More sharing options...
Ken2k7 Posted January 12, 2008 Share Posted January 12, 2008 You didn't bother me. It's just early. I could very well not answer this if I wanted. So it's not your fault. Is there only 1 array or are there more? If there are more, you probably want: $total_arrays = count($something, COUNT_RECURSIVE) - count($something); Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437430 Share on other sites More sharing options...
phorcon3 Posted January 12, 2008 Author Share Posted January 12, 2008 <?php $something = array( '1' => array('array1', 'MN'), '2' => array('array2', 'MN'), '3' => array('array3', 'MN'), '4' => array('array4', 'MN'), '5' => array('array5', 'MN'), '6' => array('array6', 'MN'), '7' => array('array7', 'MN'), '8' => array('array8', 'MN'), '9' => array('array9', 'AL'), ); foreach($something as $item => $value) { if($value[1] == 'MN') { $total = count($something); echo '<div>'.$value[0].' / total_arrays: '.$total.'</div>'; } } ?> this will output: total_arrays: 9 even tho there are only 8 arrays meeting the requirement of $value[1] == 'MN' that's the problem. Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437436 Share on other sites More sharing options...
phorcon3 Posted January 12, 2008 Author Share Posted January 12, 2008 the crappiest coding i could think of is: <?php $count = ''; foreach($something as $item => $value) { if($value[1] == 'MN') { $count++; } } foreach($something as $item => $value) { if($value[1] == 'MN') { echo '<div>'.$value[0].' / total_arrays: '.$count.'</div>'; } } ?> but that's just REALLY crappy coding, and it will use up alotta load time too, I'm guessing.. that's what sucks bout it Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437442 Share on other sites More sharing options...
Barand Posted January 12, 2008 Share Posted January 12, 2008 if you reorganise your array $something = Array ( [array1] => MN [array2] => MN [array3] => MN [array4] => MN [array5] => MN [array6] => MN [array7] => MN [array8] => MN [array9] => AL ) then <?php $k = array_count_values($something); echo $k['MN'] ; // 8 Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437456 Share on other sites More sharing options...
phorcon3 Posted January 12, 2008 Author Share Posted January 12, 2008 yah, but the problem is, my array looks actually completely different than the one above <?php $something = array( '1' => array('info', 'MN', 'info', 'info'), '2' => array('info', 'MN', 'info', 'info'), '3' => array('info', 'MN', 'info', 'info'), '4' => array('info', 'MN', 'info', 'info'), '5' => array('info', 'MN', 'info', 'info'), '6' => array('info', 'MN', 'info', 'info'), '7' => array('info', 'MN', 'info', 'info'), '8' => array('info', 'MN', 'info', 'info'), '9' => array('info', 'AL', 'info', 'info'), ); ?> but thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437501 Share on other sites More sharing options...
Ken2k7 Posted January 13, 2008 Share Posted January 13, 2008 Well in that case, you'll just have to loop through it the hard way. I don't know a short way to do this. <?php foreach($something as $item => $value) { $total = 0; foreach($something as $b => $c) if($c[1]=='MN') $total++; echo $total; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85704-count-arrays/#findComment-437633 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.