I use this code to get data from API in form of Array:
$GetCurrentStock = $MyApi->GetCurrentStock('Array');
print_r($GetCurrentStock);
I am getting data in the form of Array like this:
Array ( [Status] => Ok [ReturnVal] => Array ( [0] => Array ( [name] => Alcatel [model] => Hero OT-8020X [color] => Black [warehouse] => HU11 [bar_code] => A20200125 [in_stock] => <20 [exp_delivery] => 0 [exp_available] => <20 [delivery_date] => - [price] => 301.90 [properties] => Array ( [eu_warranty] => no [keypad] => Touch screen [manual] => Hun [simlock] => Sim Free [remarks] => Data cable, headset [language] => ger, eng, esp, fra, ita, hun, ned, por, rom, tur [country] => China ) [ean] => [image] => http://www.mobileshop.bz/phone-pictures/api/3531-alcatel-hero-ot-8020x.jpg [id] => 3531 [category] => mobile ) [1] => Array ( [name] => Alcatel [model] => Idol 2 Mini OT-6016X [color] => Gray [warehouse] => HU11 [bar_code] => A20200121 [in_stock] => <5 [exp_delivery] => 0 [exp_available] => <5 [delivery_date] => - [price] => 192.60 [properties] => Array ( [eu_warranty] => no [keypad] => Touch screen [manual] => Hun [simlock] => Sim Free [remarks] => Data cable, headset [language] => cat, ger, eng, esp, fra, ita, hun, ned, por, rom, tur [country] => China ) [ean] => [image] => http://www.mobileshop.bz/phone-pictures/api/3345-alcatel-idol-2-mini-ot-6016x.jpg [id] => 3345 [category] => mobile ) [2] => Array ( [name] => Alcatel [model] => Idol 2 Mini OT-6016X [color] => White [warehouse] => HU11 [bar_code] => A20200120 [in_stock] => <5 [exp_delivery] => 0 [exp_available] => <5 [delivery_date] => - [price] => 192.60 [properties] => Array ( [eu_warranty] => no [keypad] => Touch screen [manual] => Hun [simlock] => Sim Free [remarks] => Data cable, headset [language] => cat, ger, eng, esp, fra, ita, hun, ned, por, rom, tur [country] => China ) [ean] => [image] => http://www.mobileshop.bz/phone-pictures/api/3346-alcatel-idol-2-mini-ot-6016x.jpg [id] => 3346
I have to add each product from the data programmatically to my database.
A single product is added like this:
$productData = array(
'product_description' => array('1' => array('name' => 'Alcatel', 'meta_description' => '' ,'meta_keyword' =>'', 'description' => '', 'tag' =>'')),
'model' => 'Alcatel Hero OT-8020X Black',
'price' => '301.40',
'tax_class_id' => 0,
'quantity' => 1,
'minimum' => 1,
'subtract' => 1,
'stock_status_id' => 6,
'shipping' => 1 ,
'image' => 'http://www.mobileshop.bz/phone-pictures/api/3531-alcatel-hero-ot-8020x.jpg',
'manufacturer' => 'Alcatel',
'manufacturer_id' => 44,
'category' => 'ce',
'product_category' => array('0' => 61),
'product_store' => array('0' => 0),
'date_available' => '2015-03-31',
'length_class_id' => 1,
'weight_class_id' => 1,
'status' => 1,
'sort_order' => 1,
);
//load model
$this->load->model('catalog/product');
// Attempt to pass the assoc array to the add Product method
$this->model_catalog_product->addProduct($productData);
How do I add all those product in bulk programmatically using foreach?
You can create a foreach loop for all the records you have got.
$this->load->model('catalog/product');
foreach($GetCurrentStock['ReturnVal'] as $value){
$productData = array(
'model' => $value['model'],
'price' => $value['price'],
);
// add other values in above array
$this->model_catalog_product->addProduct($productData);
}
Related
I'm trying to merge and calc only by choosen key. For example I have the following array:
$array = array();
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 2];
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 1];
$array['protein'][] = ['id' => 16, 'name' => 'soy yogurt', 'type' => 'protein', 'measurement' => '', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 1];
$array['lipid'][] = ['id' => 19, 'name' => 'coconut powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 20, 'name' => 'chocolate powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 38, 'name' => 'chocolate square', 'type' => 'lipid', 'measurement' => '', 'amount' => 1];
Final output should look like this :
Array (
[0] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
[1] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[2] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
[3] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[4] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[5] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
I tried the following :
$courses = [];
foreach ($array as $key => $item) {
if (!key_exists($key, $courses)) {
$courses[$key] = [];
}
foreach ($item as $row) {
$id = $row['id'];
if (!key_exists($id, $courses[$key])) {
$courses[$key][$id] = $row;
continue;
}
$r = &$courses[$key][$id];
$r['amount'] += $row['amount'];
}
}
Output:
Array (
[Fruit] => Array (
[17] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
)
[protein] => Array (
[16] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[18] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
)
[lipid] => Array (
[19] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[20] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[38] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
)
You were not far off, but everything you are interested in ins inside the inner foreach, that way you dont get the Fruit and Protien etc involved
$courses = [];
foreach( $array as $sub){
foreach ($sub as $item) {
if ( array_key_exists($item['id'], $courses) ) {
// dup, so just add to the amount
$courses[$item['id']]['amount'] += $item['amount'];
} else {
//New entry
$courses[$item['id']] = $item;
}
}
}
// if its important to have indexes starting at 0 for the result
$courses = array_values($courses);
I'm trying to develop an online shop and when I try to list all my basket products, if I do
$basket = array(
'6512bd43d9caa6e02c990b0a82652dca' =>
array(
'id' => '11',
'quantity' => '51',
'price' => '0.28',
'stock' => '50',
'name' => 'Tomato',
'description' => 'u (140 gr aprox.) (1,99 â¬/Kg.)',
'discount' => '0',
'img' => '10.png',
'unique_id' => '6512bd43d9caa6e02c990b0a82652dca',
'total' => 14.28
),
'72b32a1f754ba1c09b3695e0cb6cde7f' =>
array(
'id' => '57',
'quantity' => '15',
'price' => '2.70',
'stock' => '15',
'name' => 'fish ',
'description' => 'tuna 500 gr',
'discount' => '0',
'img' => '57.png',
'unique_id' => '72b32a1f754ba1c09b3695e0cb6cde7f',
'total' => 40.5
)
);
if($basket)
{
echo "Basket:" . '<br/>';
print_r($basket);
echo '<br/>';
echo "Products:" . '<br/>';
foreach($basket as $product)
{
print($product);
echo '<br/';
}
}
This is the returned
Basket:
Array
(
[6512bd43d9caa6e02c990b0a82652dca] => Array
(
[id] => 11
[quantity] => 51
[price] => 0.28
[stock] => 50
[name] => Tomato
[description] => u (140 gr aprox.) (1,99 â¬/Kg.)
[discount] => 0
[img] => 10.png
[unique_id] => 6512bd43d9caa6e02c990b0a82652dca
[total] => 14.28
)
[72b32a1f754ba1c09b3695e0cb6cde7f] => Array
(
[id] => 57
[quantity] => 15
[price] => 2.70
[stock] => 15
[name] => fish
[description] => tuna 500 gr
[discount] => 0
[img] => 57.png
[unique_id] => 72b32a1f754ba1c09b3695e0cb6cde7f
[total] => 40.5
)
)
Productos:
Array ( [id] => 11 [quantity] => 51 [price] => 0.28 [stock] => 50 [name] => Tomato [description] => u (140 gr aprox.) (1,99 â¬/Kg.) [discount] => 0 [img] => 10.png [unique_id] => 6512bd43d9caa6e02c990b0a82652dca [total] => 14.28 )
57 [quantity] => 15 [price] => 2.70 [stock] => 15 [name] => fish [description] => tuna 500 gr [discount] => 0 [img] => 57.png [unique_id] => 72b32a1f754ba1c09b3695e0cb6cde7f [total] => 40.5 )
But, if I do
if($basket)
{
echo "Basket:" . '<br/>';
print_r($basket);
echo '<br/>';
echo "Products:" . '<br/>';
foreach($basket as $product)
{
print($product["name");
echo '<br/';
}
}
The returned is:
Basket:
Array ( [6512bd43d9caa6e02c990b0a82652dca] => Array ( [id] => 11 [quantity] => 51 [price] => 0.28 [stock] => 50 [name] => Tomato [description] => u (140 gr aprox.) (1,99 â¬/Kg.) [discount] => 0 [img] => 10.png [unique_id] => 6512bd43d9caa6e02c990b0a82652dca [total] => 14.28 ) [72b32a1f754ba1c09b3695e0cb6cde7f] => Array ( [id] => 57 [quantity] => 15 [price] => 2.70 [stock] => 15 [name] => fish [description] => tuna 500 gr [discount] => 0 [img] => 57.png [unique_id] => 72b32a1f754ba1c09b3695e0cb6cde7f [total] => 40.5 ) )
Products:
Tomato
I don't understand why it only shows the first item of the basket. What can I do to fix it?
Thanks!
You're already doing it right, except to close the bracket after <br/.
It should be <br/>
You are doing correct. But when you miss the close the <br/ tag then data rendered but not visible on your end.
Products:10
I am trying to filter an array which will produce a drill down list.
This array is built from JSON.
My array looks like this
Array
(
[0] => Array
(
[make] => somemake
[model] => somemodel
[variant] => somevariant
[fuel] => somefuel
[vehicle] => somedetailedinfo
)
[1] => Array
(
[make] => somemake
[model] => somemodel1
[variant] => somevariant1
[fuel] => somefuel1
[vehicle] => somedetailedinfo
)
[2] => Array
(
[make] => somemake
[model] => somemodel2
[variant] => somevariant2
[fuel] => somefuel
[vehicle] => somedetailedinfo
)
[3] => Array
(
[make] => somemake1
[model] => somemodel3
[variant] => somevariant3
[fuel] => somefuel1
[vehicle] => somedetailedinfo
)
)
I would like to filter this array by the make $make, which is set by the $_SERVER['QUERY_STRING'].
The new array should contain all items with the make $make.
How do I do this using array_filter?
This is fairly straight-forward. Note that null coalesce (??) is >= 7.0.
$make = $_GET['make'] ?? 'Chevy';
$vehicles = [[
'make' => 'Ford',
'model' => 'F-150',
'variant' => '4x4',
'fuel' => 'diesel',
'vehicle' => [range(1,3)],
],[
'make' => 'Ford',
'model' => 'Escort',
'variant' => 'XS',
'fuel' => 'gas',
'vehicle' => [range(1,3)],
],[
'make' => 'Chevy',
'model' => 'Cobalt',
'variant' => 'ES',
'fuel' => 'electric',
'vehicle' => [range(1,3)],
],[
'make' => 'Ford',
'model' => 'Explorer',
'variant' => '2x4',
'fuel' => 'gas',
'vehicle' => [range(1,3)],
],[
'make' => 'Mini',
'model' => 'Cooper',
'variant' => 'eTurbo',
'fuel' => 'electro-diesel',
'vehicle' => [range(1,3)],
],];
print_r(array_filter($vehicles, function($vehicle) use($make) {
return $vehicle['make'] === $make;
}));
Gives:
Array
(
[2] => Array
(
[make] => Chevy
[model] => Cobalt
[variant] => ES
[fuel] => electric
[vehicle] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
)
https://3v4l.org/B8T6v
I have an array of StdClass Objects and want to return the "partner_code" with the minimum value for key "price". So for this example I would like to return partner_code "AC" as it is the partner with the lowest price. I tried using array_reduce, but I'm not sure what I'm doing wrong. Any help would be appreciated. Please note I am not looking to SORT this array. I just want to move the subarray containing AC (because it hold the lowest price) to the top - not sorting everything by price
Input Array:
Array
(
[0] => stdClass Object
(
[name] => Budget
[partner_code] => BU
[price] => 365.36
[tier] => 1
)
[1] => stdClass Object
(
[name] => Avis
[partner_code] => AV
[price] => 449.71
[tier] => 1
)
[2] => stdClass Object
(
[name] => E-Z
[partner_code] => EZ
[price] => 270.56
[tier] => 2
)
[3] => stdClass Object
(
[name] => Sixt
[partner_code] => SX
[price] => 280.52
[tier] => 2
)
[4] => stdClass Object
(
[name] => Alamo
[partner_code] => AL
[price] => 345.13
[tier] => 2
)
[5] => stdClass Object
(
[name] => Advantage
[partner_code] => AD
[price] => 357.61
[tier] => 2
)
[6] => stdClass Object
(
[name] => Enterprise
[partner_code] => ET
[price] => 364.46
[tier] => 2
)
[7] => stdClass Object
(
[name] => ACE
[partner_code] => AC
[price] => 186.53
[tier] => 3
)
[8] => stdClass Object
(
[name] => Fox
[partner_code] => FX
[price] => 265.25
[tier] => 3
)
[9] => stdClass Object
(
[name] => Payless
[partner_code] => ZA
[price] => 380.47
[tier] => 3
)
[10] => stdClass Object
(
[name] => Dollar
[partner_code] => ZR
[price] => 385.99
[tier] => 3
)
[11] => stdClass Object
(
[name] => Thrifty
[partner_code] => ZT
[price] => 385.99
[tier] => 3
)
[12] => stdClass Object
(
[name] => Silvercar
[partner_code] => SC
[price] => 424.10
[tier] => 3
)
[13] => stdClass Object
(
[name] => National
[partner_code] => NA
[price] => 448.82
[tier] => 3
)
[14] => stdClass Object
(
[name] => Hertz
[partner_code] => HZ
[price] => 487.33
[tier] => 3
)
)
Code:
array_reduce($this->results->companies, function($a,$b) {
echo "Prices: " . $a->price . "<br>";
return $a->price < $b->price ? (string)$a->partner_code : (string)$b->partner_code;
});
usort($array, function($a, $b) {
return ($a->price - $b->price) ;
});
echo $array[0]->partner_code;
If you really don't want to sort the array but instead use array_reduce, do so by reducing the array to one of its elements. Then you can get the partner_code property for that element.
For example
array_reduce($this->results->companies, function($lowest, $company) {
// $lowest will be null on the first iteration
return $lowest === null || $company->price < $lowest->price ?
$company : $lowest;
})->partner_code;
This method will not sort the entire array. It will pull out the subarray with the lowest price and set it as the first element. I could have nested some of the functions to make it more compact (1-liner), but that would have damaged readability.
Code: (Demo)
$results=[
(object)["name"=>"Budget","partner_code"=>"BU","price"=>"365.36","tier"=>"1"],
(object)["name"=>"Avis","partner_code"=>"AV","price"=>"449.71","tier"=>"1"],
(object)["name"=>"E-Z","partner_code"=>"EZ","price"=>"270.56","tier"=>"2"],
(object)["name"=>"Sixt","partner_code"=>"SX","price"=>"280.52","tier"=>"2"],
(object)["name"=>"Alamo","partner_code"=>"AL","price"=>"345.13","tier"=>"2"],
(object)["name"=>"Advantage","partner_code"=>"AD","price"=>"357.61","tier"=>"2"],
(object)["name"=>"Enterprise","partner_code"=>"ET","price"=>"364.46","tier"=>"2"],
(object)["name"=>"ACE","partner_code"=>"AC","price"=>"186.53","tier"=>"3"],
(object)["name"=>"Fox","partner_code"=>"FX","price"=>"265.25","tier"=>"3"],
(object)["name"=>"Payless","partner_code"=>"ZA","price"=>"380.47","tier"=>"3"],
(object)["name"=>"Dollar","partner_code"=>"ZR","price"=>"385.99","tier"=>"3"],
(object)["name"=>"Thrifty","partner_code"=>"ZT","price"=>"385.99","tier"=>"3"],
(object)["name"=>"Silvercar","partner_code"=>"SC","price"=>"424.10","tier"=>"3"],
(object)["name"=>"National","partner_code"=>"NA","price"=>"448.82","tier"=>"3"],
(object)["name"=>"Hertz","partner_code"=>"HZ","price"=>"487.33","tier"=>"3"]
];
$min=min(array_column($results,'price')); // find the minimum value
$index=array_search($min,array_column($results,'price')); // find the index of the subarray containing min value
$pulled=array_splice($results,$index,1); // extract the subarray and preserve it as $pulled
$new_results=array_merge($pulled,$results); // set $pulled as first element
var_export($new_results);
...okay, okay, here's the one-liner:
var_export(array_merge(array_splice($results,array_search(min(array_column($results,'price')),array_column($results,'price')),1),$results));
Output:
array (
0 =>
stdClass::__set_state(array(
'name' => 'ACE',
'partner_code' => 'AC',
'price' => '186.53',
'tier' => '3',
)),
1 =>
stdClass::__set_state(array(
'name' => 'Budget',
'partner_code' => 'BU',
'price' => '365.36',
'tier' => '1',
)),
2 =>
stdClass::__set_state(array(
'name' => 'Avis',
'partner_code' => 'AV',
'price' => '449.71',
'tier' => '1',
)),
3 =>
stdClass::__set_state(array(
'name' => 'E-Z',
'partner_code' => 'EZ',
'price' => '270.56',
'tier' => '2',
)),
4 =>
stdClass::__set_state(array(
'name' => 'Sixt',
'partner_code' => 'SX',
'price' => '280.52',
'tier' => '2',
)),
5 =>
stdClass::__set_state(array(
'name' => 'Alamo',
'partner_code' => 'AL',
'price' => '345.13',
'tier' => '2',
)),
6 =>
stdClass::__set_state(array(
'name' => 'Advantage',
'partner_code' => 'AD',
'price' => '357.61',
'tier' => '2',
)),
7 =>
stdClass::__set_state(array(
'name' => 'Enterprise',
'partner_code' => 'ET',
'price' => '364.46',
'tier' => '2',
)),
8 =>
stdClass::__set_state(array(
'name' => 'Fox',
'partner_code' => 'FX',
'price' => '265.25',
'tier' => '3',
)),
9 =>
stdClass::__set_state(array(
'name' => 'Payless',
'partner_code' => 'ZA',
'price' => '380.47',
'tier' => '3',
)),
10 =>
stdClass::__set_state(array(
'name' => 'Dollar',
'partner_code' => 'ZR',
'price' => '385.99',
'tier' => '3',
)),
11 =>
stdClass::__set_state(array(
'name' => 'Thrifty',
'partner_code' => 'ZT',
'price' => '385.99',
'tier' => '3',
)),
12 =>
stdClass::__set_state(array(
'name' => 'Silvercar',
'partner_code' => 'SC',
'price' => '424.10',
'tier' => '3',
)),
13 =>
stdClass::__set_state(array(
'name' => 'National',
'partner_code' => 'NA',
'price' => '448.82',
'tier' => '3',
)),
14 =>
stdClass::__set_state(array(
'name' => 'Hertz',
'partner_code' => 'HZ',
'price' => '487.33',
'tier' => '3',
)),
)
I think this is a leaner method generating the same/desired result: (Demo)
$min=$results[0]->price; // set initial/default min value
$index=0; // set initial/default index
foreach($results as $i=>$a){
if($a->price<$min){ // if a lower price...
$min=$a->price; // store new $min
$index=$i; // stor new $index
}
}
$results=array_merge($results,array_splice($results,$index,1)); // extract lowest and move to front of array
var_export($results);
I have converted xml string into php array. But the requirement is something different. Following is an array which is created using XML string by following
$xml = simplexml_load_string($mystin, "SimpleXMLElement",
LIBXML_NOCDATA); $json = json_encode($xml); $array =
json_decode($json,TRUE);
Array
(
[0] => Array
(
[data] => Array
(
[0] => Array
(
[attribute_set] => default
[description] => productdescription
[is_in_stock] => 1
[meta_description] => Product meta description
[meta_keyword] => Product meta keyword
[meta_title] => Product meta title
[name] => myproduct6
[price] => 100
[qty] => 1000
[re_skus] => sdfefwef
[short_description] => this is a short descriptio
[sku] => myproduct_surabhi7
[status] => 1
[store] => admin
[tax_class_id] => 4
[type] => simple
[url_key] => my-product
[url_path] => my-product.html
[visibility] => 4
[weight] => 10
)
[1] => Array
(
[attribute_set] => default
[description] => productdescription
[is_in_stock] => 1
[meta_description] => Product meta description
[meta_keyword] => Product meta keyword
[meta_title] => Product meta title
[name] => myproduct6
[price] => 100
[qty] => 1000
[re_skus] => sdfefwef
[short_description] => this is a short descriptio
[sku] => myproduct_surabhi7
[status] => 1
[store] => admin
[tax_class_id] => 4
[type] => simple
[url_key] => my-product
[url_path] => my-product.html
[visibility] => 4
[weight] => 10
)
)
)
)
I need the above array as following array:
Array
(
[0] => Array
(
[0] => Array
(
[name] => Product1
[sku] => Product1
[description] => Product description
[short_description] => Product short description
[weight] => 10
[status] => 1
[url_key] => wat12
[url_path] => wat12.html
[visibility] => 4
[price] => 100
[tax_class_id] => 4
[meta_title] => Product meta title
[meta_keyword] => Product meta keyword
[meta_description] => Product meta description
[store] => admin
[attribute_set] => default
[type] => simple
[is_in_stock] => 1
[color] => Orange
[re_skus] => Testsimple2,Testsimple1
)
)
[1] => Array
(
[1] => Array
(
[name] => Product2
[sku] => Product2
[description] => Product description
[short_description] => Product short description
[weight] => 10
[status] => 1
[url_key] => wat12
[url_path] => wat12.html
[visibility] => 4
[price] => 100
[tax_class_id] => 4
[meta_title] => Product meta title
[meta_keyword] => Product meta keyword
[meta_description] => Product meta description
[store] => admin
[attribute_set] => default
[type] => simple
[is_in_stock] => 1
[color] => Orange
[re_skus] => Testsimple2,Testsimple1
)
)
)
Try this may be it help
$array = array(
0 => array(
'data' => array(0 => array('abc2' => 1, 'adsg' =>2),1=>array('abc' => 1, 'adsg4' =>2)))
);
$new_data = array();
foreach ($array as $row)
{
for($i=0; $i<count($row['data']); $i++){
$newArray = array();
$newArray[$i] = $row['data'][$i];
$new_data[] = $newArray;
}
}
print_r($new_data);
Have a look at this example:
$array = array(
98 => array(
'City' => 'Caracas',
'Country' => 'Venezuela',
'Continent' => 'Latin America',
),
99 => array(
'City' => 'Cairo',
'Country' => 'Egypt',
'Continent' => 'Middle East',
),
105 => array(
'City' => 'Abu Dhabi',
'Country' => 'United Arab Emirates',
'Continent' => 'Middle East',
),
106 => array(
'City' => 'Dubai',
'Country' => 'United Arab Emirates',
'Continent' => 'Middle East',
),
107 => array(
'City' => 'Montreal',
'Country' => 'Canada',
'Continent' => 'North America',
)
);
$newArray = array();
foreach ($array as $row)
{
$newArray[$row['Continent']][$row['Country']][] = $row['City'];
}
print_r($newArray);
I got idea from Keval's answer. After doing a small change, it is working like charm.
$new_data = array();
foreach ($array as $row)
{
for($i=0; $i<count($row); $i++){
$newArray = array();
$newArray[$i] = $row[$i];
$new_data[] = $newArray;
}
}
print_r($new_data);