How to Select a Part of an Array in PHP

1 year ago admin PHP

In this lesson, we will see how to select a part of an array in PHP and return the selected part.


Select a part of an array

So to do that we will use the array_slice function that takes the array, the start of the slice, and the number of elements to return as params.

                                                        
                                                                                                                        
<!DOCTYPE html>
<html>
<body>

<?php
   $names = array("john","jack","maria","lora","carla");
   $values = array_slice($names, 1, 2);
   echo 'Selected values: '.$values[0]. ' , ' . $values[1];
   
   //result
   //Selected values: jack , maria
?>

</body>
</html>


Array with key & value

You can do the same using an array with a key & value.

                                                            
                                                                                                                                
<!DOCTYPE html>
<html>
<body>

<?php
   $names = array("a" => "john","b" => "jack","c" =>"maria","d" =>"lora","e" => "carla");
   $values = array_slice($names, 1, 2);
   echo 'Selected values: '.$values["b"]. ' , ' . $values["c"];
   
   //result
   //Selected values: jack , maria
?>

</body>
</html>

Related Tuorials

How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...


How to Sort Associative Arrays in Ascending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the value...


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...


How to Sort Ascending an Array in PHP

In this lesson, we will see how to sort ascending an array in PHP, we will use the sort() function t...


How to Remove a Key and its Value from an Associative Array in PHP

In this lesson, we will see how to remove a key and its value from an associative array in PHP,&nbsp...


How to Modify a Value in an Associative Array in PHP

In this lesson, we will see how to modify a value in an associative array in PHP, an Associative arr...


How to Add an Item to an Associative Array in PHP

In this lesson, we will see how to add an item to an associative array in PHP, an Associative array...


How to Add an Element to an Array in PHP

In this lesson, we will see how to add an element to an array in PHP, PHP has a built-in function th...