-5

Example: When adding square brackets to $main_array, I get 3 dots inside array but when I remove those brackets, it works good.

    <?php
$user = array('a','b','c');
$cars=array("Volvo","BMW","Toyota");
$roll = array(1,2,3);

$length = count($user);

$main_array = array();

foreach($user as $name){
    $list = array();
    for($i=0;$i<$length;$i++){
        $list[] = array(
            'label' => $cars[$i],
            'roll' => $roll[$i] 
        );
    }

    $main_array[] = array(
        'name'=> $name,
        'list'=> $list
    );

}
var_dump($main_array);
?>

Added.... So here is what I am trying to do. Trying to pull feed from twitter api 1.1

$status = array();
$tweetfeed = array();
foreach($users as $user){
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$user."&count=".$notweets);
    $t=0;
    foreach($tweets as $key) {  
        $status[$t]['text'] = $key ->text;
        $status[$t]['stamp'] = twitter_time($key -> created_at);
        ++$t;
    }   
    $tweetfeed = array (
        'status' => $status         
    );  
var_dump($tweetfeed);
}
var_dump($tweetfeed);

..End

   foreach($user)
    Main_parent {
         Child-1{
                 'key': value
                 'key': value
         }

         Child-2{
                 'key': value
                 'key': value
         }
         Child-3{
                 'key': value
                 'key': value
         }

    }
var_dump(Main_parent);
    }
var_dump(Main_parent);

Calling Main_parent with in foreach loop is working fine but when called outside foreach loop is only showing child-3.

sf_user
  • 168
  • 1
  • 2
  • 9

1 Answers1

0

You are overwriting the value of $tweetfeed in every itteration.

 $tweetfeed = array (
        'status' => $status         
    );  

Should be:

 $tweetfeed[] = array (
        'status' => $status         
    );  

By using [] you are pushing the value into the array, rather than overwriting it. You could actually simplify the whole thing to:

$tweetfeed = array();
foreach($users as $user){
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$user."&count=".$notweets);
    $status = array();
    foreach($tweets as $key) {  
        $status[]=array(
                        'text' => $key ->text, 
                        'stamp' => twitter_time($key -> created_at)
                       );
    }   
    $tweetfeed[] = array (
        'status' => $status         
    );  
var_dump($tweetfeed);
}
var_dump($tweetfeed);
Steve
  • 20,703
  • 5
  • 41
  • 67
  • As said, If I add square bracket to tweetfeed, I am getting the array structure out as expected but within the status array, there is no content, only 3 dots... If I remove the square brackets, only last element of tweetfeed is shown but the status array has the content in it. – sf_user Sep 08 '14 at 13:11
  • @pmvsdt What do you mean three dots?? Whether you push the data into an array, or overwrite a variable, it wont change the data in $status. Can you make a live example, for example here: http://codepad.viper-7.com/ so i can see what you mean – Steve Sep 08 '14 at 13:15
  • Edited my question and added example. Please check. Thanks – sf_user Sep 08 '14 at 14:18
  • @pmvsdt Still not seeing a problem, your example works just fine: http://codepad.viper-7.com/iuLycB# – Steve Sep 08 '14 at 14:21
  • For some reason, I dont get any data in the main_array with squarebrackets. Tried different browsers with cache cleared. Can you please copy that code to php file and view it in browser. Thanks – sf_user Sep 08 '14 at 14:26
  • @pmvsdt I really dont understand what you mean, the codepad link should show you there is no problem, but here is a screengrab from chrome, running the exact code from your example on a local server: http://i.imgur.com/uf33mjm.png – Steve Sep 08 '14 at 14:45
  • and here is a screenshot of what i am getting... unable to understand how to resole this... tried in different system too http://imgur.com/TIFPCjR – sf_user Sep 08 '14 at 14:59
  • @pmvsdt Thats just your browser truncating the data. Maybe its a plugin, but the important thing is its just the display, the code and data is fine. If you want to see the raw data without your browser interfering, just right click and select view source – Steve Sep 08 '14 at 15:08
  • If I remove square brackets to main_array, then the entire is displaying properly within foreach loop. But outside foreach, only the last element of main_array is displayed – sf_user Sep 08 '14 at 15:09
  • @pmvsdt Why are we going round in circles here? This has nothing to do with the code. Its just your browser. Did you try 'view page source' – Steve Sep 08 '14 at 15:13
  • @pmvsdt Or are you using xdebug? That also truncates data: http://stackoverflow.com/questions/5127842/xdebug-hiding-dump-information But either way, its just how the data is displayed to you, the actual data is fine – Steve Sep 08 '14 at 15:15
  • Here is a screenshot of pagesource http://imgur.com/LAq5Skv – sf_user Sep 08 '14 at 15:20
  • Alright, atlast, found a solution.. The problem is with localhost. I pushed the file to production and it worked.. But is there any reason why this code will not work on localhost.. – sf_user Sep 08 '14 at 15:45
  • Yup, it's x debug on localhost. See the link above – Steve Sep 08 '14 at 16:00