PHP Array Part 2 - Array Built in PHP Functions and Some More Examples

PHP Array Part 2 - Array Built in PHP Functions and Some More Examples

In our previous chapter, we’ve seen the array and all of the basic parts of that. Today we’ll learn some built in functions of array in PHP. These functions helps us to access or modify arrays very easily. We’ll show only the necessary and important functions of array in our day-to-day-life, and others could be get from PHPNet’s manual - https://www.php.net/manual/en/ref.array.php

  1. count(),
  2. sizeof,
  3. explode,
  4. implode
  5. array_push(),
  6. array_pop(),
  7. array_shift()
  8. array_keys(),
  9. array_values,
  10. in_array,
  11. unset,
  12. is_array
  13. array_search,
  14. array_replace,
  15. array_reverse,
  16. array_sum,
  17. array_unique,
  18. compact,
  19. extract,
  20. list
  21. sort,
  22. asort,
  23. arsort,
  24. rsort,
  25. array_merge,
  26. array_filter,
  27. array_reduce

count() in array

This functions get the number of items in an array -

$marks = [10, 50, 60];
echo count($marks); // 3

$names = array(
    "Akash",
    "Jhon",
    "Rony",
    "Polash"
);
echo count($names); // 4

sizeof() in array

Same as count(). It’s actually the alias of count(). This functions get the number of items in an array. Preferred is to use count(), rather than sizeof().

$marks = [10, 50, 60];
echo sizeof($marks); // 3

implode() in array

$names = array(
    "Akash",
    "Jhon",
    "Rony",
    "Polash"
);

for ($i = 0; $i < count($names); $i++) {
    echo $names[$i] . "<br>";
}
echo "<br>";

// Join array elements with a string
$names_print = implode(", ", $names);
echo $names_print;

explode() in array

$names_again = explode(", ", "Akash, Jhon, Rony, Polash");
print_r($names_again);

array_push() in array

$good_students = [];
// print_r($good_students);

// array_push($good_students, "Akash");
// print_r($good_students);

// array_push($good_students, "Polash");
// print_r($good_students);

$students = array(
    array(
        "name" => "Akash",
        'age' => 50,
        "result" => 3.5
    ),
    array(
        "name" => "Jhon",
        'age' => 50,
        "result" => 3.0
    ),
    array(
        "name" => "Doe",
        'age' => 50,
        "result" => 3.6
    ),
    array(
        "name" => "Rony",
        'age' => 30,
        "result" => 2.5
    )
);

foreach ($students as $student_data) {
    if ($student_data['result'] >= 3.5) {
        array_push($good_students, $student_data);
    }
}

array_unshift() in array

// array_unshift($good_students, "Jhon Doe");
// print_r($good_students);

print_r($good_students);

array_pop() in array

echo array_pop($names);
print_r($names);

array_shift() in array

array_shift($names);
print_r($names);

array_keys() in array

$student = array(
    "name" => "Akash",
    'age' => 50,
    "result" => 3.5,
    // "mother_name" => "Mina Begum"
);


print_r(array_keys($student));

array_values() in array

print_r(array_values($student));

in_array() in array

$student = array(
    "name" => "Akash",
    'age' => 50,
    "result" => 3.5,
    "mother_name" => "Mina Begum"
);


echo "<h3>in_array() function</h3>";
if (in_array('Akash2', $student)) {
    echo "Hi, It's exist";
}

key_exists() in array

if (key_exists('mother_name', $student)) {
    echo 'Mother Name: ' . $student['mother_name'];
}

unset() in array

print_r($student);

unset($student['mother_name']);
print_r($student);

is_array() in array


$student_name = "Akash";
// echo $student_name['name'];

if (is_array($student_name)) {
    echo $student_name['name'];
}

$student_name = [
    'age' => 28
];

## Example is_array() with key_exists()

if (is_array($student_name) && key_exists('name', $student_name)) {
    echo $student_name['name'];
}

array_search() in array

$searched_key = array_search("Akash", $student);
if ($searched_key) {
    echo $student[$searched_key];
}

array_replace() in array

$replace_student_data = ['age' => 40];
$replace_student_data2 = ['age' => 60, 'result' => 4.0];
$replaced_student = array_replace($student, $replace_student_data, $replace_student_data2);
print_r($replaced_student);

array_reverse() in array

$marks = [10, 20, 40];
print_r(array_reverse($marks));

array_sum() in array

$marks = [10, 20, 40];

// We can achieve also by loop
$total_marks = 0;
foreach ($marks as $mark) {
    $total_marks += $mark;
}
echo $total_marks;

// But simpler with array_sum()
echo array_sum($marks);

compact() in array

$name = "Akash";
$age = 28;

$compact_array = compact('name', 'age');
print_r($compact_array);

extract() in array

extract($student);
echo $result;

array_merge() in array

$marks = [50, 10, 20, 40];
// sort($marks);
// print_r($marks);

$marks_of_class_8 = [50, 30, 100];
$marks_of_class_9 = [90];

$total_marks_data = array_merge($marks, $marks_of_class_8, $marks_of_class_9);

print_r($total_marks_data);

print_r($student);

array_filter() in array


$new_students = array_filter($student, function($s_value){
    return floatval($s_value) > 3;
});

print_r($new_students);

array_reduce() in array

$a = array(1, 2, 3, 4, 5);

function sum($carry, $item)
{
    $carry += $item;
    return $carry;
}

echo array_reduce($a, 'sum');

Get all Reference functions of Array in PHP - https://www.php.net/manual/en/ref.array.php

Previous
PHP Array and all it's Examples
Next
PHP Bonus - Online Judge Problem Solving Using PHP