PHP Array Readable [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an Array format.
Array
(
[0] => Array
(
[order_id] => 1
)
[1] => Array
(
[order_id] => 11
)
[2] => Array
(
[order_id] => 12
)
)
It should to be changed the array format to be like this format:
[1,11,12];
Please help. Thank you in advanced.

For (PHP 5 >= 5.5.0, PHP 7) you can use array_column and lower than that you can use array_map function (for PHP < 5.3) you need to define saperate function for array_map instead of anonymous function.
$array = array
(
'0' => array
(
'order_id' => 1
),
'1' => array
(
'order_id' => 11
),
'2' => array
(
'order_id' => 12
)
);
$new_array = array_column($array, 'order_id');
print_r($new_array);
$new_array = array_map(function($element) {
return $element['order_id'];
}, $array);
print_r($new_array);
output
Array
(
[0] => 1
[1] => 11
[2] => 12
)

How about this?
$arr = Array
(
0 => Array
(
'order_id' => 1
),
1 => Array
(
'order_id'=> 11
),
2 => Array
(
'order_id' => 12
),
);
$newArr = array();
foreach($arr as $x){
foreach($x as $y){
$newArr[] = $y;
}
}

You can try this:
$ar = array(array('order_id' => 1), array('order_id' => 11), array('order_id' => 12));
$temp = array();
foreach ($ar as $val) {
$temp[] = $val['order_id'];
}
print_r($temp);

Basically, you are looking to flatten the array. This SO answer has some tips on how to do that. Also, there's a golf version in this Github gist.
For the specific case in your question, this should do
function specificFlatten($arr) {
if (!is_array($arr)) return false;
$result = [];
foreach ($arr as $arr_1) {
result[] = $arr_1['order_id'];
}
return $result;
}

Related

Change keys to multidimensional array [closed]

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 2 years ago.
Improve this question
Here is my array
Array
(
[0] => Array
(
[0] => 710715609
[1] => 3
[2] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
[3] => 2
[4] => sports/Mtunes/Astro/D50
[5] => sports-1/Mtunes-1/Astro-1/D50-2
)
[1] => Array
(
[0] => 119774100
[1] => 2
[2] => 2020-02-22 00:01:01/2020-02-22 00:01:01
[3] => 1
[4] => sports/D50
[5] => sports-1/D50-1
)
)
I want to change these 5 keys in array to (msisdn, logCount, logins, transc, actVas) like this and I have tried by using array_fill_keys
Is there a way to do this ?
You can map and combine:
$array = array_map(function($v) {
return array_combine(["msisdn", "logCount", "logins",
"transc", "actVas", "vasCount"], $v);
}, $array);
Or walk and combine:
array_walk($array, function(&$v) {
$v = array_combine(["msisdn", "logCount", "logins",
"transc", "actVas", "vasCount"], $v);
});
You can do either one with a defined array and use() like:
$k = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];
$array = array_map(function($v) use($k) { return array_combine($k, $v); }, $array);
//or
array_walk($array, function(&$v) use($k) { $v = array_combine($k, $v); });
Assuming you may have a very large array, this will just replace the existing arrays keys rather than making another copy of the array.
$pushed = [
[ 710715609, 3, '2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01', 2, 'sports/Mtunes/Astro/D50', 'sports-1/Mtunes-1/Astro-1/D50-2'],
[ 119774100, 2, '2020-02-22 00:01:01/2020-02-22 00:01:01', 1, 'sports/D50', 'sports-1/D50-1']
];
$names = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];
foreach ($pushed as $x => $push){
foreach ($push as $i => $v) {
unset ($pushed[$x][$i]);
$pushed[$x][$names[$i]] = $v;
}
}
print_r($pushed);
RESULT
Array
(
[0] => Array
(
[msisdn] => 710715609
[logCount] => 3
[logins] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
[transc] => 2
[actVas] => sports/Mtunes/Astro/D50
[vasCount] => sports-1/Mtunes-1/Astro-1/D50-2
)
[1] => Array
(
[msisdn] => 119774100
[logCount] => 2
[logins] => 2020-02-22 00:01:01/2020-02-22 00:01:01
[transc] => 1
[actVas] => sports/D50
[vasCount] => sports-1/D50-1
)
)

