IrfanClemson
IrfanClemson

Reputation: 1779

php array loop to get array

I have a $date array like this:

[1]=> array(11) {
    ["meetingname"]=> string(33) "win2008connectcurrent0423131"
    [0]=> array(3) {
        ["scoid"]=> string(7) "3557012"
        ["datecreated"]=> string(19) "2013-05-23 10:02:39"
        ["numparticipants"]=> string(1) "3"
    }
    [1]=> array(3) {
        ["scoid"]=> string(7) "3557012"
        ["datecreated"]=> string(19) "2013-05-23 14:40:06"
        ["numparticipants"]=> string(1) "3"
    }
}   

foreach($date[0] as $key =>  $meetings){
    print "$key = $meetings\n";////yields scoid = 3557012
}

And, as you can see above, I am looping over individual elements. The first element (not indexed) is always meetingname; the rest of the elements are indexed and themselves contain arrays with three elements in each array--in the above code they are [0] and [1].

What I need to do is make the $meetings as an array containing [0] and then [1] etc, depending on the number of elements. So essentially, the output for print should be an array (I can also use var_dump) with key/values of [0] but right not it only outputs individual keys and their values, for example, as you can see above, scoid=3557012. I will need, something all keys/values in the $meetings variable, something like:

{
    ["scoid"]=> string(7) "3557012"
    ["datecreated"]=> string(19) "2013-05-23 10:02:39"
    ["numparticipants"]=> string(1) "3"
}

How can I fix the foreach loop for that?

Upvotes: 0

Views: 119

Answers (2)

Hugo Delsing
Hugo Delsing

Reputation: 14173

You can just create a new array and add the meetings to that one

<?php
$meetings = array();
foreach($date[1] as $key=>$meeting) {
  if (!is_int($key))
    continue;  //only handle numeric keys, incase you ever change the name of the first key 'meetingname'

  $meetings[] = $meeting
}

var_dump($meetings);
?>

Upvotes: 1

Hasina Ansari
Hasina Ansari

Reputation: 845

please Try this. hope it help.

foreach($date as $key =>  $meetings){
       if($key == "meetingname")
         continue;
       else
         print "$meetings\n";
 }

Upvotes: 4

Related Questions