I have the following multidimensional array:
Array
(
[0] => 57950340
[1] => SALE-86
[2] => COMPLETE
[3] =>
[4] => 333
[5] => 819
[6] => Array
(
[0] => Array
(
[number] => 1
[product] => Array
(
[id] => 90316
[name] => CLASSIC COCKTAIL
)
[quantity] => 1
[price_variation] => 1
[modifiers] => Array( )
[notes] =>
[unit_price] => 16.3636
[unit_tax] => 1.63636
)
[1] => Array
(
[number] => 2
[product] => Array
(
[id] => 90316
[name] => CLASSIC COCKTAIL
)
[quantity] => 1
[price_variation] => 1
[modifiers] => Array ( )
[notes] =>
[unit_price] => 16.3636
[unit_tax] => 1.63636
)
)
)
I'm trying to loop through the array so that I can echo the name of the product items (held within the array at key 6 and echo each of these out on a separate line with the unit price and an the initial order ID (key 0 of the initial array).
I've been trying to do this for a few hours but am going round in very confusing circles, can anyone shed any light on how I can do this?
<?PHP
$multi_dimensional_array = [...]; // Your array here
$order_id = $multi_dimensional_array[0];
$products_array = $multi_dimensional_array[6];
foreach($products_array as $product) {
echo $product['product']['name']." costs ".$product['unit_price'];
echo " - ORDER: ".$order_id;
echo "<br/>";
}
?>
Useforeach and is_array() to check if the values is array then foreach to access the inside variables then lastly you can echo it.
foreach($array as $key => $val)
{
if(is_array($val){
foreach($val as $key2 => $val2)
{
//print_r($val2); to see the actual data you are accessing
echo "ID: " . $val2['product']['id']. ' Product Name: ' . $val2['product']['name'] . ' Quantity: ' . $val2['quantity'];
}
}
}
Related
My array is like this. So how can I access the names,values with php?
Array(
[0] => Array
(
[name] => subject
[value] => คอมพิวเตอร์ ม.3
)
[1] => Array
(
[name] => subject_code
[value] => ง33101
)
[2] => Array
(
[name] => subject_hour
[value] => 2
)
[3] => Array
(
[name] => semester
[value] => 1
)
[4] => Array
(
[name] => level
[value] => 3
)
[5] => Array
(
[name] => classroom
[value] => 301
)
[6] => Array
(
[name] => classroom
[value] => 302
)
)
I have tried FOREACH to loop through the array and it does the job, but how can I get their names and values to be used later?
My FOREACH code:
foreach($objects AS $values){
foreach($values as $value){
echo $value.'<br/>';
}
}
assume your array is $array:
foreach($array as $item){
$name = $item['name']; //extract name
$value = $item['value']; //extract value
echo $name.' '.$value.'</br>';
}
To get the index/key, just name it:
foreach($collection as $key => $value)
print($key . ' = ' . $value . '\n');
Iteration over $objects to get values and names;
foreach($objects as $value){
echo "Name: ".$value['name'];
echo " - Value: ".$value['value'];
echo "<br/>";
}
I tried to parse a string array using the code below but the required data never printed! could any one tell me how to fix it ?Thanks
$data Array structure :
Array
(
[js] => Array
(
[total_items] => 20
[max_page_items] => 2
[selected_item] => 0
[cur_page] => 0
[data] => Array
(
[0] => Array
(
[tmp] => 1
[name] => mango
[abc] => abcd4 http://mysite/items/1234
[number] => 1123
[itemCategory_title] => fruits
[logo] => 2123.png
[itemCategory_id] => 90
)
[1] => Array
(
[tmp] => 0
[name] => cherry
[abc] => abcd4 http://mysite/items/1235
[number] => 1124
[itemCategory_title] => fruits
[logo] => 2124.png
[itemCategory_id] =>
)
)
)
[text] => no error
)
php code:
<?
$code2 = stripslashes($_POST['outputtext']);
$data = json_decode($code2);
foreach( $data as $item ) {
echo $item['tmp'];
echo $item['name'];
echo $item['abc'];
echo $item['number'];
echo $item['itemCategory_title'];
echo $item['log'];
echo $item['itemCategory_id'];
}
?>
It should be:
foreach ($data['js']['data'] AS $item)
because the array is nested several levels down in $data.
Note that you need to call json_decode($code2, true) to get an associative array like that. By default, it returns an object, not an array, so you would do:
foreach ($data->js->data as $item) {
echo $item->tmp;
echo $item->name;
...
}
I have an array from a form submission:
Array
(
[form_key] => 9juTLit5qQbaBb98
[sku] => Array
(
[0] => AC25
[1] => AC30
[2] => AC31
)
[product] => Array
(
[0] => 95
[1] => 100
[2] => 101
)
[related_product] => Array
(
[0] =>
[1] =>
[2] =>
)
[qty] => Array
(
[0] => 2
[1] => 2
[2] => 2
)
)
Is there a good way to sort it more like?
Array
(
[form_key] => 9juTLit5qQbaBb98
[0] => Array
(
[sku] => AC25
[product] => 95
[qty] => 2
)
[1] => Array
(
[sku] => AC30
[product] => 100
[qty] => 2
)
[2] => Array
(
[sku] => AC31
[product] => 101
[qty] => 2
)
)
Since you indicated that you may be able to change the form, you could do something like this and get the desired array structure:
<input type="text" name="data[0][sku]">
<input type="text" name="data[0][product]">
<input type="text" name="data[0][qty]">
<input type="text" name="data[1][sku]">
<input type="text" name="data[1][product]">
<input type="text" name="data[1][qty]">
The array will be in $_POST['data'] which is handy for looping since it is isolated from form_key, submit etc...
Pick one field to loop over, then grab the values from the other fields:
foreach($data['sku'] as $key=>$sku){
$product = $data['product'][$key];
$related_product = $data['related_product'][$key];
$qty = $data['qty'][$key];
}
Then you can build a new array, or do whatever with the values.
$newArray[] = array(
'sku' => $sku,
'product' => $product,
'related_product' => $related_product,
'qty' => $qty
);
Quite basic example but should work with more complicate data set
$rs = [];
foreach($_POST as $k1=>$elem) {
if(is_array($elem)) {
foreach($elem as $k2=>$value) {
$rs[$k2][$k1] = $value;
}
}
}
$newArray = [];
foreach ($array as $mainKey => $value ) {
foreach($value as $subKey => $subValue) {
$newArray[$subKey][$mainKey] = $subValue;
}
}
print_r($newArray);
I have an array stored in $result like this
$result=$array
by using echo $result i get the following array
Array
(
[success] => 1
[product] => Array
(
[id] => 83
[seo_h1] =>
[name] => Beer Week
[manufacturer] => The Boxer Store
[model] => WPEB/0413/74/BW
[sku] => WPEB/0413/74/BW
[reward] => 0
[points] => 0
[image] => asd
[images] => Array
(
[0] => asd
)
[quantity] => 4
[price] => Rs.599
[special] =>
[discounts] => Array
(
)
[options] => Array
(
[0] => Array
(
[product_option_id] => 42
[option_id] => 25
[name] => Size Option
[type] => select
[option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 165
[option_value_id] => 72
[name] => Large
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 166
[option_value_id] => 73
[name] => XL
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 163
[option_value_id] => 70
[name] => Small
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
[3] => Array
(
[product_option_value_id] => 164
[option_value_id] => 71
[name] => Medium
[option_sku] =>
[image] => asd
[price] =>
[price_prefix] => +
)
)
[required] => 1
)
)
[minimum] => 1
[rating] => 0
[description] => as
[attribute_groups] => Array
(
)
[date_added] => 2014-09-30 12:35:12
[date_modified] => 2014-10-17 17:33:46
[currency] => INR
[status] => 1
)
)
i am able to fetch the result individually like this
$finalid = $array['product']['id'];
echo $finalid;
but wish to fetch each and every data even the inner most values using for loop. for this i tried this
$c=count($result);
for ( $i=0; $i < $c; $i++)
{
echo $array[$i]['id'];
echo $array[$i]['images'][0];
echo $array[$i]['options'][0]['product_option_id'];
echo $array[$i]['images'][0]['option_value'][0]['product_option_value_id'];
echo $array[$i]['images'][0]['option_value'][1]['product_option_value_id'];
}
but it didn't displayed any result can anyone tell how it can be done
Use:
$products = $result['product'];
foreach ( $products as $product )
{
echo $product['id'];
echo $product['images'][0];
echo $product['options'][0]['product_option_id'];
echo $product['images'][0]['option_value'][0]['product_option_value_id'];
echo $product['images'][0]['option_value'][1]['product_option_value_id'];
}
You may want a recursive function like this to print your multidimensional arrays:
function printIt($data){
if (is_array($data)){
foreach ($data as $index=>$slice){
if (is_array($slice)){
printIt($slice);
}else{
echo $index.": ".$slice."<br>";
}
}
}else{
echo $data."<br>";
}
}
printIt($yourArray);
Use foreach instead,
foreach ( $array['product'] as $product)
{
echo $product['id'];
echo $product['images'][0];
echo $product['options'][0]['product_option_id'];
echo $product['options'][0]['option_value'][0]['product_option_value_id'];
echo $product['options'][0]['option_value'][1]['product_option_value_id'];
}
Here you will be looping inside each element of $array['product'] and for each loop $product will have the current element of $array['product'].
The mistake in your attempt was in [$i]. For each loop $i would have 0,1,2 etc and the array index would be $array['product'][0],etc but there is no element of that sort. Your method works if your array is an Indexed array, but here it is an Associative Array.
Try this,
<?php
foreach($result as $res){
echo '<pre>'; echo $res['id'];
if(is_array($res['options']) && !empty($res['options'])) {
foreach($res['options'] as $option) {
echo '<pre>'; echo ($option['product_option_id']);
if(is_array($option['option_value']) && !empty($option['option_value'])) {
foreach($option['option_value'] as $option_value) {
echo '<pre>'; echo $option_value['product_option_value_id'];
}
}
}
}
?>
I have a php array that just like this:
Array
(
[0] => Array
(
[product] => 2
[price] => 30
[qnty] => 1
)
[1] => Array
(
[product] => 2
[price] => 30
[qnty] => 1
)
[2] => Array
(
[product] => 1
[price] => 250
[qnty] => 1
)
)
and I want to combine the duplicate values, add "qnty" index value and print that array like this:
Array
(
[0] => Array
(
[product] => 2
[price] => 30
[qnty] =>2
)
[1] => Array
(
[product] => 1
[price] => 250
[qnty] => 1
)
)
How can i combine this array. Please help me
try the code below. I am assuming the your array name is $products
$merged = array();
foreach($products as $product) {
$key = $product['product'];
if(!array_key_exists($key, $merged)) {
$merged[$key] = $product;
}
else {
$merged[$key]['qnty'] += $product['qnty'];
}
}
echo '<pre>';
print_r($merged);
exit;