0

This the array...

$rt = array(
    '0' => array(
    'nombre'=>'Jojo',
    'fecah'=> '195',
    'fch'=>'12'
    ),
    '1' => array(
        'nombre'=>'Tito',
        'fecah'=> '197',
        'fch'=>'13'
    ),
    '2' => array(
        'nombre'=>'Jojo',
        'fecah'=> '195',
        'fch'=>'12'
    ),
    '3' => array(
        'nombre'=>'Joji',
        'fecah'=> '195',
        'fch'=>'12'
    ),
);

and this is my code:

$a = array();
foreach ($rt as $k=>$v) {
    if (in_array($v['nombre'], $a) && in_array($v['fecah'], $a) && in_array($v['fch'], $a) ) {
        $a[]=$k;
        echo 'The name ' . $v['nombre'] .' is in the array<br>';
    } else {
        echo 'The name is not in the array <br>';
    }
}

so as you can see in the array index [2] the information also exist in the index [0], so I don't need it again, so I need a away to see if the data get repeated or not... if the data is "unique" then build the new array with the "unique" data if the data already exist then jump to next, but I need to compare the 3 keys not just the name... so how do I do that?

Tanker
  • 1,178
  • 3
  • 16
  • 49
  • Yes and no, with that I only get a unique "array", which is fine, but what I need to know what arrays or values are repeated? ... – Tanker Sep 01 '14 at 22:26
  • If you get the unique array, you can then do a diff to find the repeated values. – scrowler Sep 01 '14 at 22:27
  • dimmit!... you are totally correct, I don't know what I'm thinking... so.. I get the unique arrays `$unique_arrs` and then I just do a `$repeated_arrs = array_diff($unique_arrs, $rt);`... correct? – Tanker Sep 01 '14 at 22:31
  • ok, that didn't worked... as far as I know `array_diff()` only work on simple arrays not multidimensional arrays, so I guess I have to build a second array for each array, `$arr2[] = $an->name . ' ' .$an->fecah;` and so on like a single string but then is the matter to retrieve the information, which I can put the ID at the end then just `substr();` whatever i need and build a 4th query to view the information... well, like you said in theory it might work... thanks... – Tanker Sep 01 '14 at 22:51
  • It's conceptually what you need to do - go back to the duplicate I flagged, and instead of returning a unique array (multidimensional), return the common values if that is what you want. `array_diff()` will work in the same way with that function if you are serializing everything – scrowler Sep 01 '14 at 22:53
  • Actually yes, that might work, but, I'm doing what I just find in this url http://nl3.php.net/manual/en/function.array-diff.php#37527 with that I got all the unique "array" and another array for the repeated arrays, that did the trick... thank you. – Tanker Sep 01 '14 at 23:00

2 Answers2

0

You insert array when it is in_array and you use AND

you should reverse your codes like this

$a = array();
foreach ($rt as $k=>$v) {
    if (!in_array($v['nombre'], $a) || !in_array($v['fecah'], $a) || !in_array($v['fch'], $a) ) {
        $a[]=$k;
        echo 'The name ' . $v['nombre'] .' is in the array<br>';
    } else {
        echo 'The name is not in the array <br>';
    }
}
  • This won't work, it's a multi dimensional array so you can't use `in_array()` to check whether the keys exist within it since they will be a level deeper than `in_array()` checks for. – scrowler Sep 02 '14 at 01:51
  • What i see is he tries to insert each deeper level of array into one dimension array – Shen Siddharta Sep 02 '14 at 01:53
  • You say `if(!in_array($v['nombre'], $a))` - but `$a` is an array like: `array(0 => array('nombre' => 'whatever, 'etc' => 'foobar'))` - won't work. – scrowler Sep 02 '14 at 01:56
-1

This is you need this

$newarray = array();
foreach($rt as $key => $value)
{
    $nombre = $value['nombre'];
    if(!isset($newarray[$nombre])) 
    {
       $newarray[$nombre] = $value;
    }
}
kefy
  • 535
  • 2
  • 10