How to set keys in a multidimentional array in php - php

I have doubt in php i want to set an associative array for which i have the keys ,as well as values.
i have an array $headers and a mutidimentional array $data as follows:
$headers=(
[0] => Testcase Name
[1] => Cell Name
[2] => Customer
[3] => Flops
[4] => Title
[5] => Status
[6] => Mfix CCR(open/close)
[7] => Scenerio-Brief Description
[8] => Expected Results
[9] => CCR Status
[10] => CCR No.
[11] => Remarks
[12] => Testcase Path
)
$data=(
[0] => Array
(
[0] => /a/b/c
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] => close
[7] => 2D Elastic with scanformat=parallel
[8] => No miscompares for both scan and logic tests
[9] =>
[10] => 1716280
[11] =>
[12] =>
)
[1] => Array
(
[0] => /x/y/z
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] => close
[7] => 2D Elastic with scanformat=parallel & explicitshifts
[8] => No miscompares for both scan and logic tests
[9] =>
[10] => 1717028
[11] =>
[12] =>
)
[2] => Array
(
[0] => /a/p/q
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] =>
[7] => Error if explicitshifts greater than scan length
[8] => No miscompares for both scan and logic tests
[9] =>
[10] =>
[11] =>
[12] =>
)
[3] => Array
(
[0] => /s/m/p
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] =>
[7] => 2D Elastic + wide 1 Masking with scanformat=parallel
[8] => No miscompares for both scan and logic tests
[9] =>
[10] =>
[11] =>
[12] =>
)
)
I want to set the numeric keys [0]....[12] as the values of $headers array.
Means i want to replace [0]....[12] with $header[0]....$headers[12].
Please provide a solution.

Use array_combine:
$dataWithKeys = [];
foreach ($data as $row) {
$dataWithKeys[] = array_combine($headers, $row);
}

$result = array();
foreach($data as $key => $val){
$temp = array();
foreach($val as $k => $v){
$temp[$header[$k]] = $v;
}
$result[] = $temp;
}

Related

How to find a different array between the two multidimentional arrays in php

