1

I've got an array which points to a bunch of other arrays, structured like a JSON object. This is the structure of it:

$allRows= array(
    "Global task 1"=> array(
        "major-task"=> array(
            "points"=> 850,
        ),
        "minor-task"=> array(
            array(
                "points"=> 400,
                "task"=> "Minor task 1",
            ),
            array(
                "points"=> 200,
                "task"=> "Minor task 2",
            ),
            array(
                "points"=> 250,
                "task"=> "Minor task 3",
            ),
        )
    ),
    "Global task 2"=> array(
        "major-task"=> array(
            "points"=> 850,
        ),
        "minor-task"=> array(
            array(
                "points"=> 400,
                "task"=> "Minor task 1",
            ),
            array(
                "points"=> 200,
                "task"=> "Minor task 2",
            ),
            array(
                "points"=> 250,
                "task"=> "Minor task 3",
            ),
        )
    ),
);

I'm trying to be able to access each of these in the following way:

foreach ($allRows as $row) {
    // Print Global task 1, Global task 2, etc
    echo $row[0]; // unsure how to acquire this 'Global task x' text

    // I've already accessed this just fine
    echo $row["major-task"]["points"];
}

I've got the latter echo just fine, but I'm unable to print the Global task 1 and Global task 2. Any help printing this text would be highly appreciated!

1
  • get both the keys and values on your foreach loop like so foreach ($allRows as $taskName => $row){ // result here ! } Commented Apr 29, 2017 at 22:29

5 Answers 5

1

The tasks are the keys for $allRows, you'll need to catch them in the foreach:

foreach ($allRows as $taskName => $row) {
    echo $taskName; // "Global task 1"

    echo $row["major-task"]["points"];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I originally tried this but kept trying to echo $row; instead of the taskName. Will accept this when it lets me!
1

Use it like this:

foreach ($allRows as $key => $row) {
    // Print Global task 1, Global task 2, etc
    echo $key;

    // I've already accessed this just fine
    echo $row["major-task"]["points"];
}

http://php.net/manual/en/control-structures.foreach.php

Comments

1

What you are trying to do is get the key for the multidimensional array

To do this, define the a $key variable with the foreach() loop

Example:

foreach ($allRows as $key => $value) {
    echo $key; // Global task 1 or Global task 2
    echo $value["major-task"]["points"];
}

Also, does not matter, but when using strings in arrays, I like to use ''. Example:

echo $value['major-task']['points'];

Comments

0

I will suggest you to use recursive function to parse multidimensional array and get values, here are reference links to understand better:

http://php.net/manual/en/function.array-walk-recursive.php

Iterate multidimensional Array recursively and return same array structure and inserting new key/values in PHP

And here is solution with your current implementation:

foreach ($allRows as $key=>$row) {
    // Print Global task 1, Global task 2, etc
    //echo $row[0]; // unsure how to acquire this 'Global task x' text
    echo $key;
    // I've already accessed this just fine
    echo $row["major-task"]["points"];
}

Comments

0

This is an associative array, if you want to access it with indexes you need to get the array of its keys array_keys($row) first:

 foreach ($allRows as $row) {
    $keys = array_keys($row);
    print_r($row[$keys[0]]);
}

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.