-2

i m getting the array as below

$teachers=array(array('post_id' => "81",'video_id' => array("81","73")), array('post_id' => "81",'video_id' => array("81","73")));

if all the key-value are same i would like to display only one key-value ( as in above example) as below :

i would like to display

{ post_id -> array([0]-> 81 [1]-> 73) }

And if its different as in the below example it should display both the arrays..

{ $teachers=array(array('post_id' => "81",'video_id' => array("81","73")), 
array('post_id' => "81",'video_id' => array("81", "59")));}

i would like to display

{post_id -> array([0]-> 81 [1]-> 73 [2] -> 59) }
2

2 Answers 2

0
function array_values_recursive($ary)  {
    $lst = array();
    foreach( array_keys($ary) as $k ) {
        $v = $ary[$k];
        if (is_scalar($v)) {
            $lst[] = $v;
        } elseif (is_array($v)) {
            $lst = array_merge($lst,array_values_recursive($v));
        }
    }
    return array_values(array_unique($lst)); // used array_value function for rekey
}
    $teachers=array(
        array('post_id' => "81",'video_id' => array("81","73")),
        array('post_id' => "81",'video_id' => array("81", "59")));

$flat = array_values_recursive($teachers);
print_r($flat); //OUTPUT : Array ( [0] => 81 [1] => 73 [2] => 59 )
Sign up to request clarification or add additional context in comments.

Comments

0

In this case you can still use array_unique.

$teachers = array_unique($teachers);

Output:

Array
(
    [0] => Array
        (
            [post_id] => 81
            [video_id] => Array
                (
                    [0] => 81
                    [1] => 73
                )
        )
)

http://codepad.org/eC4FR2fq

However note, that it may not work if you have different set of keys except post_id and video_id because

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.

Comments

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.