-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) }
Sean
  • 12,443
  • 3
  • 29
  • 47
dhairya
  • 375
  • 1
  • 4
  • 10
  • http://www.php.net/manual/en/function.array-merge-recursive.php#92195 – Cito Mar 16 '13 at 05:24
  • 1
    Is this the same question that you asked here - http://stackoverflow.com/questions/15445624/how-to-convert-multidimensional-associative-array-into-single-dimensional-array - except you changed the values? – Sean Mar 16 '13 at 05:26

2 Answers2

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.

dfsq
  • 191,768
  • 25
  • 236
  • 258
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 )
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53