-2

I have an array $s which stores incoming data from a JSON feed. The format of the array is like this:

     $s = array("tweets"=>1, "likes"=>4, "plusones"=>7);

How would I loop through the array: $s, and pick the values of the keys so that I can then assign to the variables $tweets, $likes and $plusones.

This was what I did but I'm not able to extract the values and store them in the variables I've set to nil

   foreach($social_shares as $social_share=>$include){
     echo $social_share . ": " . $include . "<br>";
        $tweets = nil;
        $likes = nil;
        $plusones = nil;

   $sql4 = mysql_query("INSERT INTO social_shares(article_id, tweets, likes, plusones ) VALUES ($s -> article_id ,'$tweets', '$likes', '$plusones')");
    }
Mutuma
  • 1,943
  • 3
  • 24
  • 33
  • 2
    The question is sloppy and hard to read. If you want attention, give some effort. – bosnjak Mar 22 '14 at 00:18
  • You are probably looking for `list(...) = each(...)` statement. – barell Mar 22 '14 at 00:19
  • There's something called [extract()](http://es1.php.net/extract), but you should only used with data you trust (you created it), not with data fetched from outside. – Francisco Presencia Mar 22 '14 at 00:19
  • @barell I just got into php programming so could you please give an example of how to use the functions you have listed? – Mutuma Mar 22 '14 at 00:23
  • @FranciscoPresencia The data is from a JSON feed so I guess that function would not suffice. I have however noted it down – Mutuma Mar 22 '14 at 00:25
  • Looping through an array is something extremely basic - a simple Google search will give numerous examples – ChrisW Mar 22 '14 at 00:25
  • And, is there any reason you've added the `multidimensional array` tag? The example you've given is only 1D – ChrisW Mar 22 '14 at 00:27
  • 1
    I changed the tags since they seemed too wrong. No SQL nor multidimensional tags here, but instead JSON. Please feel free to revert. – Francisco Presencia Mar 22 '14 at 00:29
  • @ChrisW Please check my last edit to see how I had looped through the array. The multidimensional array was a noobie mistake since I'm a ruby on rails developer and we call that kind of array a hash – Mutuma Mar 22 '14 at 00:32
  • @FranciscoPresencia. I've just added the sql but thanks for the heads up – Mutuma Mar 22 '14 at 00:33
  • *PLEASE* don't use `mysql_*` functions in new code - they are now deprecated – ChrisW Mar 22 '14 at 00:35

1 Answers1

0

Now we're talking. The first question didn't show any effort from your part. However, I need to tell you that this is PHP arrays 101. You can simply do:

$tweets = $social_shares['tweets'];
$likes = $social_shares['likes'];
$plusones = $social_shares['plusones'];

However, you cannot use SQL like that. You are vulnerable to SQL injection. Please read over this thread to see possible solutions:

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • 1
    It worked. Reading through the SQL INJECTION and I have to say this is very new but exremely insightful especially the answer part where he/she used PDO – Mutuma Mar 22 '14 at 00:45