0

i have an array in below format

$op = Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [contact_id] => 36
                    [sender_id] => 79
                    [sendto] => 9192
                )

            [1] => Array
                (                       
                    [event_id] => 145
                    [sender_id] => 9139
                    [sendto] => 9192
                )

        )

    [1] => Array
        (
            [0] => Array
                (                       
                    [event_id] => 145
                    [sender_id] => 9272
                    [sendto] => 9290
                )

        )    
    [2] => Array
        (
            [0] => Array
                (                        
                    [event_id] => 145
                    [sender_id] => 9138
                    [sendto] => 9316
                )

            [1] => Array
                (                       
                    [event_id] => 145
                    [sender_id] => 9283
                    [sendto] => 9316
                )

        )
)  

i want to filter array in a way that resultant array's key should be different sendto values and all sender_id under that sendto shoud come under that array's key

Desired output

Array
(
    [9192] => Array
        (
            [0] =>79
            [1] =>9139
        )
    [9290] =>Array
        (
            [0]=>9272
        )
    [9316] =>Array
        (
            [0] =>9138
            [1] =>9283
        )
)
 

although i tried with below code

foreach ($op as $ok=>$ov)
{
    if( array_key_exists($ov['sendto'],$mid))
        $mid[$ov['sendto']][]=$ok;
    else
        $mid[$ov['sendto']]=$ok;
}

but this one display notice:Undefined index: sendto

please tell me where i m doing wrong?? i always stuck in such problem

Community
  • 1
  • 1
xkeshav
  • 53,360
  • 44
  • 177
  • 245

3 Answers3

2

Something like this:

<?php
//Test Array
$op = array(
    array(
        array(
              'contact_id' => 36,
              'sender_id' => 79,
              'sendto' => 9192
        ),
        array(
              'contact_id' => 145,
              'sender_id' => 9139,
              'sendto' => 9192
        )
    ),
    array(
        array(
              'contact_id' => 145,
              'sender_id' => 9272,
              'sendto' => 9290
        )
    ),
    array(
        array(
              'contact_id' => 145,
              'sender_id' => 9138,
              'sendto' => 9316
        ),
        array(
              'contact_id' => 145,
              'sender_id' => 9283,
              'sendto' => 9316
        )
    ),
);

//Switch array format
$new = array();
foreach($op as $element)
{
    foreach($element as $entity)
    {
        if(!isset($new[$entity['sendto']]))
        {
           $new[$entity['sendto']] = array();
        }

        $new[$entity['sendto']][] = $entity['sender_id'];
    }
}

//Debug the new array.
print_r($new);
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
0

Try:

$mid = array();

foreach($op as $tmp_array)
{
  foreach($tmp_array as $message)
  {
    if (!isset($mid[$message['sendto']]))
      $mid[$message['sendto']] = array();

    $mid[$message['sendto']][] = $message['sender_id'];
  }
}
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
  • why u prefer `isset` over `array_key_exists()`?? any reason?? – xkeshav Mar 23 '11 at 13:18
  • There is a small behavior difference between those two functions, but `isset()` is much faster to handle most common cases. See http://juliusbeckmann.de/blog/php-benchmark-isset-or-array_key_exists.html – Maxime Pacary Mar 23 '11 at 13:38
  • I doubt there is any reason, but due to the fact that `isset` is used on all forms of variables its just common to use `isset` over other functions – RobertPitt Mar 23 '11 at 13:40
-1

You should go like this:

foreach ($op as $ok=>$ov)
{
    if(!array_key_exists('sendto',$mid))
    {

        $mid[$ov['sendto']] = array();
    }

    $mid[$ov['sendto']][] = $ov['sender_id'];
}
Imi Borbas
  • 3,683
  • 1
  • 19
  • 16