0

I have the following output when printing an array called yestardayArray using print_r:

Array
(
    [project-id] => Array
        (
            [373482] => Array
                (
                    [responsible-ids] => Array
                        (
                            [129938] => Array
                                (
                                    [0] => Array
                                        (
                                            [task-id] => 1812
                                            [content] => HU-003 - FRONT
                                            [progress] => 100
                                            [completed_On] => 2020-10-23
                                            [created_On] => 2020-10-14
                                            [responsible-ids] => 129938
                                            [responsible-name] => Malo M.
                                        )

                                )

                        )

                )

        )

)

I need a way to get the "129938" value as an string from the responsible-ids Array. I'm iterating througth project-id ($pid), in the case of this example "373482", and using different keys. Let's call a variable needString:

$needString = $yesterdayArray["project-id"][$pid]["responsible-ids"][$key];

When I print needString I get "Array" instead of "129938".

How can I get that array key in a string form?

9
  • Can the responsible-ids array have multiple elements? Commented Oct 25, 2020 at 5:08
  • Yes, in fact they do, at least most of the times Commented Oct 25, 2020 at 5:10
  • But isn't $key already that key? Just echo key - or a turn it into a string by adding a blank string to the end $needString = $key. "" Commented Oct 25, 2020 at 5:16
  • @jameson who taught you to needlessly add . "" to string declarations? I see this strange technique too often on SO. Commented Oct 25, 2020 at 6:17
  • I'm not sure really @mickmackusa it's not something I really do myself as I rarely have a use case for explicitly making an int a string (if I concat an int value inside a string it takes care of itself). If OP explicitly needs it as a string it's just a quick way to do it. What would you recommend Commented Oct 25, 2020 at 6:21

1 Answer 1

1

If you want to iterate over the keys for each responsible-ids sub-array, you can simply use a nested foreach e.g.

foreach ($yesterdayArray['project-id'] as $pid => $array) {
    foreach ($array['responsible-ids'] as $key => $rid) {
        echo $key . PHP_EOL;
    }
}

If you just want to get a list of the keys of each responsible-ids sub-array, you can just use array_keys:

foreach ($yesterdayArray['project-id'] as $pid => $array) {
    print_r(array_keys($array['responsible-ids']));
}

Output (for your sample data):

129938

Array
(
    [0] => 129938
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.