php get sub array from an array - php

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.

Related

How to set keys in a multidimentional array in 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;
}

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
)

Filter part of multidimensional array

I would like to filter part of a multidimensional array. I have used the array_filter function. When I print the filtered data, it shows correctly, but I can't seem to save the data back to the array.
Here is the multidimensional array (called $posted_product_details) beforehand, containing the internal array ([data]) which I would like filter:
Array
(
[column_1] => Array
(
[name] => Colour
[data] => Array
(
[0] => Blue
[1] => Green
[2] => Red
[3] => Yellow
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_2] => Array
(
[name] => Pack QTY
[data] => Array
(
[0] => 3
[1] => 3
[2] => 3
[3] => 3
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_3] => Array
(
[name] => Product Code
[data] => Array
(
[0] => 65030
[1] => 65029
[2] => 65028
[3] => 65031
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_4] => Array
(
[name] => Barcode
[data] => Array
(
[0] => 5099570650307
[1] => 5099570650291
[2] => 5099570650284
[3] => 5099570650314
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
[column_5] => Array
(
[name] =>
[data] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
)
)
Here I attempt to loop through the array and filter the data:
foreach ($posted_product_details as $column => $info) {
$name = $info['name'];
$data = $info['data'];
$info['data'] = array_filter($data);
}
However, upon printing the array afterward, the array has not changed.
Pass the value by reference to modify the original array:
foreach ($posted_product_details as $column => & $info) {
$name = $info['name'];
$data = $info['data'];
$info['data'] = array_filter($data);
}
This will correctly filter the data part of your array. However, if you need to filter out deeper elements, you'll have to use a recursive function, such as this one.
Demo!
The foreach construct makes a copy of each piece of the array as you iterate over it. You have to explicitly call the original array to edit it:
$posted_product_details[$column]['data'] = array_filter($data);

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);

Problem while ordering an array in PHP

Well I have a PHP array like this:
Array
(
[0] => post9.post
[3] => post8.post
[4] => post4.post
[5] => post7.post
[6] => post5.post
[7] => post10.post
[8] => post3.post
[9] => post6.post
[10] => post1.post
[11] => post2.post
)
But after trying all the PHP array functions I don't manage to sort it in this way:
Array
(
[0] => post10.post
[1] => post9.post
[2] => post8.post
[3] => post7.post
[4] => post6.post
[5] => post5.post
[6] => post4.post
[7] => post3.post
[8] => post2.post
[9] => post1.post
)
I already made a question like this, which seemed to work but now it doesn't works :(
EDIT: After using rsortit looks like this:
Array
(
[0] => post9.post
[1] => post8.post
[2] => post7.post
[3] => post6.post
[4] => post5.post
[5] => post4.post
[6] => post3.post
[7] => post2.post
[8] => post10.post
[9] => post1.post
)
Almost good, except that post10.post should be [0] and is [8]
You need to use natsort and then reverse the order.
natsort($array);
$array = array_reverse($array);

Categories