Skip to content

Commit

Permalink
Fix arrays concept (#723)
Browse files Browse the repository at this point in the history
Closes #661

---------

Co-authored-by: homersimpsons <[email protected]>
  • Loading branch information
mk-mxp and homersimpsons authored May 25, 2024
1 parent e12ff58 commit 86775f6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
17 changes: 9 additions & 8 deletions concepts/arrays/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ $no_keys == $integer_keys // => equal returns true
$no_keys === $integer_keys // => strictly equal returns true
```

A value can be of `mixed` type, and each value in an array is not required to be of the same type.
An array can store values of all types.
Each value can have a different type.

```php
$my_arr = [
Expand All @@ -27,7 +28,7 @@ $my_arr = [

## Using Arrays

Arrays can be declared as a literal (written in code, as done above) or created and manipulated as functions.
Arrays can be declared as a literal (written in code, as done above) or created and manipulated by functions.

```php
$my_empty_arr = [];
Expand All @@ -36,17 +37,17 @@ $my_empty_arr = [];
$my_empty_arr = array(); // => []

// or
$letters = explode("Hello", ""); // => ["H", "e", "l", "l", "o"]
$letters = mb_str_split("Hello"); // => ["H", "e", "l", "l", "o"]
```

Access, assign, append values using the index operator:

```php
$prime_numbers = [1, 3, 5, 7, 11, 12];
$prime_numbers = [2, 3, 5, 6];

$prime_numbers[5] = 13; // replace 12 with 13
$prime_numbers[3] = 7; // replace 6 with 7

$prime_numbers[] = 17; // array now contains [1, 3, 5, 7, 11, 13, 17]
$prime_numbers[] = 11; // array now contains [2, 3, 5, 7, 11]
```

Iterate over an array using foreach:
Expand All @@ -55,12 +56,12 @@ Iterate over an array using foreach:
$names = ["Ani", "Jack", "Rami"]

// Iterate just the values
foreach($names as $name) {
foreach ($names as $name) {
echo $name;
}

// Iterate keys and values
foreach($names as $index => $name) {
foreach ($names as $index => $name) {
echo "$index => $name";
}
```
9 changes: 5 additions & 4 deletions concepts/arrays/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ $no_keys == $integer_keys // => equal returns true
$no_keys === $integer_keys // => strictly equal returns true
```

All values are of `mixed` type, and each value in an array is not required to be of the same type.
An array can store values of all types.
Each value can have a different type.

## Using Arrays

Arrays can be declared as a literal (written in code, as done above) or created and manipulated as functions.
Access, assign, append values using the index operator:

```php
$prime_numbers = [1, 3, 5, 7, 11, 12];
$prime_numbers = [2, 3, 5, 6];

$prime_numbers[5] = 13; // replace 12 with 13
$prime_numbers[3] = 7; // replace 6 with 7

$prime_numbers[] = 17; // array now contains [1, 3, 5, 7, 11, 13, 17]
$prime_numbers[] = 11; // array now contains [2, 3, 5, 7, 11]
```
4 changes: 2 additions & 2 deletions concepts/arrays/links.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "PHP: Array Functions"
},
{
"url": "https://www.php.net/manual/en/function.explode",
"description": "PHP: String Functions: explode"
"url": "https://www.php.net/manual/en/function.mb-str-split.php",
"description": "PHP: Multibyte String Functions: mb_str_split"
}
]
9 changes: 5 additions & 4 deletions exercises/concept/language-list/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ $no_keys == $integer_keys // => equal returns true
$no_keys === $integer_keys // => strictly equal returns true
```

All values are of `mixed` type, and each value in an array is not required to be of the same type.
An array can store values of all types.
Each value can have a different type.

### Using Arrays

Arrays can be declared as a literal (written in code, as done above) or created and manipulated as functions.
Access, assign, append values using the index operator:

```php
$prime_numbers = [1, 3, 5, 7, 11, 12];
$prime_numbers = [2, 3, 5, 6];

$prime_numbers[5] = 13; // replace 12 with 13
$prime_numbers[3] = 7; // replace 6 with 7

$prime_numbers[] = 17; // array now contains [1, 3, 5, 7, 11, 13, 17]
$prime_numbers[] = 11; // array now contains [2, 3, 5, 7, 11]
```

## Variable-Length Arguments
Expand Down

0 comments on commit 86775f6

Please sign in to comment.