Assume that I have two multidimensional arrays as follows:
Multidimensional array 1:
$newArray = Array
(
[0] => Array
(
[0] => 45521
[1] => Clothing
[2] => Percent discount
[3] => Get 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Germany
)
[1] => Array
(
[0] => 45516
[1] => Apparel
[2] => Percentage Off
[3] => 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Australia
)
[2] => Array
(
[0] => 45672
[1] => Food & Drink
[2] => Free Shipping
[3] => Summer Collection Now. Shop Now!
[4] => 2020-05-17
[5] => 2020-08-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40761312
[8] => Fancy
[9] => US
)
[3] => Array
(
[0] => 45866
[1] => Food & Drink
[2] => Percentage off
[3] => 20% Off
[4] => 2020-05-17
[5] => 2026-05-15
[6] => OFF20
[7] => https://domain[dot]com/AYuANDEqU40780908
[8] => Biata
[9] => US
)
[4] => Array
(
[0] => 45524
[1] => Health & Wellness
[2] => Other
[3] => Jet's monthly bottle of probiotics. Try it now!
[4] => 2020-05-02
[5] => 2026-05-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40758409
[8] => Jet
[9] => US
)
)
[5] => Array
(
[0] => 45524
[1] => Health & Wellness
[2] => Other
[3] => Jet seasonal probiotics. No hidden cost.
[4] => 2020-05-02
[5] => 2026-05-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40758408
[8] => Jet
[9] => US
)
Multidimension array 2:
$oldArray = Array
(
[0] => Array
(
[0] => 45521
[1] => Clothing's
[2] => Percent discount
[3] => Get 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Germany
)
[1] => Array
(
[0] => 45516
[1] => Apparel
[2] => Percentage Off
[3] => 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Australia
)
[2] => Array
(
[0] => 45672
[1] => Food & Drink
[2] => Free Shipping
[3] => Summer Collection Now. Shop Now!
[4] => 2020-05-17
[5] => 2020-08-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40761312
[8] => Fancy
[9] => US
)
[3] => Array
(
[0] => 45866
[1] => Food & Drink
[2] => Percentage off
[3] => 20% Off
[4] => 2020-05-17
[5] => 2026-05-15
[6] => OFF20
[7] => https://domain[dot]com/AYuANDEqU40780908
[8] => Biata
[9] => US
)
[4] => Array
(
[0] => 45706
[1] => Electronic Equipment
[2] => Percentage off
[3] => 40% Off-Set Under $200!
[4] => 2020-05-17
[5] => 2026-05-15
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40768379
[8] => GVM
[9] => US
)
)
You can easily see that the $newArray has one different array([4]) from the $oldArray, and one new array([5]) to the $oldArray, and one different key=value pair like [1] => Clothing's from the $oldArray
What i am expecting is to compare the $newArray with the $oldArray to get the different array([4]) and the new one array([5]) in the $newArray only.
I have searched for these links:
https://www.php.net/manual/en/function.array-diff-assoc.php
and
https://www.w3schools.com/php/func_array_diff_assoc.asp
but they do not help.
Then I've searched for the function in this link:
Compare two different multidimentional arrays and highlight the changes
The function:
function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff;
}
} else {
if ($mValue != $aArray2[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}
$arr1 = arrayRecursiveDiff($newentry,$oldentry);
It filters and shows me both the COMPLETE new arrays from the $newArray and the new arrays from the $newArray with only the key=value pairs that are different from the $oldArray.
Yes and again, what i am expecting is to compare the $newArray with the $oldArray to get:
1/ The COMPLETE different array like array([4]) from the $newArray.
2/ The new array like array([5]) from the $newArray.
3/ The arrays from the $newArray of which one of the key=value pairs is different from that of the $oldArray, but it must print all 9 key=value pairs of that array, which is the array[0] in this example. I don't want to get the new arrays from $newArray with the output of ONLY the updated key=value pairs.
Sorry for a long post with wordy explanation, but I have no choice.
Your help is appreciated.
$a1=array(1,2,3);
$a2=array(1,2,4);
$i =0;
$diff_Value = '';
foreach( $a1 as $val )
{
if($a2[$i]!=$val){
$diff_Value.= $val.','.$a2[$i];
}
$i =$i+1;
}
echo $diff_Value;

php get sub array from an array

here is an array and I want to extract "category_input" as sub array. This sub array contains "cat_id" and "post-titles". Then I will want to insert the posts into WordPress DB under each category. is. cat_20 will have posts named as post-in-cat-20 and so on.
Array
(
[post_type] => post
[post_status] => publish
[content_texarea] =>
[category_input] => Array
(
[0] => cat_20
[1] => post-in-cat-20
[2] => post-in-cat-20
[3] => post-in-cat-20
[4] => cat_19
[5] => post-in-cat-19
[6] => post-in-cat-19
[7] => post-in-cat-19
[8] => post-in-cat-19
[9] => cat_2
[10] => post-in-cat-2
[11] => post-in-cat-2
[12] => post-in-cat-2
)
)
Expected Results
extracted array will be as
[category_input] => Array
(
[0] => cat_20
[1] => post-in-cat-20
[2] => post-in-cat-20
[3] => post-in-cat-20
[4] => cat_19
[5] => post-in-cat-19
[6] => post-in-cat-19
[7] => post-in-cat-19
[8] => post-in-cat-19
[9] => cat_2
[10] => post-in-cat-2
[11] => post-in-cat-2
[12] => post-in-cat-2
)
Then, I will need to make caegory and posts array to use in wp query
[category_input] => Array
(
[cat_20] => Array
[1] => post-in-cat-20
[2] => post-in-cat-20
[3] => post-in-cat-20
[cat_19] => Array
[5] => post-in-cat-19
[6] => post-in-cat-19
[7] => post-in-cat-19
[8] => post-in-cat-19
[cat_2] => Array
[10] => post-in-cat-2
[11] => post-in-cat-2
[12] => post-in-cat-2
)
As you want to update the portion of your array- take a look at the given parts.
The array from the main array by $main_arr[category_input];
foreach($category_input as $val){
$part = explode('_', $val);
if(isset($part[0]) && $part[0] == 'cat'){
$out_arr[$val] = array();
$key = $val;
}else{
$out_arr[$key][] = $val;
}
}
Look at the complete example: https://3v4l.org/JI9U7
Now you can put the $out_arr again to the $main_arr.

Php array saving values only till 62 indices

I am sending a big array of 130+ indices from ajax to php. But while going to php, if i print, it became a 2D array with indices 63, 63, 6 respectively.
Below is snip
Array
(
[0] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
................
[60] => 1452105000
[61] => 1454869800
[62] => 1457375400
)
[1] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
......
[61] => 1454869800
[62] => 1457289000
)
[2] => Array
(
[0] => 1440441000
[1] => 1443033000
[2] => 1445970600
[3] => 1445970600
[4] => 1447007400
[5] => 1448908200
)
)
But i want them in a single D array...[0]--[127] together. I tried to copy them using foreach aswell. It copies the first 63 indices and stops. Any one please help. Thanks
You must use foreach twice:
INTPUT (example):
Array
(
[0] => Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
)
[1] => Array
(
[0] => 1115002
[1] => 27608
[2] => 3531620
[3] => 4412066
[4] => 4032217
[5] => 2681734
)
[2] => Array
(
[0] => 3360879
[1] => 5190034
[2] => 3452229
[3] => 5112636
[4] => 628357
[5] => 4299124
)
)
PHP Code:
$output = array();
foreach($input as $key=>$sub){
foreach($sub as $k => $v){
$output[] = $v;
}
}
print_r($output);
Output:
Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
[6] => 1115002
[7] => 27608
[8] => 3531620
[9] => 4412066
[10] => 4032217
[11] => 2681734
[12] => 3360879
[13] => 5190034
[14] => 3452229
[15] => 5112636
[16] => 628357
[17] => 4299124
)

