Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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 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. – Meengla Jun 5 at 13:50

2 Answers

up vote 3 down vote accepted

please Try this. hope it help.

foreach($date as $key =>  $meetings){
       if($key == "meetingname")
         continue;
       else
         print "$meetings\n";
 }
share|improve this answer
Yes! This will work. Thanks! – Meengla Jun 5 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);
?>
share|improve this answer
Hi, $date[1] is not an element--it must be $date[0]. Anyway, @Hasina's answer is working. Thanks for your help! – Meengla Jun 5 at 14:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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