Merge the associative array with multiple keys [closed]

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 5 years ago.
Improve this question
How to merge the associative array with multiple keys.
I have two arrays below:-
Array1 = Array
(
[294] => Array
(
[2017] => Array
(
[6] => Underperforming
)
)
[236] => Array
(
[2017] => Array
(
[6] => High
)
)
);
Array2 = Array
(
[294] => Array
(
[2017] => Array
(
[5] => Steady
)
)
[236] => Array
(
[2017] => Array
(
[5] => Low
)
)
);
I want these arrays to be merged in PHP and need output like below,
Expecting Output Array:
Array (
[294] => Array
(
[2017] => Array
(
[6] => Underperforming
[5] => Steady
)
)
[236] => Array
(
[2017] => Array
(
[6] => High
[5] => Low
)
)
);
The array keys won't change and the values only be change. Both array has same array structure.
You can do it like below:-
$final_array = array();
foreach($Array1 as $key=>$val){
if(is_array($val) && is_array($Array2[$key])){
if(array_keys($val)[0] == array_keys($Array2[$key])[0]){
$final_array[$key][array_keys($val)[0]] = $Array1[$key][array_keys($Array1[$key])[0]]+$Array2[$key][array_keys($Array2[$key])[0]];
}
}
}
print_r($final_array);
Output:- https://eval.in/834913
A slight better approach:-
$final_array = array();
if(count($Array1) >= count($Array2)){
foreach($Array1 as $key=>$val){
if(is_array($val) && is_array($Array2[$key])){
$final_array[$key][array_keys($val)[0]] = $Array1[$key][array_keys($Array1[$key])[0]]+$Array2[$key][array_keys($Array2[$key])[0]];
}else{
$final_array[$key] = $val;
}
}
}
if(count($Array1) < count($Array2)){
foreach($Array2 as $key=>$val){
if(is_array($val) && is_array($Array1[$key])){
$final_array[$key][array_keys($val)[0]] = $Array1[$key][array_keys($Array1[$key])[0]]+$Array2[$key][array_keys($Array2[$key])[0]];
}else{
$final_array[$key] = $val;
}
}
}
print_r($final_array);
Output:- https://eval.in/835143
For the recursive merge php have array_merge_recursive() function, but it does not work in your case (because you have the numeric keys).
So need a custom function like this (which compares all elements individually):
function multidimensionalMerge(array $mainArray, array $secondArray)
{
foreach ($secondArray as $k => $v) {
if (!empty($mainArray[$k])) {
if (is_array($mainArray[$k]) && is_array($v))
$mainArray[$k] = multidimensionalMerge($mainArray[$k], $v);
else
$mainArray[$k] = $v;
} else
$mainArray[$k] = $v;
}
return $mainArray;
}
$array3 = multidimensionalMerge($array1, $array2);
Example: https://eval.in/835129

Php multidimentional array convert to one dimentional [duplicate]

This question already has answers here:
Multidimensional array to array
(4 answers)
Closed 6 years ago.
I have an array which consist monthly information as shown below :
[appr] => Array
(
[0] => Array
(
[month] => August
[approd] => 23
)
[1] => Array
(
[month] => September
[approd] => 546
)
[2] => Array
(
[month] => October
[approd] => 234
)
)
I want the output as below
[appr] => Array(
August => 23,
September => 546,
October => 234
)
can anybody help me to achieve this using php.
If you're looking for a simple one-liner solution, use array_column() and array_combine() functions.
Try this:
$array['appr'] = array_combine(array_column($array['appr'], "month"), array_column($array['appr'], "approd"));
Simply loop in your array and create a new array
$array = array(array('month'=>'August','approd'=>'23'),array('month'=>'September','approd'=>'56'),array('month'=>'October','approd'=>'234'),);
$new = array();
foreach($array as $val) {
$new['appr'][$val['month']] = $val['approd'];
}
"One-line" solution using array_column function:
$arr['appr'] = array_column($arr['appr'], 'approd', 'month');
print_r($arr);
The output:
Array
(
[appr] => Array
(
[August] => 23
[September] => 546
[October] => 234
)
)
Another option is to use array_walk_recursive:
<?php
$array = array(
0 => array(
'something',
),
1 => array(
'else',
)
);
// If the keys are unique
$newArray = array();
array_walk_recursive($array, function($v, $k) use (&$newArray) {
$newArray[$k] = $v;
});
// If you have duplicate keys
$newArray = array();
array_walk_recursive($array, function($v) use (&$newArray) {
$newArray[] = $v;
});
And finally output the result:
print_r($newArray);
Resources
array_walk_recursive - Manual

