Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[PHP] Associative arrays

Featured Replies

If I have an associative arrays which is like this:

 

([4] => 1, [1] => 2, [3] => 1)

 

I know that the things in the brackets is the key and the number to the right of the "arrow" is the value.

 

How do I / what is the whole thing collectively called i.e. [4] => 1

 

Is it the first 'item'?

 

If I wanted to output this as a table what do I need to do to get this "value" - if the table e.g. was something like this:

 

Id   Product    Qty
1    4          2
2    1          2
3    3          1

Edited by Citrus Cube Design

$x = 0;
foreach($array as $key => $value) {
   $x++;
   echo $x . ' | ' . $key . ' | ' . $value;
}

Yes you can describe it as an item. As per the manual, there are a lot of different uses for it such as lists, stack, queue, collection

So it really depends on what your use is for it, but I guess there's no wrong answer

$x = 0;
foreach($array as $key => $value) {
   $x++;
   echo $x . ' | ' . $key . ' | ' . $value;
}

 

...providing you're not interested in $array[0].

 

Alternatively, you could use:

 

foreach($array as $key => $value) {
   echo $x . ' | ' . $key . ' | ' . $value;
   $x++;
}

 

Correct term would be an array member or a key/value pair.

Edited by Renaissance-Design

That counters nothing to do with the array in my code, i just started it from 1 because his table id column started at 1.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.