Sinalco
Sinalco

Reputation: 39

How can I using a associative array with the url as a key?

I use the RollingCurl.php from Josh Fraser

At the moment this is the Output from my Skript: (The sequence is every time different, because of the loading Speed of each Website.)

array(17) { [0]=> string(6) "Google" [1]=> string(6) "Google" [2]=> string(6) "Google" [3]=> string(6) "Google" [4]=> string(73) "MSN Deutschland | Nachrichten, Sport, Wetter, Hotmail & Outlook Login" [5]=> string(30) "Microsoft - Official Home Page" [6]=> string(8) "��Ѷ��ҳ" [7]=> string(27) "百度一下,你就知道" [8]=> string(44) "WordPress.com: Create a Free Website or Blog" [9]=> string(71) "Blogger.com - Create a unique and beautiful blog. It’s easy and free." [10]=> string(9) "Wikipedia" [11]=> string(33) "Sign in to your Microsoft account" [12]=> string(7) "YouTube" [13]=> string(27) "Featured Content on Myspace" [14]=> string(12) "新浪首页" [15]=> string(12) "Yahoo! JAPAN" [16]=> string(35) "Twitter. Alles, was gerade los ist." }

Instant of the numbers I will use the $url as a key to set it to a associative array. Like this:

array(17) { [google]=> string(6) "Google" [msn]=> string(73) "MSN Deutschland | Nachrichten, Sport, Wetter, Hotmail & Outlook Login" ...

My Skript:

<?php

$array = [];

// a little example that fetches a bunch of sites in parallel and echos the page title and response info for each request
function request_callback($response, $info) {
    global $array;

    // parse the page title out of the returned HTML
    if (preg_match("~<title>(.*?)</title>~i", $response, $out)) {
        $array[] = $out[1];
    }
}

require("RollingCurl.php");

// top 20 sites according to alexa (11/5/09)
$urls = array("http://www.google.com",
              "http://www.facebook.com",
              "http://www.yahoo.com",
              "http://www.youtube.com",
              "http://www.live.com",
              "http://www.wikipedia.com",
              "http://www.blogger.com",
              "http://www.msn.com",
              "http://www.baidu.com",
              "http://www.yahoo.co.jp",
              "http://www.myspace.com",
              "http://www.qq.com",
              "http://www.google.co.in",
              "http://www.twitter.com",
              "http://www.google.de",
              "http://www.microsoft.com",
              "http://www.google.cn",
              "http://www.sina.com.cn",
              "http://www.wordpress.com",
              "http://www.google.co.uk");
$rc = new RollingCurl("request_callback");
$rc->window_size = 20;
foreach ($urls as $url) {
    $request = new RollingCurlRequest($url);
    $rc->add($request);
}
$rc->execute();

var_dump($array);
?>

Upvotes: 1

Views: 638

Answers (2)

parttimeturtle
parttimeturtle

Reputation: 1215

Arrays in PHP are actually ordered maps and the key values can be either integers or strings. If you do not specify a key when adding a value to an array, PHP will default to using indexed keys, e.g. 0, 1, 2 etc.

When you add elements to an array like this: $array[] = $out[1];, you are not specifying a key, as such PHP defaults to using indexed keys.

If you want to use associative keys (e.g. names), you need to specify the key when you add its value to the array: $array[$somevalue] = $out[1]. In this example, $somevalue would be the URL string you want to use as a key, where ever that may come from.

However, be aware that these keys will need to be unique. If you attempt to add a value to $array['google'] and that key/value pair already exists, the old value will be overwritten with the new value. That may not be ideal for your purposes.

Upvotes: 1

hanshenrik
hanshenrik

Reputation: 21463

$array[$info["url"]] = $out[1];

Upvotes: 1

Related Questions