Sort array data without duplicate

时间:2016-02-03 03:13:54

标签: php arrays twitter

I'm making a twitter bot using Codebird.

I want to sort the data status in php array without duplicates. Line by line (urls media /remote file links)

This my code:

require_once ('codebird.php');
\Codebird\Codebird::setConsumerKey("pubTRI3ik5hJqxxxxxxxxxx", "xxxxxS6Uj1t5GJPi6AUxxxxx");

$cb = \Codebird\Codebird::getInstance();

$cb->setToken("xxxxxxx-aVixxxxxxxxxX5MsEHEK", "Dol6RMhOYgxxxxxxFnDtJ6IzXMOLyt");

$statusimgs = array (
    "/images.com/hfskehfskea33/jshdfjsh.jpeg",
"/pic.images.com/SDjhs33/sZddszf.jpeg",
"/pic.images.com/dfggfd/dgfgfgdg.jpeg",
"//pic.images.com/xgxg/xdgxg6.jpeg",
);

$params = array(
  'status' => 'halo my ststus',
  'media[]' => $statusimgs[array_rand($statusimgs)]
);

$reply = $cb->statuses_updateWithMedia($params);

Initially I use random array, but this can make duplicate photos.

I want to sort link remote files from first line to last. I have 1-100 link images to upload on twitter from remote file methot. One by one when script execute manual or with cron.

I want set cron every 60s , 60s 1 photo tweet.

1 个答案:

答案 0 :(得分:0)

I understand your question as "Remove duplicates from an array and sort it".
So, you could try this:

  • Make $statusarray unique,
  • sort $statusarray,
  • add $statusarray to $params array (key = 'media').

Code

    <?php
        // Input array
        $statusimgs = array (
            "/images.com/hfskehfskea33/jshdfjsh.jpeg",
            "/pic.images.com/SDjhs33/sZddszf.jpeg",
            "/pic.images.com/dfggfd/dgfgfgdg.jpeg",
            "/pic.images.com/dfggfd/dgfgfgdg.jpeg",
            "/pic.images.com/xgxg/xdgxg6.jpeg",
            "/pic.images.com/xgxg/xdgxg6.jpeg",
            "/pic.images.com/xgxg/xdgxg6.jpeg",
        );

        // Make unique and sort
        $statusimgs = array_unique($statusimgs, SORT_STRING);
        sort($statusimgs,SORT_STRING);

        // Create the resulting $params array
        $params = array(
            'status' => 'halo my ststus',
            'media' => $statusimgs
        );

        // Display the result
        echo '<pre>'; var_dump($params); '</pre>';

        //$reply = $cb->statuses_updateWithMedia($params);
    ?>

Result

array(2) {
  ["status"]=>
  string(14) "halo my ststus"
  ["media"]=>
  array(4) {
    [0]=> string(39) "/images.com/hfskehfskea33/jshdfjsh.jpeg"
    [1]=> string(36) "/pic.images.com/SDjhs33/sZddszf.jpeg"
    [2]=> string(36) "/pic.images.com/dfggfd/dgfgfgdg.jpeg"
    [3]=> string(32) "/pic.images.com/xgxg/xdgxg6.jpeg"
  }
}

Alternative Code

<?php
$statusimgs = array (
    "/images.com/hfskehfskea33/jshdfjsh.jpeg",
    "/pic.images.com/SDjhs33/sZddszf.jpeg",
    "/pic.images.com/dfggfd/dgfgfgdg.jpeg",
    "/pic.images.com/dfggfd/dgfgfgdg.jpeg",
    "/pic.images.com/xgxg/xdgxg6.jpeg",
    "/pic.images.com/xgxg/xdgxg6.jpeg",
    "/pic.images.com/xgxg/xdgxg6.jpeg",
);

$statusimgs = array_unique($statusimgs, SORT_STRING);
sort($statusimgs,SORT_STRING);

session_start();
if (!isset($_SESSION['index'])) {
    $_SESSION['index'] = 1;
} else {
    $_SESSION['index'] = ($_SESSION['index']>=count($statusimgs)) ? 1 : $_SESSION['index']+1;
}
session_write_close();

$params = array(
    'status' => 'halo my ststus',
    'media' => $statusimgs
);

// echo '<pre>'; var_dump($params); '</pre>';
echo 'Choosen: ' . $params['media'][$_SESSION['index']-1] . '<br />';

//$reply = $cb->statuses_updateWithMedia($params);
?>