PHP sample: howto distinct the subarray value

The Code

<?php
$myvars = array(
"myvar1" => array("stock" => "C", "mykey" => "1", "name" => "apple"),
"myvar2" => array("stock" => "D", "mykey" => "1", "name" => "apple"),
"myvar3" => array("stock" => "E", "mykey" => "1", "name" => "apple"),
"myvar4" => array("stock" => "F", "mykey" => "1", "name" => "apple"),
"myvar5" => array("stock" => "G", "mykey" => "2", "name" => "orange"),
"myvar6" => array("stock" => "H", "mykey" => "2", "name" => "orange"),
"myvar7" => array("stock" => "I", "mykey" => "2", "name" => "orange"),
"myvar8" => array("stock" => "J", "mykey" => "2", "name" => "orange"),
);


$_myvars = array();
foreach($myvars as $myvar)
{
if ( !in_array($myvar['mykey'], $_myvars) )
{
echo $myvar['mykey'] . "\n" . $myvar['name'] . "\n";  
$_myvars[] = $myvar['mykey'];
}
}
?>

The Result

1 apple 2 orange

Leave a Reply