0

I have an array with different arrays in them with values. I want to loop over these arrays inside to get all the values but for some reason It only goes over the first array.

Here's what the array looks like: This array's name is slots.

Array
(
    [41] => Array
        (
            [0] => Array
                (
                    [attractie] => attractie1
                    [start] => 0930
                    [end] => 1200
                    [personen] => 
                    [catering] => 1
                    [bedrijfsnaam] => attractie1
                    [link] => http:
                    [color] => dd0330
                )

            [1] => Array
                (
                    [attractie] => attractie1
                    [start] => 1000
                    [end] => 1230
                    [personen] => 
                    [catering] => 1
                    [bedrijfsnaam] => Bedrijf2
                    [link] => http:
                    [color] => e49fca
                )
        )

    [52] => Array
        (
            [0] => Array
                (
                    [attractie] => attractie2
                    [start] => 0930
                    [end] => 1030
                    [personen] => 
                    [catering] => 1
                    [bedrijfsnaam] => Bedrijf4
                    [link] => http:
                    [color] => f7e300
                )

            [1] => Array
                (
                    [attractie] => attractie2
                    [start] => 0930
                    [end] => 1030
                    [personen] => 
                    [catering] => 0
                    [bedrijfsnaam] => bedrijf5
                    [link] => http:
                    [color] => f78f1e
                )

        )

)

So this is what my loop looks like:

$i=0;
foreach($slots[$attractieIDs[$i]] as $s){

        $myOrders[] = array( 'attractie' => $s['attractie'],
                             'name' => $s['bedrijfsnaam'],
                             'start' => $s['start'],
                             'end' => $s['end'],
                             'link' => $s['link'], 
                             'personen' => $s['personen'],
                             'catering' => $s['catering'],
                             'color' => $s['color'],
                           ); 
         $i++;
}

attractieID is an array with the id's in them the (41 and 52).

When I print out $myOrders I only get to see the values of the array with id 41 it doesn't go to the next array with a new id.

Anyone knows how I can fix this?

Many thanks in advance!

10
  • What is your desired output? Commented Apr 20, 2016 at 7:27
  • I would like to output the values of the array with id 52 as well Commented Apr 20, 2016 at 7:27
  • But how it should look exactly Commented Apr 20, 2016 at 7:31
  • Just all the values(name, attractie, etc.) of every item in all the arrays in my big $slots array Commented Apr 20, 2016 at 7:34
  • 1
    Man why don't you put desired array structure in your question.? Commented Apr 20, 2016 at 7:35

4 Answers 4

1

Your current code will merge entries from 41 and 52 into one array and you will not be able to tell which one was which.

$sourceArray = .... your source array here :)
$attractieIDs = array(41, 52);

foreach($attractieIDs as $id) {
   foreach($sourceArray[$id] as $attr) {
     $myOrders[] = $attr;
   }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I get an Illegal offset type and Invalid argument supplied for foreach() error
Forgot one level. Try now.
@MarcinOrlowski, I retract my previous comment, however there is something wrong: you should only need 2 loops. Now you will only get values, no keys, since $s is not an array.
This seems wrong (unless the OP has been very unclear in expressing), because this does not give you the keys "attractie", "bedrijfsnaam", "start".... Only their values are returned in one big array. I put your code here.
Also don't it will restrict because of $attractieIDs = array(41, 52);? what happen if lot of other indexes will come? how much indexes will be hardcoded?
|
1

You could use this:

foreach ($attractieIDs as $id) {
    foreach ($slots[$id] as $s) {
        $myOrders[] = $s;
    }
}

See it run on eval.in

Comments

0

Try this loop:

foreach($slots as $outer_arr){

    foreach($outer_arr as $s) {
        $myOrders[] = array( 'attractie' => $s['attractie'],
              'name' => $s['bedrijfsnaam'],
              'start' => $s['start'],
              'end' => $s['end'],
              'link' => $s['link'], 
              'personen' => $s['personen'],
              'catering' => $s['catering'],
              'color' => $s['color'],
        ); 

    }


}

But still not clear with your desired output.

3 Comments

What should $outer_arr be?
The $outer_arr will contain the array with index 41 and 52 and one more foreach loop for iterate over the array.
If you can add array variable NOT array output I can test it.
0

Try this one. This loop will output any value inside your array regardless on what index you have.

<?php
$test = array(
    '41' => array( 
        array('attractie' => 'attractie1',
             'start' => '0930',
             'end' => '1200',
             'personen' => NULL,
             'catering' => '1',
             'bedrijfsnaam' => 'attractie1',
             'link' => 'http:',
             'color' =>'dd0330'),
        array('attractie' => 'attractie1',
             'start' => '1000',
             'end' => '1230',
             'personen' => NULL,
             'catering' => '1',
             'bedrijfsnaam' => 'Bedrijf2',
             'link' => 'http:',
             'color' =>'e49fca'),
            ),
    '51' => array( 
        array('attractie' => 'attractie2',
             'start' => '0930',
             'end' => '1030',
             'personen' => NULL,
             'catering' => '1',
             'bedrijfsnaam' => 'Bedrijf4',
             'link' => 'http:',
             'color' =>'f7e300'),
        array('attractie' => 'attractie2',
             'start' => '0930',
             'end' => '1030',
             'personen' => NULL,
             'catering' => '0',
             'bedrijfsnaam' => 'bedrijf5',
             'link' => 'http:',
             'color' =>'f78f1e'),
            )
    );

foreach ($test as $a => $val) {
    echo "<b>Index $a</b><br><br>";
    foreach ($val as $b) {
        foreach ($b as $key => $value) {
            echo "<b>$key</b> - $value<br>";
        }
    }
    echo "<br><br>";
}
?>

Output:

Index 41

attractie - attractie1
start - 0930
end - 1200
personen - 
catering - 1
bedrijfsnaam - attractie1
link - http:
color - dd0330
attractie - attractie1
start - 1000
end - 1230
personen - 
catering - 1
bedrijfsnaam - Bedrijf2
link - http:
color - e49fca


Index 51

attractie - attractie2
start - 0930
end - 1030
personen - 
catering - 1
bedrijfsnaam - Bedrijf4
link - http:
color - f7e300
attractie - attractie2
start - 0930
end - 1030
personen - 
catering - 0
bedrijfsnaam - bedrijf5
link - http:
color - f78f1e

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.