I have problem to create array. I have form.
Form's inputs are tag inputs. First input is option name. When you put any option name the new tag input added in the form.
I post data in controller and create auto variations for product. Eg. Red-S Red-M Blue-S Blue-M ... The Code which i'm using gives variation look like this:
array:4 [
0 => "Red-S"
1 => "Blue-S"
2 => "Red-M"
3 => "Blue-M"
]
And i have another array for option names
array:2 [
0 => "Color"
1 => "Size"
]
I want to create one array look like this:
array:2 [
"Color" => "Red"
"Size" => "S"
]
This is my Controller
function make_combinations($props)
{
if ( empty($props) ) return [];
$keys = array_keys($props);
$key = $keys[0];
$values = $props[$key];
unset($props[$key]);
$rest = make_combinations($props);
if ( empty($values) ) return $rest;
if ( empty($rest) ) return $values;
$combinations = [];
foreach($rest as $comb)
{
foreach($values as $value)
{
$combinations[] = $value . '-' . $comb;
}
}
return $combinations;
}
$inputs = explode(',', $request['option']);
$props = [];
foreach($inputs as $input)
{
$input_key = strtolower($input);
if ( !$request->has($input_key) ) continue;
$input_values = explode(',', $request->input($input_key));
$props[$input_key] = $input_values;
}
$combinations = make_combinations($props);
$result[] = compact('combinations', 'inputs');
return view('system.modules.variants', compact('result'));
}
If I gessed right what are you trying to achieve, having this input:
$inputs = ["Red-S-Cotton", "Blue-Silk-M"];
$keys = ['Color-Size-Material', 'Color-Material-Size'];
By doing a:
$output = array_map(function($key) use ($inputs, $keys) {
$expKeys = explode('-', $keys[$key]);
$expInputs = explode('-', $inputs[$key]);
return array_combine($expKeys, $expInputs);
}, array_keys($inputs));
$output will be:
[
[
"Color" => "Red",
"Size" => "S",
"Material" => "Cotton",
],
[
"Color" => "Blue",
"Material" => "Silk",
"Size" => "M",
]
]
You can skip one loop.
You can write code something like this
$arr = [
0 => "Red-S-Silk-Y",
1 => "Blue-S",
2 => "Red-M-Cotton",
3 => "Blue-M",
];
$arrFinal = [];
$dataArray = [];
foreach($arr AS $value){
$expArr = explode("-",$value);
if(isset($expArr[0])){
$dataArray['color'] = $expArr[0];
}
if(isset($expArr[1])){
$dataArray['size'] = $expArr[1];
}
if(isset($expArr[2])){
$dataArray['Material'] = $expArr[2];
}
if(isset($expArr[3])){
$dataArray['Style'] = $expArr[3];
}
$arrFinal[] = $dataArray;
Unset($dataArray);
}
echo "<pre>";
print_r($arrFinal);
Your output will be something like this.
Array
(
[0] => Array
(
[color] => Red
[size] => S
[Material] => Silk
[Style] => Y
)
[1] => Array
(
[color] => Blue
[size] => S
)
[2] => Array
(
[color] => Red
[size] => M
[Material] => Cotton
)
[3] => Array
(
[color] => Blue
[size] => M
)
)
$result = array_merge($array1, $array2,....);
Related
I need to merge arrays into 1 array but what I need is to remove before the main data they b oth have in common (duplicated values i mean), I need only unique values when merged.
array formed from foreach.
public $arrayfields= [];
public $itemMap = [];
public function getRowMapping(array $row,$uniqueField,$moduleName)
{
$arrayData = [];
foreach ($this->columnMapping as $key => $value) {
$row = $this->moduleType($key,$value,$row,$moduleName);
}
$unique = $row[$uniqueField];
if(!isset($this->itemMap[$unique])){
$this->itemMap[$unique] = $row;
$this->itemMap[$unique][$unique]['items'] = [];
}
$this->itemMap[$unique]['items'][] = $row['items'];
return $row;
}
i changed little bit to my input
How can i do that?
so now the input will be like this
Array
(
[bill_type] => 1
[bill_number] => BIL-003
[order_number] => ON-003
[items] => Array
(
[track_inventory] => 0
[sku1] => sku1
[name] => Kidswear1
)
)
Array
(
[bill_type] => 1
[bill_number] => BIL-003
[order_number] => ON-003
[items] => Array
(
[track_inventory] => 0
[sku1] => sku2
[name] => Pant
)
)
Array
(
[bill_type] => 1
[bill_number] => BIL-002
[order_number] => ON-002
[items] => Array
(
[track_inventory] => 0
[sku1] => sku3
[name] => Pants
)
)
The final output I'm looking for is
Array
(
[BIL-003] => Array
(
[bill_type] => 1
[order_number] => ON-003
[items] => Array
(
[0] => Array(
[track_inventory] => 0
[sku1] => sku1
[name] => Kidswear1
)
[1] => Array
(
[track_inventory] => 0
[sku1] => sku2
[name] => Pant
)
)
[BIL-002] => Array
(
[bill_type] => 1
[order_number] => ON-002
[items] => Array
(
[0] => Array(
[track_inventory] => 0
[sku1] => sku3
[name] => pants
)
)
)~~~
I think there is no standard library function to accomplish this.
So here is the function that accomplishes this:
function merge($array1, $array2)
{
$arraymerged = [];
if (is_array($array1) && is_array($array2)) {
//collect the keys of two arrays
$keys = [];
foreach ($array1 as $key => $value) {
$keys[$key] = true;
}
foreach ($array2 as $key => $value) {
$keys[$key] = true;
}
//merge key values for each key
foreach ($keys as $key => $value) {
if (isset($array1[$key]) && isset($array2[$key])) {
if ($array1[$key] == $array2[$key]) {
$arraymerged[$key] = $array1[$key];
} else {
$arraymerged[$key] = [$array1[$key], $array2[$key]];
}
} else if (isset($array1[$key]))
$arraymerged[$key] = $array1[$key];
else
$arraymerged[$key] = $array2[$key];
}
return $arraymerged;
}
}
Here is my suggestion,
if the input is array, check my answer on this post Convert an associative array into an associative array which has a key with another associative array as its value in php
otherwise check below code.
bill_number as unique key and run foreach and append items array elements.
let me give you an example
$arr1 = [
"bill_type" => 1,
"bill_number" => 'BIL-003',
"items"=>[
0 => [
"name"=>"test"
]
]
];
$arr2 = [
"bill_type" => 1,
"bill_number" => 'BIL-003',
"items"=>[
0 => [
"name"=>"test_2"
]
]
];
$formattedArr = [];
//merge these arrays
$combinedArr = [];
$combinedArr[] = $arr1;
$combinedArr[] = $arr2;
foreach($combinedArr as $key=>$value){
$formattedArr[$value['bill_number']]["bill_type"] = $value["bill_type"];
$formattedArr[$value['bill_number']]["items"][] = $value["items"];
}
print_r($formattedArr);
I haven't tested this code, but i think this will be ans to your question.
<?php
$a1 = array(
"bill_type" => "1",
"bill_number" => "BIL-003",
"order_number" => "ON-003",
"items" => array(
"track_inventory" => "0",
"sku1" => "sku1",
"name" => "Kidswear1"
)
);
$a2 = array(
"bill_type" => "1",
"bill_number" => "BIL-003",
"order_number" => "ON-003",
"items" => array(
"track_inventory" => "0",
"sku1" => "sku2",
"name" => "Pant"
)
);
$result = function ($a1, $a2) {
$b1 = $a1['items'];
$b2 = $a2['items'];
$c1 = $a1;
$c2 = $a2;
unset($c1['items']);
unset($c2['items']);
if (count(array_diff($c1, $c2)) == 0) {
if (count(array_diff($b1, $b2)) != 0) {
$c1['items'] = [$b1, $b2];
} else {
$c1['items'] = $b1;
}
return $c1;
}else{
//you should complete this part by yourself, if first parts are not equal.
}
return $c1;
};
$r = $result($a1, $a2);
var_dump($r);
result:
(array) [4 elements]
bill_type: (string) "1"
bill_number: (string) "BIL-003"
order_number: (string) "ON-003"
items:
(array) [2 elements]
0:
(array) [3 elements]
track_inventory: (string) "0"
sku1: (string) "sku1"
name: (string) "Kidswear1"
1:
(array) [3 elements]
track_inventory: (string) "0"
sku1: (string) "sku2"
name: (string) "Pant"
I have array something like this
Array
(
[0] => Array
(
[product_id] => 13
[unit] => 1
[price] => 20.0000
[total] => 20.0000
)
[1] => Array
(
[product_id] => 15
[unit] => 2
[price] => 30.0000
[total] => 10.0000
)
)
I was stuck to transform above array to the "ProductList" in the following pattern
array(
"UserID" => 2,
"ProductList" => [
{
"ProductID"=> 13,
"Price"=> 20.0000
},
{
"ProductID"=> 15,
"Price"=> 30.0000
}
]
)
I tried like this:
$products = [];
foreach($items as $item) {
if(!empty($item['product_id'])) {
$product = '{"ProductID"=>' . $item['product_id'] . '}';
$products[] = $product;
}
}
But not the expected result.
You're looking for json_encode(): http://php.net/manual/en/function.json-encode.php
Assign your array to a variable, for example $products, and then you can simply write json_encode($products) to achieve your desired result.
Try this,
$prodlist = array();
$x = 0;
foreach($arrs as $arr) {
//if ($arr['product_id'] == "" && $arr['price'] == "") continue;
$prodlist[$x]['Price'] = $arr['price'];
$prodlist[$x]['ProductID'] = $arr['product_id'];
$x += 1;
}
$arr2return['user_id'] = $user_id;
$arr2return['ProductList'] = $prodlist;
json_encode($arr2return);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm newbie in PHP.
I need send data for API purpose.
I have array print_r($detail) like this
Array
(
[0] => Array
(
[id] => item1
)
[1] => Array
(
[price] => 300
)
[2] => Array
(
[quantity] => 1
)
[3] => Array
(
[id] => item2
)
[4] => Array
(
[price] => 400
)
[5] => Array
(
[quantity] => 2
)
)
And I want convert it to multidimensional before sending to another process, like
Array
(
[0] => Array
(
[id] => item1
[price] => 300
[quantity] => 1
)
[1] => Array
(
[id] => item2
[price] => 400
[quantity] => 2
)
)
How it is possible?
You can split your $details array into multiple chunks. I've written the following function which accepts a custom chunk size (note count($initialArray) % $chunkSize === 0 must be true):
function transformArray(array $initialArray, $chunkSize = 3) {
if (count($initialArray) % $chunkSize != 0) {
throw new \Exception('The length of $initialArray must be divisible by ' . $chunkSize);
}
$chunks = array_chunk($initialArray, 3);
$result = [];
foreach ($chunks as $chunk) {
$newItem = [];
foreach ($chunk as $item) {
$newItem[array_keys($item)[0]] = reset($item);
}
$result[] = $newItem;
}
return $result;
}
Given your details array, calling transformArray($details) will result in:
The customizable chunk size allows you to add more data to your source array:
$details = [
['id' => 'item1'],
['price' => 300],
['quantity' => 1],
['anotherProp' => 'some value'],
['id' => 'item2'],
['price' => 400],
['quantity' => 2],
['anotherProp' => 'another value'],
];
The function call is now transformArray($details, 4);:
This version may be with the same result of #PHPglue answer. But this takes only one foreach. I think this is faster, more efficient way as stated here by one of the Top SO Users.
Try to look if you want it.
Live Demo
<?php
function explodeArray($arr)
{
$newArr = array();
foreach($arr as $row)
{
$key = array_keys($row)[0];
$curArr = count($newArr) > 0 ? $newArr[count($newArr)-1] : array();
if( array_key_exists($key, $curArr) )
{
array_push($newArr, array($key=>$row[$key]) );
}
else
{
$index = count($newArr) > 0 ? count($newArr) - 1 : 0 ;
$newArr[$index][$key] = $row[$key];
}
}
return $newArr;
}
Different Arrays for testing.
$detail = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2)
);
var_dump( explodeArray($detail) );
$detail_match = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('newkey'=>'sample'),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2),
array('newkey'=>'sample')
);
var_dump( explodeArray($detail_match) ); // Works with any size of keys.
$detail_diff_key = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('diff1'=>'sample1'),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2),
array('diff2'=>'sample2')
);
var_dump( explodeArray($detail_diff_key) ); // Works with any size of keys and different keys.
$detail_unmatch = array(
array('id'=>'item1'),
array('price'=>300),
array('quantity'=>1),
array('unmatchnum'=>'sample1'),
array('id'=>'item2'),
array('price'=>400),
array('quantity'=>2)
);
var_dump( explodeArray($detail_unmatch) );
I think this little block of code work for you.
Use:
$detail = array(
"0" => array("id" => "item1"),
"1" => array("price" => "300"),
"2" => array("quantity" => "1"),
"3" => array("id" => "item2"),
"4" => array("price" => "400"),
"5" => array("quantity" => "2"),
"6" => array("id" => "item3"),
"7" => array("price" => "500"),
"8" => array("quantity" => "3")
);
$i = 0;
$j = 0;
$multi_details = array();
while($j < count($detail)){
$multi_details[$i][id] = $detail[$j++][id];
$multi_details[$i][price] = $detail[$j++][price];
$multi_details[$i][quantity] = $detail[$j++][quantity];
$i++;
}
echo '<pre>';
print_r($multi_details);
echo '</pre>';
Output:
Array
(
[0] => Array
(
[id] => item1
[price] => 300
[quantity] => 1
)
[1] => Array
(
[id] => item2
[price] => 400
[quantity] => 2
)
[2] => Array
(
[id] => item3
[price] => 500
[quantity] => 3
)
)
Something like this should do it
$newArray = array();
$newRow = array();
foreach ($array as $row) {
$key = array_keys($row)[0];
$value = array_values($row)[0];
if ($key == 'id') {
$newArray[] = $newRow;
}
$newRow[$key] = $value;
}
$newArray[] = $newRow;
This version looks for repeat keys then creates a new Array whenever they are the same so you could have some different data. Hope it helps.
function customMulti($customArray){
$a = array(); $n = -1; $d;
foreach($customArray as $i => $c){
foreach($c as $k => $v){
if(!isset($d)){
$d = $k;
}
if($k === $d){
$a[++$n] = array();
}
$a[$n][$k] = $v;
}
}
return $a;
}
$wow = customMulti(array(array('id'=>'item1'),array('price'=>300),array('quantity'=>1),array('id'=>'item2'),array('price'=>400),array('quantity'=>2)));
print_r($wow);
I have an array within an array like this:
Array
(
[0] => Array
(
[name] => B
[id] => 924572
)
[1] => Array
(
[name] => A
[id] => 120689
)
[2] => Array
(
[name] => A
[id] => 120689
)
[3] => Array
(
[name] => C
[id] => 919644
)
[4] => Array
(
[name] => A
[id] => 120689
)
[5] => Array
(
[name] => B
[id] => 924572
)
)
How can I get the most repeated value from object named name and id?
I've already tried the code below but I'm getting an error: Warning: array_count_values(): Can only count STRING and INTEGER values!
$count = array_count_values($info);
arsort($count);
$popular = array_keys($count);
echo $popular[0];
Any fix regarding to this problem?
"Serializing way" for searching most repeated couples (name,id):
$out = array();
foreach($arr as $el){
$key = serialize($el);
if (!isset($out[$key]))
$out[$key]=1;
else
$out[$key]++;
}
arsort($out);
foreach($out as $el=>$count){
$item = unserialize($el);
echo "Name = ".$item['name'].' ID = '.$item['id'].' Count = '.$count.'<br/>';
}
Output:
Name = A ID = 120689 Count = 3
Name = B ID = 924572 Count = 2
Name = C ID = 919644 Count = 1
update Without loop
.....
arsort($out);
$most = unserialize(key($out));
$most_count = array_shift($out);
echo $most['name'];
echo $most['id'];
echo $most_count;
Output:
A
120689
3
A more linear solution.
$arr = Array
(
Array
(
"name" => "B",
"id" => 924572
),
Array
(
"name" => "A",
"id" => 120689
),
Array
(
"name" => "A" ,
"id" => 120689
),
Array
(
"name" => "C",
"id" => 919644
),
Array
(
"name" => "A",
"id" => 120689
),
Array
(
"name" => "B",
"id" => 924572
));
$countArr = Array();
for($i = 0; $i < count($arr); $i++)
{
$tmpArr = $arr[$i];
if(array_key_exists($tmpArr["name"],$countArr))
$countArr[$tmpArr["name"]]++;
else
$countArr[$tmpArr["name"]] = 0;
}
arsort($countArr);
var_dump($countArr);
Maybe you can work with this solution:
<?php
$info = array(
array(
"name" => "B",
"id" => 924572
),
array(
"name" => "A",
"id" => 120689
),
array(
"name" => "A",
"id" => 120689
),
array(
"name" => "C",
"id" => 919644
),
array(
"name" => "A",
"id" => 120689
),
array(
"name" => "B",
"id" => 924572
),
);
$result = array();
foreach ($info as $infoKey => $infoValue) {
foreach ($infoValue as $itemKey => $itemValue) {
if ($itemKey != "name") {
continue;
}
if (array_key_exists($itemValue, $result)){
$result[$itemValue]++;
continue;
}
$result[$itemValue] = 1;
}
}
arsort($result);
var_dump($result);
Will result in:
array (size=3)
'A' => int 3
'B' => int 2
'C' => int 1
Based on finding the mode and mapping in PHP. Would this work?
$name_array = array_map(function($x) {return $x["name"];}, $info);
$count = array_count_values($name_array);
$mode = array_keys($count, max($count));
To return an array of "name", "id" pairs use:
$return_value = array_filter($info, function($x) use ($mode) { return (in_array($x["name"], $mode));});
Makes use of array_column (requires PHP 5.5 or shim).
$count_values = array_count_values(array_column($array, 'name'));
$most_frequent_name = array_search(max($count_values), $count_values);
Then if you want all arrays with this name:
$items = array_filter($array, function ($v) use ($most_frequent_name) {
return $v['name'] == $most_frequent_name;
});
If several names may have the same top frequency:
$count_values = array_count_values(array_column($array, 'name'));
$most_frequent_names = array_keys($count_values, max($count_values));
$items = array_filter($array, function ($v) use ($most_frequent_names) {
return in_array($v['name'], $most_frequent_names);
});
Try following code. It will give you count of occurrences of all elements
function array_icount_values($arr,$lower=true) {
$arr2=array();
if(!is_array($arr['0'])){$arr=array($arr);}
foreach($arr as $k=> $v){
foreach($v as $v2){
if($lower==true) {$v2=strtolower($v2);}
if(!isset($arr2[$v2])){
$arr2[$v2]=1;
}else{
$arr2[$v2]++;
}
}
}
return $arr2;
}
$arr = array_icount_values($array);
echo "<pre>";
print_r($arr);
I want to create a list where if its already in the array to add to the value +1.
Current Output
[1] => Array
(
[source] => 397
[value] => 1
)
[2] => Array
(
[source] => 397
[value] => 1
)
[3] => Array
(
[source] => 1314
[value] => 1
)
What I want to Achieve
[1] => Array
(
[source] => 397
[value] => 2
)
[2] => Array
(
[source] => 1314
[value] => 1
)
My current dulled down PHP
foreach ($submissions as $timefix) {
//Start countng
$data = array(
'source' => $timefix['parent']['id'],
'value' => '1'
);
$dataJson[] = $data;
}
print_r($dataJson);
Simply use an associated array:
$dataJson = array();
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (!isset($dataJson[$id])) {
$dataJson[$id] = array('source' => $id, 'value' => 1);
} else {
$dataJson[$id]['value']++;
}
}
$dataJson = array_values($dataJson); // reset the keys - you don't nessesarily need this
This is not exactly your desired output, as the array keys are not preserved, but if it suits you, you could use the item ID as the array key. This would simplify your code to the point of not needing to loop through the already available results:
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (array_key_exists($id, $dataJson)) {
$dataJson[$id]["value"]++;
} else {
$dataJson[$id] = [
"source" => $id,
"value" => 1
];
}
}
print_r($dataJson);
You should simplify this for yourself. Something like:
<?
$res = Array();
foreach ($original as $item) {
if (!isset($res[$item['source']])) $res[$item['source']] = $item['value'];
else $res[$item['source']] += $item['value'];
}
?>
After this, you will have array $res which will be something like:
Array(
[397] => 2,
[1314] => 1
)
Then, if you really need the format specified, you can use something like:
<?
$final = Array();
foreach ($res as $source=>$value) $final[] = Array(
'source' => $source,
'value' => $value
);
?>
This code will do the counting and produce a $new array as described in your example.
$data = array(
array('source' => 397, 'value' => 1),
array('source' => 397, 'value' => 1),
array('source' => 1314, 'value' => 1),
);
$new = array();
foreach ($data as $item)
{
$source = $item['source'];
if (isset($new[$source]))
$new[$source]['value'] += $item['value'];
else
$new[$source] = $item;
}
$new = array_values($new);
PHP has a function called array_count_values for that. May be you can use it
Example:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)