A
A
Ad4ptec2015-07-16 11:12:46
PHP
Ad4ptec, 2015-07-16 11:12:46

How to equalize an array by a unique value?

At the moment, the code is not working (displays the original values):

<?php
$array = Array(
  '0' => Array(
    '0' => 'http://www.example.com/index.php',
    '1' => 'www.example.com/index.php',
    '2' => 'www.example.com/forum/index.php'),
  '1' => Array(
    '0' => 'http://',
    '1' => ''),
  '2' => Array(
    '0' => 'www.example.com'),
  '3' => Array(
    '0' => 'example.'),
  '4' => Array(
    '0' => 'com'),
  '5' => Array(
    '0' => '/index.php',
    '1' => '/forum/index.php')
  );

function arrayUnique($myArray) {
    $newArray = Array();
    if (is_array($myArray)) {
        foreach($myArray as $key=>$val[2]) {
            if (is_array($val[2])) {
                $val2 = arrayUnique($val[2]);
                } else {
                $val2 = $val[2];
                $newArray=array_unique($myArray);
                break;
                }
            if (!empty($val2)) {
                $newArray[$key] = $val2;
                }
            }
        }
    return ($newArray);
    }
$output=arrayUnique($array);

print_r($output);
?>

The main task is to check for uniqueness of the values ​​of 2 arrays (www.example.com) and remove extra keys in the remaining arrays.
I found the arrayUnique function in the php documentation, but for some reason it does not work, tell me what's wrong, can anyone come across a similar task?
After processing, the array should look like this:
Array
(
    [0] => Array
        (
            [0] => http://www.example.com/index.php
        )

    [1] => Array
        (
            [0] => http://
        )

    [2] => Array
        (
            [0] => www.example.com
        )

    [3] => Array
        (
            [0] => example.
        )

    [4] => Array
        (
            [0] => com
        )

    [5] => Array
        (
            [0] => /index.php
        )

)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-07-16
@Ad4ptec

$array = array(
  array(
    'http://www.example.com/index.php',
    'www.example.com/index.php',
    'www.example.com/forum/index.php'
  ),
  array('http://', ""),
  array('www.example.com'),
  array('example.'),
  array('com'),
  array('/index.php', '/forum/index.php')
  );
  
$NewArr = array(2 => $array[2]);
foreach($array as $k => $arr){
    if($k != 2){
        $NewArr[$k] = array();
        foreach($arr as $key => $tag){
            foreach($array[2] as $if){
                if(preg_match("/".preg_quote($if)."/i", $tag)){
                    $NewArr[$k][count($NewArr[$k]) + 1] = $tag;
                    continue 2;
                }
            }
        }
    }
}

print_r($NewArr);

A
Alexey Ukolov, 2015-07-16
@alexey-m-ukolov

Maybe you just need to use parse_url ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question