Javi
Javi

Reputation: 190

Basic associative arrays in php. How to fill them

First of all, I'm sorry if this is too easy for many of you... I'm learning as much as I can.

I want to create an array with customers, like:

$customers=["customer A", "customer B", "customer C"];

Then, I want to create an array with some characteristics for every customer in that array. The characteristics are 'City', 'Points' and 'Results'. Each customer has a name (string), not a number.

I would need to change (or update) a characteristic ('points') of all customers of the array when needed with PHP. Something like:

for each $customer in $customers {
  $points[$customer]=$points[$customer]+2;
}

I would like to also update the info of only one customer, something like:

$points['Customer C']=$points['Customer B']+3;

Finally, I need to access the characteristics of a give Customer, like:

$i=$points['Customer A']+$result['Customer A'];

I know this is not correct, but how should I proceed in PHP?

How could I eliminate a Customer from the array $Customers (with all its characteristics)? And how could I add a new Customer to $Customers?

Thank you very much for your help. I really appreciate it.

Upvotes: 0

Views: 81

Answers (4)

Steve
Steve

Reputation: 1963

You can use multidimensional arrays to hold the data. This will be like how data would be returned from a database request so is a good approach to use.

// Customers array
$customers = [
   [
      'name'    => 'Customer A',
      'city'    => 'Townville',
      'points'  => '3',
      'results' => '2',
   ],
   [
      'name'    => 'Customer B',
      'city'    => 'Blagstonberry',
      'points'  => '1',
      'results' => '4',
   ],
   [
      'name'    => 'Customer C',
      'city'    => 'Thorpington',
      'points'  => '6',
      'results' => '3',
   ],
];

To access or edit specific customer details you will need to get the key for that customer. For example, get the key for Customer A,

$key = array_search('Customer A', array_column($customers, 'name'));

Now to access that customer's city for example, you can use the code,

$customer_a_city = $customers[$key]['city'];

To remove Customer B,

// get the key
$key = array_search('Customer B', array_column($customers, 'name'));

// remove customer
unset($customers[$key]);

Add 2 points to all customers,

foreach ($customers as &$customer) {
    $customer['points'] += 2;
}
unset($customer);

Here we are passing by reference using the &. This means we can update the value directly in the foreach loop. It is good practice to unset the variable, in this case $customer so that you don't make any unwanted changes later on.


Characteristics of a particular customer - Customer C,

// get the key
$key = array_search('Customer C', array_column($customers, 'name'));

$i = $customers[$key]['points'] + $customers[$key]['results'];

Add a customer,

$customers[] = [
   'name'    => 'Customer D',
   'city'    => 'Dongleville',
   'points'  => '7',
   'results' => '1',
 ];

Notes

If you search for a key for a customer by name that doesn't exist $key will be false. For example,

// get the key
$key = array_search('MADE UP NAME', array_column($customers, 'name'));

if ($key === false) {
   // customer name did not exist
} else {
   // do your thing
}

Also if more than one customer has the same name, the first customer key will be returned.


Reference

Passing by reference

Array search

Array column

Upvotes: 1

Kwido
Kwido

Reputation: 1382

As your question states, assoc array:

$customers = [
    'customerA' => [
        'points' => 100,
        'city' => 'New York',
        'results' => 40,
    ],
    'customerB' => [
        'points' => 75,
        'city' => 'Amsterdam',
        'results' => 10,
    ],
    'customerC' => [
        'points' => 25,
        'city' => 'London',
        'results' => 5,
    ],
];

// Remove customer C
unset($customers['customerC']);

// add customer D
$customers['customerD'] = [
    'points' => 50,
    'city' => 'Berlin',
    'results' => 5,
];

// Update points of customer
$customers['customerA']['points'] += 2; // Adds 2 points
// Update points of customer by another customers points
$customers['customerB']['points'] = $customers['customerA']['points'] + 3;

// Add 2 points to each customer
foreach ($customers as $name => $properties) {
    $properties['points'] += 2;
    $customers[$name] = $properties; 
}

The key within the customers array is the name of your customer and all the properties are within the value of that key.

Upvotes: 1

Shujaat Haider
Shujaat Haider

Reputation: 124

$customers = array();

//Adding customers 
$customers["Customer 1"]["City"]    = "Houston";
$customers["Customer 1"]["points"]  = 3;
$customers["Customer 1"]["Results"] = "";

$customers["Customer 2"]["City"]    = "Paris";
$customers["Customer 2"]["points"]  = 8;
$customers["Customer 2"]["Results"] = "";

//updating characteristics 
foreach ($customers as $name=>$customer) 
{
    $customers[$name]["City"]  = "Dallas";
    $customers[$name]["points"] = $customers[$name]["points"] + 2;
    $customers[$name]["Results"] = "";
}


//Removing customer
unset($customers["Customer 1"]);

Upvotes: 1

Peter
Peter

Reputation: 9143

You were close with your foreach:

foreach ($customers as $customer) {
    $points[$customer] = $points[$customer] + 2;
}

You can update a single value like this:

$points['customer A'] = $points['customer A'] + 2;

And you can unset/remove a value like this:

unset($points['customer A']);

Upvotes: 1

Related Questions