Dirk Harriman Banner Image

 

PHP Notes - Arrays


Simple ID Array

The purpose of this is to create the next id. The array created has some ids that have been deleted.

$id_ar = array(); $id_ar[] = 1; $id_ar[] = 3; $id_ar[] = 5; $id_ar[] = 6; $id_ar[] = 7; $id_ar[] = 9; $id_count = 0; $largest_id = 0; echo "Length of \$id_ar is ". count($id_ar) ."
 
"; foreach($id_ar as $id_val ) { $id_count++; echo "<br/>Item: ". $id_count ." - ID: ". $id_val; if ($id_val > $largest_id) { $largest_id = $id_val; } } $largest_id++; echo "<br/>New ID: ". $largest_id;

Length of $id_ar is 6
 

Item: 1 - ID: 1
Item: 2 - ID: 3
Item: 3 - ID: 5
Item: 4 - ID: 6
Item: 5 - ID: 7
Item: 6 - ID: 9
New ID: 10
 

 

 

Two Dimensional Array Filter

A two dimensional array of employees of company divisions

$business = array ( "Developer" => array("0" => array ( "name" => "John", "salary" => 90000 ), "1" => array ("name" => "Roger", "salary" => 105000 ), "2" => array ( "name" => "Mike", "salary" => 100000 ), "3" => array ( "name" => "Drew", "salary" => 140000 ), "4" => array ( "name" => "Lance", "salary" => 140000 )), "Manager" => array("0" => array ( "name" => "Stewart", "salary" => 120000 ), "1" => array ( "name" => "Jill", "salary" => 140000 ), "2" => array ( "name" => "Steve", "salary" => 114000 )), "Executive" => array( "0" => array ( "name" => "Karen", "salary" => 500000 ), "1" => array ( "name" => "Bonny", "salary" => 250000 ), "2" => array ( "name" => "Fred", "salary" => 650000 ))); $filterArray = array(); $i = 0; foreach($business as $dept => $employee){ foreach($employee as $index => $data){ if($data['salary'] > 120000){ $filterArray[$i]['deprtment'] = $dept; $filterArray[$i]['name'] = $data['name']; $filterArray[$i]['salary'] = $data['salary']; } $i++; } } echo "Length of \$filterArray is ". count($filterArray); foreach ($filterArray as $i => $ar) { foreach ($ar as $k => $v) { echo $k .": ". $v ."<br/>"; } }

Length of $filterArray is 6
 
department: Developer
name: Drew
salary: 140000
department: Developer
name: Lance
salary: 140000
department: Manager
name: Jill
salary: 140000
department: Executive
name: Karen
salary: 500000
department: Executive
name: Bonny
salary: 250000
department: Executive
name: Fred
salary: 650000


Whole Array
Developer
 0 - name - John
salary - 90000
1 - name - Roger
salary - 105000
2 - name - Mike
salary - 100000
3 - name - Drew
salary - 140000
4 - name - Lance
salary - 140000

 
Manager
 0 - name - Stewart
salary - 120000
1 - name - Jill
salary - 140000
2 - name - Steve
salary - 114000

 
Executive
 0 - name - Karen
salary - 500000
1 - name - Bonny
salary - 250000
2 - name - Fred
salary - 650000

 

 

 

 

Reservation Array

$array1 = array(); for ($i=0; $i<4; $i++){ $array1[] = array('reservationId' => $i, 'startDate' => '2023-12-15', 'endDate' => '2023-12-16', 'partyName' => 'Kramer' ); } $array2 = array('reservations' => $array1); foreach ($array2 as $name => $res_ar) { foreach ($res_ar as $i => $res_item) { echo $i ." - Res ID: ". $res_ar[$i]['reservationId'] ." Party:". $res_ar[$i]['partyName'] ."<br/>"; } }

0 - Res ID: 0 Party:Kramer
1 - Res ID: 1 Party:Kramer
2 - Res ID: 2 Party:Kramer
3 - Res ID: 3 Party:Kramer