Jump to content

count arrays


phorcon3

Recommended Posts

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.

Link to comment
Share on other sites

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')

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.