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

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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'];
  }
}

3 Comments

why u prefer isset over array_key_exists()?? any reason??
There is a small behavior difference between those two functions, but isset() is much faster to handle most common cases. See juliusbeckmann.de/blog/…
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
-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'];
}

4 Comments

wrong way , sendto key always exist in $ov, still gives same error
Yes, I edited since then. But your desired output is ambiguous, I think that '145' in the sixth row, should be 9139. Am I right?
No, 145 is event_id but i want sender_id under each send_to
Yeah, I got that. That is why I'm wondering what does that 145 do in your desired output array's [9192][1] place, which is an event ID.

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.