0

I have an array like this:

    Array (
    [0] => Array ( 
        [0] => 510 
        [1] => 984 
        [2] => 1045 
        [3] => 2068 
        [4] => 1054 
        [5] => 673 
        ) 
    [1] => Array ( 
        [0] => 1163 
        [1] => 1982 
        [2] => 2067 
        [3] => 3989 
        [4] => 1940 
        [5] => 1242 
        ) 
    [2] => Array ( 
        [0] => june 
        [1] => july 
        [2] => august 
        [3] => september 
        [4] => october 
        [5] => november 
        ) 
    )

I want to access only one of the arrays within the array at a time and echo them out.

For example, I would get: 510, 984, 1045, 2068, 1054, and 673 as one result.

I've looked at multiple threads and answers but nothing that quite solves my issue, I've been able to print out all the values but I just want some specifically.

I'd store it in the variable: $array_item

My most recent attempt was:

foreach ($array_item as $inner) {
if (is_array($inner)) {
    foreach ($inner[0] as $value) {
        echo "$value \n";
    }
}
}

Which gives me: Warning: Invalid argument supplied for foreach().

I thought for sure that would work, what am I doing wrong?

1
  • If I want 510, it's $array_item[0][0]. If I want 984, it's $array_item[0][1]. If I want 1163, it's $array_item[1][0]. If I want september, it's $array_item[2][3], etc. Commented Nov 18, 2017 at 0:44

4 Answers 4

3

In your code $inner[0] is one of the inner array elements, like 510. You can't loop over that.

If you just want to loop over $array_item[0], don't loop over the whole array.

if (is_array($array_item[0])) {
    foreach ($array_item[0] as $value) {
        echo "$value\n";
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

The problem with that is that I don't have the values I want returned to me, I only want what is specifically under [0] and not the entire thing.
Then why did you do foreach ($array_item as $inner)? I've updated the answer.
I got stuck on the idea because of how I assembled the array I think... since I was storying arrays inside the array... like: $array_item = array( $another_array) and so on... Thanks for clearing it up, seems so stupid simple now.
You don't have to do this foreach either, just use implode as shown below. echo implode(PHP_EOL, $array_item[0]); would work
0

Does is_array($array_item) return true?

if (is_array($array_item)) {
    foreach ($array_item) {
        if (is_array($inner)) {
            foreach ($inner as $value) {
                echo "$value \n";
            }
        }    
    }
}

Edit: Also, $inner[0] is not an array. $inner is the array.

2 Comments

is_array($array_item) is returning true, yes.
Ah, do not use $inner[0]. Inner[0] is a value, not an array. Use foreach($inner as $value) instead.
0

I would use implode, http://php.net/manual/en/function.implode.php

foreach($array_items as $items) {
    echo implode(', ', $items);
}

I see that a comment was made that he wants only the first array to be outputted and not all of them.

if (is_array($array_item[0])) {
    echo implode(PHP_EOL, $array_item[0]);
}

Comments

0

You never can tell what could solve a problem

$array_group = array();

if (isset($array_item[0]) and is_array($array_item[0])) {
foreach ($array_item[0] as $value) {
    $array_group[] = $value;
}

$array_group should output

Array
(
    [0] => 510
    [1] => 984
    [2] => 1045
    [3] => 2068
    [4] => 1054
    [5] => 673
)

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.