0

This is my array :

$arr = [
    
    // fisrt part
    [
        '1' => 'one',
        '2' => 'two',
        '3' => 'three',
        'completedLanguages' => [
            0 => 'cpp',
            1 => 'javascript',
        ],
    ],

    // second part
    [
        '4' => 'four',
        '5' => 'five',
        '6' => 'six',
        'completedLanguages' => [
            0 => 'php',
            1 => 'python',
            2 => 'go',
        ],
    ],

    // third part
    [
        '7' => 'seven',
        '8' => 'eight',
        '9' => 'nine',
        'completedLanguages' => [
            0 => 'csharp',
            1 => 'vb',
            2 => 'rust',
            3 => 'scala',
        ],
    ],
];

In each of these three parts there is an index called completedLanguages .

In completedLanguages index, there may be a number of variable values ​​each time.

I want to go into the parts one by one and print all the values ​​in their completedLanguages index. And finally, if all the values printed, I go to the next part until print the completedLanguages index values ​​of all the parts.

For a better understanding, look at the following loop:

for ($i = 0; $i <= count($arr[0]['completedLanguages']) - 1; $i++) {
    echo $arr[0]['completedLanguages'][$i] . '<br>';
}

The number 0 in $arr[0]['completedLanguages'] can vary depending on the number of parts in this array. For example in this array we have three parts!

  • You want to use a [foreach](https://www.php.net/manual/en/control-structures.foreach.php) loop instead of `for`. See also [How does PHP 'foreach' actually work?](https://stackoverflow.com/questions/10057671/how-does-php-foreach-actually-work) – BadHorsie Sep 29 '20 at 16:43
  • @BadHorsie i want to save the result in a variable with vary indexes . can i do it with foreach ? –  Sep 29 '20 at 16:45

2 Answers2

0

One way, using 2 foreach loops like this

First loop all the outer occurances, then for each outer occurance loop the specific inner occurance completedLanguages

$savedLangs = []
foreach ($arr as $a){
    $langs = '';    // init the string
    foreach ($a['completedLanguages'] as $lang){
        echo $lang;
        // or save the values in another array
        $langs .= $lang . ', ';    // save each lang in a string
    }
    $savedLangs[] = trim($langs, ', ');
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • how can i save values of each completedLanguages in each parts in a different index of array. for example : $savedLang[0] gonna be cpp javascript $savedLang[1] gonna be php python go –  Sep 29 '20 at 16:54
  • There you go, languages save as a comme delimited string from each occurance – RiggsFolly Sep 29 '20 at 16:58
0

You need here two loop first to loop parts and second one to loop on completedLanguages

for ($i = 0; $i <= count($arr); $i++) {
  if(!isset($arr[$i]['completedLanguages'])) continue;
  for ($j = 0; $j <= count($arr[$i]['completedLanguages']); $j++) {
    echo $arr[$i]['completedLanguages'][$j] . '<br>';
  }
}

I prefer to use foreach instead of for

Aian
  • 226
  • 3
  • 10