3

HI I am fairly new to php.

I have an array

$arr = array(0 => array('GID'=>1,'groupname'=>"cat1",'members'=>array(0=>array('mid'=>11,'mname'=>'wwww'),1=>array('mid'=>12,'mname'=>'wswww'))), 1 => array('GID'=>2,'groupname'=>"cat2",'members'=>array(0=>array('mid'=>13,'mname'=>'gggwwww'),1=>array('mid'=>14,'mname'=>'wvvwww'))), 2 => array('GID'=>3,'groupname'=>"cat1",'members'=>array(0=>array('mid'=>15,'mname'=>'wwddsww')),1=>array('mid'=>16,'mname'=>'wwwdddw')));

      ie...,I have GID,groupname,mid(member id),mname(member name).I want to insert a new mid and mname into a group if it is already in the array ,if it is not exists then create a new subarray with these elements..I also need to check a member id(mid) is also present.........................I used the code but its not working fine............. if (!empty($evntGroup)) {


            foreach ($evntGroup as $k => $group) {

                if ($group['GID'] == $group_id) {
                    foreach($group as $j=> $mem){

                    if($mem['mid'] == $mem_id){

                        unset($evntGroup[$k]['members'][$j]['mid']);
                        unset($evntGroup[$k]['members'][$j]['mname']);
                    }
                    else{


                    $evntGroup[$k]['members'][] = array(
                        'mid' => $mem_id,
                        'mname' => $mem_name);

                    }}
                } else {


                    $evntGroup[] = array(
                        'GID' => $group_id,
                        'groupname' => $Group['event_group_name'],
                        'members' => array(
                            0 => array(
                                'mid' => $mem_id,
                                'mname' => $mem_name
                            )
                        )
                    );

                }

            }
        } else {

            $evntGroup[$i]['GID'] = $group_id;
            $evntGroup[$i]['groupname'] = $Group['event_group_name'];
            $evntGroup[$i]['members'][] = array(
                'mid' => $mem_id,
                'mname' => $mem_name);
            $i++;

        }
1
  • Its the mid is under members which has its own ID ... What wold your expected result be like of 'GID'=>1 is modified Commented Dec 4, 2012 at 12:13

3 Answers 3

2

In the form of a function, the easiest solution will look something like this:

function isGidInArray($arr, $val) {
    foreach($arr as $cur) {
        if($cur['GID'] == $val)
            return true;
    }
    return false;
}

You've updated your question to specify what you want to do if the specified GID is found, but that's just a trivial addition to the loop:

function doSomethingIfGidInArray($arr, $val) {
    foreach($arr as $cur) {
        if($cur['GID'] == $val) {
            doSomething();
            break; //Assuming you only expect one instance of the passed value - stop searching after it's found
        }
    }
}

There is unfortunately no native PHP array function that will retrieve the same index of every array within a parent array. I've often wanted such a thing.

Sign up to request clarification or add additional context in comments.

1 Comment

ie...,I have GID,groupname,mid(member id),mname(member name).I want to insert a new mid and mname into a group if it is already in the array ,if it is not exists then create a new subarray with these elements..I also need to check a member id(mid) is also present.........................I used the code but its not working fine.............
1

Something like this will match if GID equals 3:

foreach( $arr as $item ) {
  if( $item['GID'] == 3 ) {
     // matches
  }
}

Comments

1

There is the code

function updateByGid(&$array,$gid,$groupname,$mid,$mname) {
    //For each element of the array
    foreach ($array as $ii => $elem) {
        //If GID has the same value
        if ($elem['GID'] == $gid) {
            //Insert new member
            $array[$ii]['members'][]=array(
                        'mid'=>$mid,
                        'mname'=>$mname);
            //Found!
            return 0;
        }
    }       
    //If not found, create new
    $array[]=array(
            'GID'=>$gid,
            'groupname'=>$groupname,
            'members'=>array(
                0=>array(
                    'mid'=>$mid,
                    'mname'=>$mname
                )
            )
    );

    return 0;
}

3 Comments

I have GID,groupname,mid(member id),mname(member name).I want to insert a new mid and mname into a group if it is already in the array ,if it is not exists then create a new subarray with these elements
and ... What is the problem? What's missing?
then, do you want to look for a mid and mname inside a GID and if you don't find it, insert a new member? Do you have the GID which you want to use or Do you have to create a new one (if it doesn't exist)? I think the solution is easy, but I don't quite understand you

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.