0

In PHP I have this structure of Array (some are empty, some not, some are multiple items):

        Array
    (
        [0] => Array
            (
            )
    
        [1] => Array
            (
                [0] => 16534
            )
    
        [2] => Array
            (
            )
    
        [3] => Array
            (
                [0] => 16532
                [1] => 16533
            )
    
        [4] => Array
            (
            )
    
        [5] => Array
            (
                [0] => 14869
            )
}

I want to loop through this array, so as the result I get only the numbers (all of them).

I tried it this way:

   foreach ($myarray as $item) {
            echo '<pre>' . print_r($item) . '</pre>';
              //   $result[] = $this->myMethod($item);
            }

So in foreach I want to use all the items from array in my method.

However when I echo the $item in the loop, I have something like this:

Array ( ) 

Array ( [0] => 16534 ) 

Array ( ) 

Array ( [0] => 16532 [1] => 16533 ) 

Array ( ) 

Array ( [0] => 14869 ) 

So still arrays (also the empty ones), and not numbers.

Can you please help with this?

UPDATE: I just noticed, some of the arrays looks like this:

[6] => Array
        (
            [0] => Array
                (
                    [id] => 269
                    [hours] => 21.0
                )

        )

    [7] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [hours] => 12.0
                        )

                    [1] => Array
                        (
                            [id] => 7
                            [hours] => 24.0
                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [hours] => 5.0
                        )

                    [1] => Array
                        (
                            [id] => 7
                            [hours] => 0.583
                        )

                )

        )

but here the solution works not.

This one seems working but it is now brutal foreach in foreach solution:

    foreach ($myarray as $item2) {
    foreach ($item2 as $key2 => $val2) {
        if (isset($val2)) {
            foreach ($val2 as $key4 => $val4) {
                echo $val4['id'].',';

            }
        }
    }
}
1
  • This late-changed question does not clarify the exact desired output. Answers perform differently. This page is Unclear / fragmented and is not a good fit for Stack Overflow. Commented Oct 4, 2022 at 7:31

2 Answers 2

2

Just merge the array which will also remove the empties:

foreach(array_merge(...$myarray) as $item) {
    // echo '<pre>' . print_r($item) . '</pre>';
    $result[] = $this->myMethod($item);
}

This will also work and may be faster:

$result = array_map([$this, 'myMethod'], array_merge(...$myarray));

If you have an old PHP version you'll have to use array() instead of [] and:

call_user_func_array('array_merge', $myarray)
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately I noticed, one of the arrays has different structure - see my additional update in my question. No solutions from here are working on that.
So you're not going to get code that works on any type of array. You either need the arrays to always come in the same format or ask a new question with the different formats and what you need from them.
yeah, I didn't know there will be different arrays when I asked. Now I have a solution - updated question, but 3 times foreach seems not very good
I would look at how these arrays are being created, DB query or whatever. That's where you can fix it.
those are from external source, I can't change them
1

You are only looping through the main array.That's the reason why you are getting an array when you are printing the result set.(because those values are stored in sub arrays and you are not looping them.)

And to remove the sub arrays with empty values I'll use isset() so that you will get only the values.

Change your code into this.

  foreach ($myarray as $item) {
        foreach($item as $key=>$val){
            if(isset($val){
                 $values[] = $val; 
                  //   $result[] = $this->myMethod($values);
            }
        }
     }

1 Comment

I saw you updated your code, this way it is working well! Thank you

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.