PHP array_filter() not working

Goal here is to take text from a log file, plain text delimited by returns with each line delimited by a varying number of spaces.
Getting it into a multi-dimensional is easy enough, getting rid of blank spaces is not. I suppose there are a lot of messy ways to go about this but isn't it one of the reasons for array_filter()?
$alarms = array(
"1530 1545 Place_4 Fault_1",
"1617 1622 Place_1 Fault_2",
"1634 1640 Place_2 Fault_1"
);
foreach ($alarms as $data) {
$subArr = explode(" ", $data);
array_filter($subArr);
print_r($subArr);
echo "<br /><br />";
}
Output:
Array ( [0] => 1530 [1] => [2] => [3] => [4] => [5] => 1545 [6] => [7] => [8] => [9] => [10] => Place_4 [11] => [12] => [13] => [14] => [15] => Fault_1 )
Array ( [0] => 1617 [1] => [2] => [3] => [4] => [5] => 1622 [6] => [7] => [8] => [9] => [10] => Place_1 [11] => [12] => [13] => [14] => [15] => Fault_2 )
Array ( [0] => 1634 [1] => [2] => [3] => [4] => [5] => 1640 [6] => [7] => [8] => [9] => [10] => Place_2 [11] => [12] => [13] => [14] => [15] => Fault_1 )
Want it to be:
Array ( [0] => 1530 [1] => 1545 [2] => Place_4 [3] => Fault_1 )
Array ( [0] => 1617 [1] => 1622 [2] => Place_1 [3] => Fault_2 )
Array ( [0] => 1634 [1] => 1640 [2] => Place_2 [3] => Fault_1 )
Not sure what's wrong...
Cheers.
From the manual, array_filter:
Return values:
Returns the filtered array.
Emphasis on returns. Currently you ignore the return value.
Rather than explode(" ",$something), try this:
$parts = preg_split("/ +/",$something);
This will split on variable numbers of spaces.
updated code
$alarms = array(
"1530 1545 Place_4 Fault_1",
"1617 1622 Place_1 Fault_2",
"1634 1640 Place_2 Fault_1"
);
foreach ($alarms as $data) {
$subArr = explode(" ", $data);
$subArr = array_filter($subArr);
print_r($subArr);
echo "<br /><br />";
}

How to group and calculate value array when found same two keys array

I have php array like this:
Array (
[0] => Array ([electronics] => TV [condition] => GOOD [1] => 100 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[1] => Array ([electronics] => TV [condition] => NOTGOOD [1] => 50 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[2] => Array ([electronics] => AC [condition] => GOOD [1] => 200 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[3] => Array ([electronics] => TV [condition] => GOOD [1] => 50 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[4] => Array ([electronics] => AC [condition] => GOOD [1] => 50 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
)
Then I want to group and calculation by [electronics] and [condition].
(calculated when found same [electronics] and [condition] in that array).
And the result I want like this:
Array (
[0] => Array ([electronics] => TV [condition] => GOOD [1] => 150 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[1] => Array ([electronics] => TV [condition] => NOTGOOD [1] => 50 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[2] => Array ([electronics] => AC [condition] => GOOD [1] => 250 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
)
This should be one way to do it:
$result = array();
foreach ($arrays as $array) {
$key = $array['electronics'] . $array['condition'];
foreach ($array as $k => $v) {
if (is_numeric($k)) {
$result[$key][$k] += $v;
} else {
$result[$key][$k] = $v;
}
}
}
$result = array_values($result);

Categories