0

I have some multimple issues whit this but I never get a good andword about it.

I have some function who respond good but the problmes is is printing x2 my query.

I try to minimize my script to get help fast.

The is about array_merge.

So first

  1. Verify Function
function Verify($params) {
    $query = mysql_query("SELECT SOMETHING");
    if ($query->num_rows > 0) {
        $row = $query->fetch_assoc();
        // Here will pass 2 array on each function to proceed
        $data = self::CacheData($row['gid'], $params['sid']);
        $data['data'] = self::Host($row['gid'], $params['sid']);
        return $tmpdata;
    }
    return false;
}
  1. Create ChacheData and Host Function.
static function CacheData($gid, $sid){
    $cachedata = array();
        $querycache = mysql_query("SELECT WHAT I NEED");
        if ($querycache->num_rows == 1) {
            $rowcache = $querycache->fetch_assoc();
            // Send to Files the array and check if is true
            if (self::Files(array('gid' => $gid, 'sid' => $sid, 'expirecache' => 0)) == true) {
                return array('gid' => $gid, 'size' => 0, 'enabled' => $rowcache['enabled'], 'exists' => false);
            }
        }
}

function Host($gid, $sid) {
    $query = mysq_query("SELECT WHAT I NEED");
    if ($query->num_rows == 0) {
            return false;
    }
    $rowgame = $query->fetch_assoc();
    // Send to Files the array and to see if is / false
    if (self::Files(array('gid' => $gid, 'sid' => $sid)) == false) {
        return false;
    }
}

Ok here is the issues I have

When the Files recive the 2 array it`s working perect but I whant all array send by other function to array_merge all.

So the last function where is the issues is like this

function Files(array $params) {
        $default = array('skipcache' => false, 'expirecache' => 86400, 'os' => null);
        $params = array_merge($default, $params);
        //return $params;
        print_r($params);
}

Now The Output will be like

array(5) {
     ["skipcache"]=>
     bool(false)
     ["expirecache"]=>
     string(1) "0"
     ["os"]=>
     NULL
     ["gid"]=>
     string(1) "1"
     ["sid"]=>
     string(1) "1"
}
array(5) {
     ["skipcache"]=>
     bool(false)
     ["expirecache"]=>
     int(86400)
     ["os"]=>
     NULL
     ["gid"]=>
     string(1) "2"
     ["sid"]=>
     string(1) "1"
}

Base on this output I add on Database but the problmes is is add 2 on database and I need 1 array not 2.

So I was see the array_merge remove dublicates but here I see all it`s same but Is not remove because $params recive 2 array !

Can you give me some solution but not whit Foreach !

Thank you so much !

Regards,

1
  • But your final array's values are different.. Can you also post your expected output ? Commented Mar 27, 2015 at 1:27

1 Answer 1

0

You can try:

$uniqueArr = array_merge(array_flip(array_flip($mergedArray))); 

or

$uniqueArr = array_keys(array_flip($mergedArray)); 

or if you want to use array_unique method:

$uniqueArr = array_unique(array_merge($array1,$array2), SORT_REGULAR);

from this link http://php.net/manual/en/function.array-unique.php which is much faster than array_unique method. you can array_flip so you get the keys as values and vice versa.

4
  • Still not working. The output do not combine the both array. I just want to print only one array. LIKE array(5) { ["skipcache"]=> bool(false) ["expirecache"]=> int(86400) ["os"]=> NULL ["gid"]=> string(1) "2" ["sid"]=> string(1) "1" } Commented Mar 27, 2015 at 1:31
  • Seems working on me see this fiddle: codepad.org/9MPUDCSi, I changed the null and boolean values to empty string and 0 because it prints a warning that array_flip can only flip values with with string and integer types Commented Mar 27, 2015 at 1:50
  • codepad.org/yvpi0Gsf ... if you see is output 2 array but I want to output only 1 array if the key is identical Commented Mar 27, 2015 at 14:36
  • Hi your using methods in a wrong way. because in that way your calling your method two times in which you are just dumping your array two times, you can use a class so you can store the values in a property and will just use an empty method if that property is empty else it will merge the current property to your new property like this: codepad.org/qDEL5GNW better if you start using OOP because it's more easier to code in that way. hope this helps. Commented Mar 30, 2015 at 7:10

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.