PHP arrays

Hi all,

I have a multidimensional array in this format:

[php]

$array = array(
“Item set 1” => array(
“Product ID” => array(
“Name” => “a test product”
);
);
“Item set 2” => array(
“Product ID” => array(
“Name” => “a test product”
);
);
“Item set 3” => array(
“Product ID” => array(
“Name” => “a test product”
);
);
“Item set 4” => array(
“Product ID” => array(
“Name” => “a test product”
);
);
);

[/php]

Is there a way I can make it so the item set arrays merge into one big array of products? That way I can do:

[php]

foreach($array as $id => $product) {
echo "Product ID: " . $id . ’ ’ . "Product name: " . $product[‘name’];
}

[/php]

Hope this makes sense :slight_smile:

Thanks,
Adam

First, you need to fix the array. Then you need a nested foreach.

I agree with KR and just something I was goofing around with that might help you out ->

[php]<?php
$records = [
0 => [
“id” => 12345,
“name” => ‘Widgets 01’,
“price” => 19.99,
“quantity” => 22
],
1 => [
“id” => 12346,
“name” => ‘Widgets 02’,
“price” => 100.00,
“quantity” => 5
],
3 => [
“id” => 12347,
“name” => ‘Widgets 03’,
“price” => 2.29,
“quantity” => 35
],
4 => [
“id” => 12348,
“name” => ‘Widgets 04’,
“price” => 1.50,
“quantity” => 500
]
];

//echo “

” . print_r($records, 1) . “
\n”;
?> Array Data Set <?php foreach ($records as $record) { //echo "
" . print_r($record, 1) . "
\n"; echo '

Product id is ' . $record['id'] . ' Product name is ' . $record['name'] . ' Product Price is $' . number_format($record['price'], 2) . ' Product Qauntity is ' . $record['quantity'] . '

'; } ?> [/php]

Looking back at this, it appears to be an XY problem. Where is this array coming from? Are you pulling the data from a database or somewhere else?

Sponsor our Newsletter | Privacy Policy | Terms of Service