简体   繁体   中英

array_merge(): Argument #1 is not an array

need your help on this... got an error on my array_merge here's my code:

//first
    $url1="https://www.zopim.com/api/v2/chats";
    $ch1 = curl_init();
    curl_setopt($ch1, CURLOPT_URL, $url1);
    curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
    $output1 = curl_exec($ch1);
    $info1 = curl_getinfo($ch1);
    curl_close($ch1);

    $chats1 = json_decode($output1,true);

    //second
    $url2="https://www.zopim.com/api/v2/chats?page=2";
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_URL, $url2);
    curl_setopt($ch2, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    $output2 = curl_exec($ch2);
    $info2 = curl_getinfo($ch2);
    curl_close($ch2);

    $chats2 = json_decode($output2,true);



    $r = [];
    if(is_array($chats1) && is_array($chats2))
    {
        foreach($chats1 as $key => $array)
        {
            $r[$key] = array_merge($chats2[$key], $array);
        }
    }
    else
    {
    echo 'problem with json';
    }
    echo json_encode($r, JSON_UNESCAPED_SLASHES);

I need to combine a two json using array_merge()... im using curl authorization to call my API..

but when i try to run the code it has a error: 在此处输入图片说明

here's number 44 error: 在此处输入图片说明

This means your json_decode fails. It will fail if the string is no valid JSON. When it fails json_decode returns either null or false, so you have to check if the response is valid:

$chats1 = json_decode($output1, true);
$chats2 = json_decode($output2, true);

if ($chats1 && $chats2 && is_array($chats1) && is_array($chats2)) {
    // your code goes here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM