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?

  • Why don't you include the arrays you want inside your question? print_r is okey, but still everybody will be wasting time converting it into a real array. – tftd Jun 5 '13 at 13:46
  • I think I have the desired array as code block at the nearly very end of my Question--that's how the $meetings should look like. – IrfanClemson Jun 5 '13 at 13:50
up vote 4 down vote accepted

please Try this. hope it help.

foreach($date as $key =>  $meetings){
       if($key == "meetingname")
         continue;
       else
         print "$meetings\n";
 }
  • Yes! This will work. Thanks! – IrfanClemson Jun 5 '13 at 14:00

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);
?>
  • Hi, $date[1] is not an element--it must be $date[0]. Anyway, @Hasina's answer is working. Thanks for your help! – IrfanClemson Jun 5 '13 at 14:01

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.