Merge duplicates in array PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have an array generated daily that will have duplicate products in it.
[0] => Array
(
[product_id] => 85
[name] => Widescreen Espresso v6.1
[quantity] => 1
)
[1] => Array
(
[product_id] => 85
[name] => Widescreen Espresso v6.1
[quantity] => 2
)
[2] => Array
(
[product_id] => 114
[name] => Panama Esmerelda Diamond Mountain
[quantity] => 1
)
I want to find duplicate products and total them up in an array that would look like this:
[0] => Array
(
[product_id] => 85
[name] => Widescreen Espresso v6.1
[quantity] => 3
)
[1] => Array
(
[product_id] => 114
[name] => Panama Esmerelda Diamond Mountain
[quantity] => 1
)
UPDATE:
I didn't want to remove the duplicates I want to merge duplicates so that the quantity of the product is added together. I managed to work a solution to it with the help of Meenesh Jain's answer below.
$final_array = array();
foreach($order_data as $item => $item_value) {
$pid = $item_value['product_id'];
if(!isset($final_array[$pid])) {
$final_array[$pid] = $item_value;
} else {
$final_array[$pid]['quantity'] += $item_value['quantity'];
}
}
print_r(array_values($final_array));
You can do it with mysqli
OR
you can apply a custom method on your array
$temp_array = $new_array = array();
foreach($array as $key => $arr_values){
if(!in_array($arr_values['product_id'], $temp_array)){
array_push($temp_array, $arr_values['product_id']);
array_push($new_array,$array[$key]);
}
}
// this code will do the trick
My try:
function newArray($oldarray){
$newarray;
$newarray[0] = $oldarray[0];
$ids;
$ids[0] = array($oldarray[0][product_id],0);
for($i = 1; $i < count($oldarray);$i++){
$add = true;
for($x = 0; $x < count($ids);$x++){
if($oldarray[$i][product_id] == $ids[$x][0]){
$add = false;
$newarray[$ids[$x][1]-1] = array($newarray[$ids[$x][1]-1][product_id],$newarray[$ids[$x][1]-1][name],$newarray[$ids[$x][1]-1][quantity]+$oldarray[$i][quantity]);
}
}
if($add == true){
$newarray[count($newarray)] = $oldarray[$i];
$ids[count($ids)] = array($oldarray[$i][product_id],count($newarray));
}
}
return $newarray;
}
You need to use this function (need to pass $array=name of array and $key as 'product_id'):
function super_unique($array,$key)
{
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
Example :
<?php
$vikas=array('0' => array
(
'product_id' => 85,
'name' => "Widescreen Espresso v6.1",
'quantity' => 1
),
'1' => array
(
'product_id' => 85,
'name' => "Widescreen Espresso v6.1",
'quantity' => 2
),
'2' => array
(
'product_id' => 114,
'name' => "Panama Esmerelda Diamond Mountain",
"quantity" => 1
)
);
function super_unique($array,$key)
{
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
//print_r(array_unique($vikas['product_id']));
$vik=super_unique($vikas,'product_id');
print_r($vik);
?>

Addition of two matrix using loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
These are my two matrices in four arrays:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 4
[1] => 5
)
)
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 4
[1] => 5
)
)
How can I add up these matrices using loop?
Try this:-
<?php
$a1 = Array('0' => Array('0' => 1,'1' => 2),'1' => Array('0' => 4,'1' => 5));
$a2 = Array('0' => Array('0' => 1,'1' => 2),'1' => Array('0' => 4,'1' => 5));
$sumArray = array();
$result = array();
for($i=0; $i<=1; $i++) {
for($j=0; $j<=1; $j++) {
$result[$i][$j] = $a1[$i][$j] + $a2[$i][$j];
}
}
echo "<pre/>";print_r($result);
?>
Output:- http://prntscr.com/75hoqi
try this:
$result = array();
for($i=0; $i<=2; $i++) {
for($j=0; $j<=2; $j++) {
$result[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];
}
}
Go with array_merge() thats will merge the array into one

Categories