Skip to main content

Dhimant

I have no idea what the stdClass Object is. I googled it but couldn’t find anything really informative about the stdClass.A Few articles on stackoverflow explains the basic idea behind stdClass but that’s it. I haven’t searched the official documentation though.

Anyways, this article is not about stdClass Object. It is about retrieving values from array whose members are of type stdClass.

Here is a link to official php array documentation : http://php.net/manual/en/language.types.array.php

Now if you print_r the simple multidimensional array It would look something like this.

Example array :

$array = array(
   "foo" => "bar",
   42    => 24,
   "multi" => array(
        "dimensional" => array(
            "array" => "foo"
      )
  )
);

print_r($array) of the above will give the following result.

Array ( [foo] => bar [42] => 24 
[multi] => Array ( [dimensional] => Array ( [array] => foo ) ) )

Now if you want to access the value from “array” you can do that fairly easily with following code.

echo $array["multi"]["dimensional"]["array"];

That is simple enough.

But sometimes you may also encounter an array which would look like this.

Array ( [0] => stdClass Object ( [index] => 1 [title] => This is title 
[coupon] => This is coupon [link] => http://www.google.com )
[1] => stdClass Object ( [index] => 2 [title] => This is title 2
[coupon] => This is coupon 2 [link] => http://www.yahoo.com ) )

Now if you look at the array elements in the first dimension,they are of type stdClass Object. You can not just retrieve the value like we did before in the example.

Syntax to retrieve the values from the array similar to shown above is $array[0]->KEY.

echo $array[0]->link;
echo $array[1]->link;

We can also loop over array objects like this

foreach ($array as $key => $object)
{
echo $object->index;
echo $object->title;
echo $object->coupon;
echo $object->link;
}

Complete Example :

 <?php

$encoded_json = '[{"index":1, "title":"This is title", "coupon":"This is coupon", "link":"http://www.google.com" }, {"index":2, "title":"This is title 2", "coupon":"This is coupon 2", "link":"http://www.yahoo.com"} ]';

$decode = json_decode($encoded_json);
print_r($decode);

// Single Values Retrival
echo "<p>";
echo $decode[0]->index ."<p>";
echo $decode[0]->title;
echo "<p>";
echo $decode[1]->index."<p>";
echo $decode[1]->title;
foreach ($decode as $key => $object)
{
echo "<p>";
echo $object->index;
echo "<p>";
echo $object->title;
echo "<p>";
echo $object->coupon;
echo "<p>";
echo $object->link;
echo "<p>";
}
?>

Output :

Array ( [0] => stdClass Object ( [index] => 1 [title] => This is title [coupon] => This is coupon [link] => http://www.google.com ) [1] => stdClass Object ( [index] => 2 [title] => This is title 2 [coupon] => This is coupon 2 [link] => http://www.yahoo.com ) )

1

This is title

2

This is title 2

1

This is title

This is coupon

http://www.google.com

 

2

This is title 2

This is coupon 2

http://www.yahoo.com

Dhimant

Recently in one of the project I am working on required me to do a “JavaScript” based url redirect. Originally I used a PHP header based 301 redirect

<?
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.google.com  ");
?>

But this approach has one limitation.The limitation is that you can not send any data before the header. My client used third-party JavaScript based tracking service and he also wanted to use this service on a redirect page. ( Client demands. You deliver ). The code given below demonstrates the delayed JavaScript redirect. i.e redirect to a page after a pre specified delay in milliseconds.


<script>
window.onload = function()
{
Redirect();
}
// Redirect after 5 seconds
var count = 5;
function Redirect()
{
if (count > 0){
document.getElementById("redirect").innerHTML = count;
count = count - 1;
setTimeout("Redirect()", 1000);
}
else {
document.getElementById("redirect").innerHTML = "redirecting...";
window.open("http://www.google.com", "_self");
}
}
</script>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="redirect.js"></script>
<title>Redirecting.....</title>
</head>
<body>

<div id="redirect"></div>

</body>
</html>

Dhimant

What you do once you have learned the basics of a new programming language ? Obviously you would want to test your skills ( Self evaluation ). There are few ways to test your ability to program , You could try to solve the programs given in the reference book or come up with a little project of your own. Either way it will help you improve your skills.

But if you would like to know how good you are at programming ( irrespective of the programming language ) compared to other programmers then you should examine one of the following Programming contest sites.

1. HackerRank

HackerRank formerly "InterviewStreet" was the first of its kind. HackerRank offers programming challenges of Artificial Intelligence, Algorithms, Functional Programming, Code Golf - Solve coding problems using the least number of keystrokes and Machine Learning.

HackerRank supports BASH, C, C++, Java, C#, PHP, Ruby, Perl, Haskell, Clojure, Scala, Lua, Go, 
Javascript, Erland, D, OCaml, Pascal, Python 2, Python 3, Groovy, Objective C, F#, VB.NET, LOLCODE, Smalltalk, Tcl, COBOL, Whitespace, Brainfuck, Lisp.

Click Here to check out theirs upcoming contests.

2. CodeChef

CodeChef is a noncommercial organization operated by Directi, an Indian software company based in Mumbai, India. You can either practice, take part in contests or discuss about programming challenges.

They support ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 4.8.1, CPP11, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC.

Supported Programming languages differ from challenge to challenge.

3. TopCoder

There are three types challenges you can take on TopCoder.

* Graphics Design
* Software Development
* Data Science

You could also win cash prizes if you finish in the top three places. Most of the challenges they offer are real world problems provided by real companies / organizations. 

4. programmr

Another favourite of mine is programmr. One of the cool feature of theirs is "Challenge your friend" which lets you challenge one of your friends.

I am not going to write about every site on the list. I recommended that you should explore each website on your own.Here is the rest of the list containing Competitive Programming and Online Judges websites.