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?

NullEins
  • 101
  • 1
  • 13
  • Can the `responsible-ids` array have multiple elements? – Nick Oct 25 '20 at 05:08
  • Yes, in fact they do, at least most of the times – NullEins Oct 25 '20 at 05: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. "" ` – imposterSyndrome Oct 25 '20 at 05:16
  • @jameson who taught you to needlessly add `. ""` to string declarations? I see this strange technique too often on SO. – mickmackusa Oct 25 '20 at 06: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 – imposterSyndrome Oct 25 '20 at 06:21
  • I recommend never using `. ""` when echoing -- it is a total waste of characters ...it does nothing. – mickmackusa Oct 25 '20 at 06:23
  • True, but where does he say he is echoing it? Perhaps he need to convert it to a string to pass to a function with with explicit data types – imposterSyndrome Oct 25 '20 at 06:25
  • So you are inventing the need to type the value as a string? Then cast it with `(string)` if that is necessary-- the code will be more explicit in its intention. – mickmackusa Oct 25 '20 at 06:29
  • All I'm saying is that OP wants it to be a string, it's not really any of my business why they explicitly want a string – imposterSyndrome Oct 25 '20 at 06:32

1 Answers1

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

Nick
  • 138,499
  • 22
  • 57
  • 95