I know probably this was asked before not sure if was in this form but I did tried some replay from what I found here about this and failed.
ok I have this array
Array
(
[0] => Array
(
[Data3] => Array
(
[id] => 5
[category] => Whiskey
[name] => Some name
[description] => description
[image] => asdf.jpg
[price] => 83.99
)
[ammount] => 1
[Data_id] => 3
)
[1] => Array
(
[Data3] => Array
(
[id] => 4
[category] => Tequila
[name] => Something Red 75cl
[description] => description
[image] => sierratequilasilver100.jpg
[price] => 92.49
)
[ammount] => 2
[Data_id] => 3
)
[2] => Array
(
[Data4] => Array
(
[id] => 3
[category] => Whiskey
[name] => Some name Gold
[description] => description
[image] => asdf.jpg
[price] => 83.99
)
[ammount] => 1
[Data_id] => 4
)
[3] => Array
(
[Data4] => Array
(
[id] => 5
[category] => Vodka
[name] => Something Blue 100 cl
[description] => description
[image] => Something.jpg
[price] => 32.44
)
[ammount] => 1
[Data_id] => 4
)
)
What I would like to be the result is something like this:
Array
(
[0] => Array
(
[id] => 5
[category] => Whiskey
[name] => Some name
[description] => description
[image] => asdf.jpg
[price] => 83.99
[ammount] => 1
[Data_id] => 3
)
[1] => Array
(
[id] => 4
[category] => Tequila
[name] => Something Red 75cl
[description] => description
[image] => sierratequilasilver100.jpg
[price] => 92.49
[ammount] => 2
[Data_id] => 3
)
[2] => Array
(
[id] => 3
[category] => Whiskey
[name] => Some name Gold
[description] => description
[image] => asdf.jpg
[price] => 83.99
[ammount] => 1
[Data_id] => 4
)
[3] => Array
(
[id] => 5
[category] => Vodka
[name] => Something Blue 100 cl
[description] => description
[image] => Something.jpg
[price] => 32.44
[ammount] => 1
[Data_id] => 4
)
)
or another way I could work with is if I can change Data1, Data2, Data3 and so on ..
can be n Data depends how many producs a user select
into a same name ex simple Data or Info.
ex:
Array
(
[0] => Array
(
[Info] => Array
(
[id] => 5
[category] => Whiskey
[name] => Some name
[description] => description
[image] => asdf.jpg
[price] => 83.99
)
[ammount] => 1
[Data_id] => 3
)
[1] => Array
(
[Info] => Array
(
[id] => 4
[category] => Tequila
[name] => Something Red 75cl
[description] => description
[image] => sierratequilasilver100.jpg
[price] => 92.49
)
[ammount] => 2
[Data_id] => 3
)
Any solution will be fine for me.
Thanks and regards
Use this code for your result:
$final_array = array();
foreach($array1 as $offset1 => $array2) {
$tmp_array = array();
foreach($array2 as $offset2 => $array3) {
if(is_array($array3)) {
$tmp_array = $array3;
} else {
$tmp_array[$offset2] = $array3
}
}
$final_array = array_merge($final_array, $tmp_array;);
//or
$final_array[] = $tmp_array;
}
I would do this this way. However I would look to fix why the data is in that structure to begin with.
$aStartArray = array(array('Data3'=>array('id'=>1, 'cat'=>2), 'amount' => 1, 'Data_id'=>3));
foreach ($aStartArray as $iPos => $aArray) {
$aKeys = array_keys($aArray); // fetches all the keys
$aFirstElement = $aArray[$aKeys[0]]; // Get the first element using first key
// assign/ overwrite data at the same position
$aStartArray[$iPos] = array($aFirstElement, 'amount' => $aArray['amount'], 'Data_id' => $aArray['Data_id']);
}
echo "<pre>";
var_dump($aStartArray);
your first option:
foreach($arr as $key => $value){
foreach($value as $k => $val){
if(is_array($val)){
$arr[$key] = $val;
unset($arr[$key][$k]);
}
}
}
echo "<pre>"; print_r($arr);
Check output here
This is the best algorithm
const moveArrayToParentArray = (input) => {
let finalOutput = []
input.forEach((e) => {
if (Array.isArray(e)) {
finalOutput = [...finalOutput, ...e];
} else {
finalOutput = [...finalOutput, e];
}
})
return finalOutput
}
const array = ['a', 'b']
const array2 = [array]
const array3 = [array, "c"]
console.log(moveArrayToParentArray(array))
console.log("----------------------------")
console.log(moveArrayToParentArray(array2))
console.log("----------------------------")
console.log(moveArrayToParentArray(array3))
console.log("----------------------------")
Related
I have a simple two dimensional array like this:
Array
(
[0] => Array
(
[id] => 1
[name] => John
[company] => One
[price] => 12.22
)
[1] => Array
(
[id] => 1
[name] => John
[company] => Two
[price] => 14.33
)
[2] => Array
(
[id] => 2
[name] => Mike
[company] => One
[price] => 15.11
)
[3] => Array
(
[id] => 2
[name] => Mike
[company] => Two
[price] => 10.12
)
[4] => Array
(
[id] => 3
[name] => Paul
[company] => One
[price] => 42.22
)
[5] => Array
(
[id] => 3
[name] => Paul
[company] => Two
[price] => 56.62
)
[6] => Array
(
[id] => 3
[name] => Paul
[company] => Three
[price] => 16.12
)
)
I need to group id and name, then create an array with different values something like this:
Array
(
[0] => Array
(
[id] => 1
[name] => John
[companies] => array (
array(
[company] => One
[price] => 12.22
)
array(
[company] => Two
[price] => 14.33
)
)
)
[1] => Array
(
[id] => 2
[name] => Mike
[companies] => array (
array(
[company] => One
[price] => 15.11
)
array(
[company] => Two
[price] => 10.12
)
)
)
[2] => Array
(
[id] => 3
[name] => Paul
[companies] => array (
array(
[company] => One
[price] => 42.22
)
array(
[company] => Two
[price] => 56.62
)
array(
[company] => Three
[price] => 16.12
)
)
)
)
What is the best way to do it with PHP?
This is my attemp:
<?php
$items=array();
$temp = 0;
$companies = array('uno','dos','tres');
foreach ($values as $value) {
if ($temp == $value['id'] )
continue;
else
$temp == $value['id'];
foreach ($companies as $key => $company){
foreach ($values as $item){
if ($item['id'] == $temp && $item['company'] == $key)
$value['company'][$key] = $item['price'];
}
}
$items[] = $value;
}
Use the id as the key for the new array, then just append a new array with the next company and price
foreach($array as $v) {
$result[$v['id']]['id'] = $v['id'];
$result[$v['id']]['name'] = $v['name'];
$result[$v['id']]['companies'][] = array('company' => $v['company'],
'price' => $v['price']);
}
If you need to re-index it (probably not):
$result = array_values($result);
I have this array with information, which I want to create a new associative array with. Each key in the associative array should be the "name" from the old one. And in each new key, I want the corresponding information to be collected.
Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
[2] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
[3] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[4] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
This is the array I'm trying to create:
Array
(
["Ben"] => Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[2] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
["Evan"] => Array
(
[0] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
)
["Steve"] => Array
(
[0] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
)
)
$newArr = array();
foreach($myArr as $value) {
$name = $value['name'];
if (isset($newArr[$name])) {
$newArr[$name][] = $value;
}
else {
$newArr[$name] = array($value);
}
}
Use a foreach loop to create a new array:
$newArr = [];
foreach($myArr as $key => $value){
$newArr[$myArr[$key][$value['name']]][] = $myArr[$key];
}
Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}
I have one array in php like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] => People
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] => People
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] => Facility
)
)
I want to replace the duplicate type with blank. but the first one should have that type name and others should have blank. so the result array should be like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
I want array in this format only. and i am using PHP zend.
I search for this but most of them showing to remove that element from array. but i don't want to remove it. i want to replace it with blank but want to show the first one.
Tried Code
$result = array();
$result1 = array();
$result2 = array();
$y = array();
$y1 = array();
foreach ($data as $entry) {
$type= $entry["type"];
if (!isset($y[$type])) {
$y[$type] = array();
unset($entry["type"]);
$result[$type][] = $entry;
}
}
can anyone tell me how to do that ?
Code
$i = 0;
$found = 0;
foreach($data as $key=>&$val) {
if($i != 0) {
if($data[$i]['Type'] == $data[$found]['Type']) {
$data[$i]['Type'] = "";
}
else {
$found = $i;
}
}
$i++;
}
echo "<pre>";
print_r($data);
Output
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
Example
I have an array that looks like the below (this is a print_r on a $data variable)
Array
(
[0] => Array
(
[quan] => 1
[prod_key] => 6f2e8858b8333afaeec8cd51be30ba6a
[title] => Broomhandle - 6" x 12.5"
[total] => 11.00
[weight] => 0.25
[image] => thumb_37658989fcd29e9.jpg
)
[1] => Array
(
[quan] => 1
[prod_key] => 6f2e8858b8333afaeec8cd51be30ba6a
[title] => Broomhandle - 6" x 12.5"
[total] => 11.00
[weight] => 0.25
[image] => thumb_37658989fcd29e9.jpg
)
[2] => Array
(
[quan] => 1
[prod_key] => of2ef85vb8333afaeec8cd51be30jq7i
[title] => Watch
[total] => 65.00
[weight] => 0.15
[image] => thumb_37658989fcd29e9.jpg
)
)
What I am trying to do is loop through the array and combine the items that have the same prod_key into one item and update the the total, quantity and weight so the above example should look like:
Array
(
[0] => Array
(
[quan] => 2
[prod_key] => 6f2e8858b8333afaeec8cd51be30ba6a
[title] => Broomhandle - 6" x 12.5"
[total] => 22.00
[weight] => 0.50
[image] => thumb_37658989fcd29e9.jpg
)
[1] => Array
(
[quan] => 1
[prod_key] => of2ef85vb8333afaeec8cd51be30jq7i
[title] => Watch
[total] => 65.00
[weight] => 0.15
[image] => thumb_37658989fcd29e9.jpg
)
)
make a new array and use the product key as an array indice. Then you can easily add or update the entries
$result = array();
foreach ($data as $v) {
if (!isset($result[$v['prod_key']])) {
$result[$v['prod_key']] = $v;
} else {
$result[$v['prod_key']]['quan'] += $v['quan'];
$result[$v['prod_key']]['total'] += $v['total'];
$result[$v['prod_key']]['weight'] += $v['weight'];
//etc...
}
}