An open API service indexing awesome lists of open source software.

https://github.com/osamaayub/php-practice-tasks

practice-tasks that covers all basics of php and how to work with json data in php
https://github.com/osamaayub/php-practice-tasks

arrays-and-strings conditonals functions json-data loops php8 variables

Last synced: 6 months ago
JSON representation

practice-tasks that covers all basics of php and how to work with json data in php

Awesome Lists containing this project

README

          

# Core PHP Practice Task List

## πŸ“Œ Introduction

This task list is designed to strengthen your understanding of **core PHP** concepts, including variables, loops, arrays, functions, and JSON handling. By working through these tasks, you will build a strong foundation in PHP programming, preparing you for more advanced topics such as **database integration** and **front-end development**.

⚠ **Important:** Avoid copying codeβ€”focus on understanding the logic and principles behind each solution.

---

## πŸ—‚ Sample Data (JSON Format)

The following JSON dataset represents a list of users and their purchases in an online store:

```json
{
"users": [
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com",
"purchases": [
{ "item": "Laptop", "price": 999.99 },
{ "item": "Mouse", "price": 19.99 },
{ "item": "Keyboard", "price": 49.99 }
]
},
{
"id": 2,
"name": "Jane Smith",
"email": "jane.smith@example.com",
"purchases": [
{ "item": "Phone", "price": 799.99 },
{ "item": "Charger", "price": 29.99 }
]
},
{
"id": 3,
"name": "Bob Johnson",
"email": "bob.johnson@example.com",
"purchases": []
}
]
}
```

---

## πŸ“ Task List

### βœ… Task 1: Parse and Display User Data
- **Function:** `getUserData($jsonData)`
- **Expected Output:**
```
Name: John Doe, Email: john.doe@example.com
Name: Jane Smith, Email: jane.smith@example.com
Name: Bob Johnson, Email: bob.johnson@example.com
```

### βœ… Task 2: Calculate Total Number of Purchases
- **Function:** `countPurchases($jsonData)`
- **Expected Output:**
```
Total Purchases: 5
```

### βœ… Task 3: Calculate Total Amount Spent by a User
- **Function:** `getUserTotalSpent($jsonData, $userId)`
- **Expected Output for User 1:**
```
Total Spent by User 1: 1069.97
```

### βœ… Task 4: Find the User with the Highest Spending
- **Function:** `findTopSpender($jsonData)`
- **Expected Output:**
```
Top Spender: John Doe with Amount: 1069.97
```

### βœ… Task 5: List Users with No Purchases
- **Function:** `findUsersWithNoPurchases($jsonData)`
- **Expected Output:**
```
Users with no purchases: Bob Johnson
```

### βœ… Task 6: Sort Users by Total Spending
- **Function:** `sortUsersBySpending($jsonData)`
- **Expected Output:**
```
John Doe spent 1069.97
Jane Smith spent 829.98
Bob Johnson spent 0
```

### βœ… Task 7: Add New User and Purchase
- **Function:** `addUserAndPurchase($jsonData, $userName, $email, $item, $price)`
- **Example Usage:**
```php
$newJsonData = addUserAndPurchase($jsonData, "Alice Brown", "alice.brown@example.com", "Headphones", 149.99);
echo $newJsonData;
```

- **Expected JSON Output (Partial):**
```json
{
"id": 4,
"name": "Alice Brown",
"email": "alice.brown@example.com",
"purchases": [
{ "item": "Headphones", "price": 149.99 }
]
}
```

---

## πŸ† Conclusion
By completing these tasks, you will:
βœ… Strengthen your understanding of **PHP fundamentals**
βœ… Learn how to work with **JSON data structures**
βœ… Improve your **problem-solving skills** in real-world scenarios

Mastering these will **prepare you for database integration (MySQL) and full-stack development**! Keep practicing, and **happy coding! πŸš€**

---

## πŸ‘¨β€πŸ’» Author
**Osama Ayub**