-2

I am having some trouble creating an array in PHP, I am pulling data from a JSON file creating an array with it to post.

I can't quite figure out why this foreach loop will not work. I can't see an apparent syntax error in my code.

Error :

PHP Syntax Check: Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')' in your code on line 4 foreach($w['order']['something'] as $field => $value) {

I have an array created without the foreach loop and it appears to work fine (below).

"Items" => array(
    array(
        "l_ref" => "888",
        "sku" => "888",
        "qty" => "QTY",
        "typ" => "4",
        "job_ref" => "3929"
    ),
    array(
        "l_ref" => "888",
        "sku" => "1323"
    )
)

Here is the code that doesn't work with the foreach loop present. Is there anything obvious I am missing?

$orderData = array (
    "Order" => array(
        "Items" => array(
            foreach($w['order']['something'] as $field => $value) {
                if ($value['something'][1]['name'] == "id") {
                    array(
                        "something" => "something"
                    )
                }
            }
        )
    )
)

I am expecting the code to loop and pull out items based on a certain value.

Error: PHP Syntax Check: Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')' in your code on line 4 foreach($w['order']['something'] as $field => $value) {

treyBake
  • 6,440
  • 6
  • 26
  • 57
Tq2719
  • 7
  • 1
  • This shouldn't be marked as a duplicate – Chris Jun 05 '19 at 09:31
  • @Chris why? I mean the error message is kinda self-explanatory, it's a syntax error due to incorrect variable declaration ... which is handled [here](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them/18092318#18092318) – treyBake Jun 05 '19 at 09:34
  • That infamously old question is pages long which doesn't explicitly answer the question at hand. The question, nor the answer mirrors this question - so it isn't a duplicate. If you were new to PHP or programming, how is the above error message self-explanatory? An `unexpected 'bla'` message can occur for a multitude of reasons – Chris Jun 05 '19 at 09:39
  • @Chris yes it does... read that post. point 3 on [this answer](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them/18092318#18092318) exactly explains the problem ... – treyBake Jun 05 '19 at 09:44
  • My point is that it isn't a duplicate, it's a catch all. What do you think is more useful for a new contributor to the site? – Chris Jun 05 '19 at 09:48
  • 1
    @Chris but it is .. the answer matches this question and in fact, your answer is more or less the exact same, minus the code sample .. so in my eyes, that is a dupe. It doens't need to be 100% exact to be a dupe.. And IMHO, to read the manual – treyBake Jun 05 '19 at 09:50
  • 1
    @Tq2719 I'm sorry you feel that way - I do my best to try to support new contributors. I agree 100% that most questions could be pointed there, but as you said, that does not make it, nor this site, useful as a resource and learning tool – Chris Jun 05 '19 at 09:58
  • 1
    @Chris, I appreciate your help and your efforts to help new users, for me the answer you gave me was helpful and straight to the point. No doubt it didn't take you very long either and overall it is a lot more specific than the 'duplicate'. I can't understand why it is an issue. Thanks again! – Tq2719 Jun 05 '19 at 10:03
  • @Tq2719 I'm not so sure, this dupe usually gets brought up for syntax error-related posts. Not general SO - but, the answer linked is `Most language constructs can only be used as statements. They aren't meant to be placed inside other expressions:` followed by an example of invalid syntax, which mirrors your code. Hence, to me, it is a dupe. At the top is a contents list for you to find your specific syntax error .. – treyBake Jun 05 '19 at 10:07

1 Answers1

0

Long story short, you can't have a statement inside an array constructor like that

It should look something more akin to:

<?php

$orderData = array (
    "Order" => array(
        "Items" => array()
    )
);

foreach($w['order']['something'] as $field => $value) {
    if ($value['something'][1]['name'] == "id") {
        $orderData['Order']['Items'] = array(
            "something" => "something"
        )
    }
}
Chris
  • 54,599
  • 30
  • 149
  • 186
  • Thanks for the help Chris, much appreciated. This has solved my issue – Tq2719 Jun 05 '19 at 09:43
  • No worries at all! – Chris Jun 05 '19 at 09:47
  • Further expanding this it looks that the foreach loop is overwriting the "Items" array when there is more than one item in ['order']['something']. For example, I have multiple ['something'] and I am only getting one object in the "Items" array. Is there a way around this? – Tq2719 Jun 05 '19 at 10:26
  • You should push into the array. You could use `$orderData['Order']['Items'][] = array(...)` . Noting the extra [] at the end – Chris Jun 05 '19 at 10:29
  • Great! Thanks again Chris – Tq2719 Jun 05 '19 at 10:33