0

I've multidimential PHP Array array

 $_SESSION['alldata'][0]["thisisval1"] = 1
 $_SESSION['alldata'][0]["markedval1"] = 1
 $_SESSION['alldata'][0]["herechcked"] = 1

 $_SESSION['alldata'][1]["thisisval1"] = 2
 $_SESSION['alldata'][1]["markedval1"] = 1
 $_SESSION['alldata'][1]["herechcked"] = 10

 $_SESSION['alldata'][2]["thisisval1"] = 1
 $_SESSION['alldata'][2]["markedval1"] = 0
 $_SESSION['alldata'][2]["herechcked"] = 1

 $_SESSION['alldata'][3]["thisisval1"] = 3
 $_SESSION['alldata'][3]["markedval1"] = 0
 $_SESSION['alldata'][3]["herechcked"] = 1

 $_SESSION['alldata'][4]["thisisval1"] = 2
 $_SESSION['alldata'][4]["markedval1"] = 1
 $_SESSION['alldata'][4]["herechcked"] = 7

What is required is :

  1. Get list of distinct values of "thisisval1" in the array
  2. Get Count of array elements with "markedval1" = 1 with "thisisval1" = 1

Currently I'm thinking of running loop on $_SESSION['alldata'] and get values required above; Is there any SMART way to do same?

3
  • 1
    If these values are retrieved from a database, depending on the length of the array, it's better for you to go for a query, instead of the loop. If not, I think there isn't a smart way. Commented Dec 16, 2013 at 15:00
  • 1
    Even if you could hack something together in array_map() or whatever, you'd STILL be running at loop at some point, whether yourself explicity, or implicitly within whatever array function(s) you end up using. Commented Dec 16, 2013 at 15:01
  • Is there any way to use array_unique and array_count_values on multidimensional array? Commented Dec 16, 2013 at 15:46

1 Answer 1

1
 echo '<pre>';
 $newarray = array_values($arr);
 $i=0;
 $j=0;//thisisval1
 $distinctarray = array();
 foreach($newarray[0] as $k=>$v){

 if($v['thisisval1']){
    if(!in_array($v['thisisval1'],$distinctarray)){
    $distinctarray[] = $v['thisisval1'];
 }
 }

 if($v['thisisval1'] ==1){

 $i+=1;
 }elseif($v['markedval1'] ==1){
 $j+=1;
 }
 }
 echo 'count for thisisval1='.$i;
  echo 'count for markedval1='.$j;
 print_r($distinctarray);

output

count for thisisval1=2
count for markedval1=2
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Amarjit, I was trying few options using : array_unique and array_count_values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.