0

I have an array in my php code


$list = array(
'RETAIL' => 'SUPERMARKET'
'RETAIL' => 'BAR'
'RETAIL' => 'DEP. MARKET'
'BUSINESS' => 'HOTEL'
'BUSINESS' => 'PUB'
'OTHER' => 'GROCERY'
'OTHER' => 'BUTCHERY'
// I have 20+ items
);

foreach( $list as $type => $name ){
  var_dump($type,$name);
}

//var_dump() output 
// RETAIL SUPERMARKET
// BUSINESS HOTEL
// OTHER BUTCHERY

I'm facing the problem that when I try to loop the array only three values will be returned and the rest are ignored. How I can fix this? I'm trying to loop the array to save the data into a custom wordpress database. With the same way I've successfully looped another array inserted the keys and values into the db.

5
  • 4
    PHP arrays cannot have multiple entries with the same key, but you could have each key as an array containing multiple types. ['retail' => ['bar', 'shop']] Commented Oct 14, 2022 at 12:59
  • Can you add more details where is this $list array being retrieved from? Commented Oct 14, 2022 at 13:00
  • @Dale ok. If you provide an answer with example I will accept it. Thanks for the help Commented Oct 14, 2022 at 13:03
  • @newbiedev can you add the code you use to extract the data from the excel file? Commented Oct 14, 2022 at 13:05
  • 1
    What @Dale means is that this array cannot in fact exist. One key will overwrite the other. Commented Oct 14, 2022 at 13:12

3 Answers 3

1

I think a better structure for your array would something like this

$list = [
    'RETAIL' => [
        'BAR',
        'RESTAURANT'
    ]
];

And you could loop over like so

foreach ($list as $businessType => $businesses) {
    foreach ($businesses as $business) {
         echo "<li>{$business}</li>";
    }
}

Just an example

Sign up to request clarification or add additional context in comments.

2 Comments

I'm testing the solution and seems working. Just a question about the keys, can I have for example RETAIL used as a value to insert in my db query?
If I understand you correctly, after the first foreach in the above code you have the variable $businessType which is that first key RETAIL
1

As a general way of handling instances where you have more than one element to each piece of data (even when it's not in a tree structure like this may be), you should structure each item in the list as either an array or object, eg:

$list_of_arrays = [
    ['RETAIL', 'SUPERMARKET'],
    ['RETAIL', 'BAR'],
    ['RETAIL', 'DEP. MARKET'],
];

foreach( $list_of_arrays as $array ){
    echo "<li>{$array[0]} {$array[1]}</li>";
}

or

$list_of_objects = [
    (object)['type' => 'RETAIL', 'subtype' => 'SUPERMARKET'],
    (object)['type' => 'RETAIL', 'subtype' => 'BAR'],
    (object)['type' => 'RETAIL', 'subtype' => 'DEP. MARKET'],
];

foreach( $list_of_objects as $object ){
    echo "<li>{$object->type} {$object->subtype}</li>";
}

Comments

1

Maybe I show heavily but as you only have two data for each entity, why your table is not built like this at the base...?

$list = array(
'SUPERMARKET'=>'RETAIL',
'BAR'=>'RETAIL',
'DEP. MARKET'=>'RETAIL',
'HOTEL'=>'BUSINESS',
'PUB'=>'BUSINESS',
'GROCERY'=>'OTHER',
'BUTCHERY'=>'OTHER'
// I have 20+ items
);

You do have keys that uniquely identify entities.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.