Merge Array Into Single Outside Loop - php

I am attempting to do the following. I have the following array "item_code" within an array:
Array
(
[name] => Wes
[email] => no#no.com
[duration] => 2 days
[comment] => stuff
[item_code] => Array
(
[0] => USE4220HP9,USE4220HP8,USE4220HP7,USE4220HP6,USE4220HP5
[1] => USE0463V8E,USE1066KYN,USE0463V7S,USE1066KYS,USE1066KYK
)
)
The item_code array can potentially have multiple keys - more than two. What I want to do is turn the item_code array into a single array where each USExxxxx value is a single key within the array.
I understand how to use something like foreach and turn the example into this:
$serial = $_POST['item_code'];
foreach ($serial as $sn => $id)
{
$finalsn = ($serial[$sn]);
print_r(explode(',', $finalsn));
}
Result
Array
(
[0] => USE4220HP9
[1] => USE4220HP8
[2] => USE4220HP7
[3] => USE4220HP6
[4] => USE4220HP5
)
Array
(
[0] => USE0463V8E
[1] => USE1066KYN
[2] => USE0463V7S
[3] => USE1066KYS
[4] => USE1066KYK
)
But how can I merge these two arrays into one array? I need this to be a single array outside of the loop so that I can pass the values into a single mysql query. I tried with array_merge but having no luck.

You could merge it into a final array using array_merge
$serial = $_POST['item_code'];
$finalArray = Array();
foreach ($serial as $sn => $id)
{
$finalsn = ($serial[$sn]);
print_r(explode(',', $finalsn));
$finalArray = array_merge($finalArray, explode(',', $finalsn));
}

$serial = $_POST['item_code'];
$final_arr=array()
foreach ($serial as $sn => $id)
{
$finalsn = ($serial[$sn]);
$temp_arr=(explode(',', $finalsn));
foreach($temp_arr as $temp){
$final_arr[]=$temp;
}
}
print_r($final_arr;)

Maybe I'm misunderstanding something here, but with this $testArr contains all USE numbers after the iteration.
$serial = $_POST['item_code'];
$finalArr = array();
foreach ($serial as $sn => $id)
{
$tmpStr = ($serial[$sn]);
$tmpArr= explode(',', $tmpStr);
array_push($finalArr,$tmpArr);
}

Related

Replace every string in multidimensional array if conditions matched

Ok so I have an array look like this,
Array
(
[0] => Array
(
[0] => order_date.Year
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date.Quarter
[1] => =
[2] => 1
)
)
What I want to do is, in any element of this multidimensional array I want to replace any string that have a . with removing everything after .
So the new array should look like this,
Array
(
[0] => Array
(
[0] => order_date
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date
[1] => =
[2] => 1
)
)
I have tried doing this,
foreach ($filter as $key => $value) {
if(is_array($value)) {
$variable = substr($value[0], 0, strpos($value[0], "."));
$value[0] = $variable;
}
}
print_r($filter);
I'm getting $value[0] as order_date but can't figure out how to assign it to $filter array without affecting other values in array;
The $value variable is not linked with the original array in the foreach loop.
You can make a reference to the original array by using ampersand "&"
foreach ($filter as $key => &$value) { ... }
Or you can use old school key nesting
$filter[$key][0] = $variable;
Please take a look here https://stackoverflow.com/a/10121508/9429832
this will take off values after . in every element of any multidimensional array.
// $in is the source multidimensional array
array_walk_recursive ($in, function(&$item){
if (!is_array($item)) {
$item = preg_replace("/\..+$/", "", $item);
}
});

Creating an array from a string issue

I have a function that creates a multi-dimensional array from a string. Here's how the output looks like for each string:
Strings:
app.name.version
app.vendor
NOTE: These are strings that are being retrieved from a database
Output:
['app']['name']['version']
['app']['vendor']
and I assign them values accordingly. The problem arises when I include numbers in the string representing an index number of a sub array. Here's an example:
shifts.breaks.unpaid.0.description
shifts.breaks.unpaid.0.duration
shifts.breaks.unpaid.1.description
shifts.breaks.unpaid.1.duration
with output:
Array
(
[unpaid] => Array
(
[0] => Array
(
[description] => Lunch
)
[1] => Array
(
[duration] => 30
)
[3] => Array
(
[description] => Lunch 2
)
[4] => Array
(
[duration] => 30
)
)
)
Where it should normally look like:
Array
(
[unpaid] => Array
(
[0] => Array
(
[description] => Lunch
[duration] => 30
)
[1] => Array
(
[description] => Lunch 2
[duration] => 30
)
)
)
The only thing that remedies this is if I replace the numbers with anything but numerical values like the following:
shifts.breaks.unpaid.b0.description
shifts.breaks.unpaid.b0.duration
shifts.breaks.unpaid.b1.description
shifts.breaks.unpaid.b1.duration
Array
(
[unpaid] => Array
(
[b0] => Array
(
[description] => Lunch
[duration] => 30
)
[b1] => Array
(
[description] => Lunch 2
[duration] => 30
)
)
)
Here's the function that creates the arrays:
function toArray($keys, $value){
$array = array();
$ref = &$array;
while(count($keys) > 0){
$n = array_shift($keys);
if(!is_array($ref))
$ref = array();
$ref = &$ref[$n];
}
$ref = $value;
return $array;
}
Where $keys contains $keys = explode('.', "my.testing.string"); and here's the example I've been working with:
$strings = array (
"app.names.0.first"=> "Samuel",
"app.names.0.last"=> "Smith",
"app.names.1.first" => "Mary",
"app.names.2.last" =>"Kubik"
);
$list = array();
foreach($strings as $key => $name) {
$list[] = (toArray(explode('.', $key),$name));
}
print_r(call_user_func_array('array_merge_recursive', $list));
At this point, I'm not too sure if this has something to do with array_merge_recursive. Any help in correcting this would be great!
Well one solution I found was re-writing a new array_merge_recursive function without overwriting numeric keys.
function array_merge_recursive_new() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base); //important
while (list($key, $value) = #each($array)) {
if (is_array($value) && #is_array($base[$key])) {
$base[$key] = array_merge_recursive_new($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
Thanks to a user on php.net. This will produce the correct output when keys are numeric.

Merge multple arrays from one result into a single array in PHP

I'm really sorry to bug you, put I've got a problem that I've been trying to resolve for quite some time now. I've done some research and have found things like array_merge but it doesn't appear to help me.
Anyway, enough waffle. I have a result of a query that looks something like this:
Array
(
[0] => STRINGA
)
Array
(
[0] => STRINGA
[1] => STRINGB
)
Array
(
[0] => STRINGA
[1] => STRINGB
[2] => STRINGC
)
Array
(
[0] => STRINGD
[1] => STRINGC
[2] => STRINGA
[3] => STRINGB
[4] => STRINGE
[5] => STRINGF
)
How can I combine the above into one array so that the result will look more like:
Array
(
[0] => STRINGA
[1] => STRINGB
[2] => STRINGC
[3] => STRINGD
[4] => STRINGE
[5] => STRINGF
)
Duplicates in the original arrays can be ignored as I only need the string to be placed into the new array once.
Any help would be massively appreciated.
Thank you.
EDITED: This is the block of code that brings out the result from the database:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
foreach($row as $splitrow) {
if(NULL != $splitrow) {
$therow = explode(';',$splitrow);
}
//print_r retrieves result shown above
print_r($therow);
}
}
$bigarray = array(
array (
0 => 'STRINGA',
),
array (
0 => 'STRINGA',
1 => 'STRINGB',
),
array(
0 => 'STRINGA',
1 => 'STRINGB',
2 => 'STRINGC',
)
);
$result = array_values(
array_unique(
array_merge( $bigarray[0], $bigarray[1], $bigarray[2] )
)
);
// array_merge will put all arrays together, including duplicates
// array_unique removes duplicates
// array_values will sort out the indexes in ascending order (1, 2, 3 etc...)
$bigarray = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
foreach($row as $value){
if($value != NULL){
$therow = explode(';',$value);
foreach($therow as $key=>$values){
//push the value into the single array 'bigarray'
array_push($bigarray, $values);
}
}
}
}
//remove duplicates
$uniquearray = array_unique($bigarray);
//reset key values
$indexedarray = array_values($uniquearray);
print_r($indexedarray);
Thanks to all of those that helped, much appreciated!

Sum multidimensional associative array values while preserving key names

There are many nice Q&A on Stackoverflow on how to take a multidimensional associative array and sum values in it. Unfortunately, I can't find one where the key names aren't lost.
For example:
$arr=array(
array('id'=>'1', 'amount'=>'5'),
array('id'=>'1', 'amount'=>'5'),
array('id'=>'2', 'amount'=>'1'),
array('id'=>'2', 'amount'=>'3')
);
I want the resulting array to look like this:
$result=array(
array('id'=>'1', 'amount'=>'10'),
array('id'=>'2', 'amount'=>'4')
);
Unfortunately the only thing I can figure out how to do is this:
$result = array();
foreach($arr as $amount){
if(!array_key_exists($amount['id'], $arr))
$result[$amount['id']] =0;
$result[$amount['id']] += $amount['amount'];
}
Which when echo'd as follows produces (notice the lack of the keys' words "id" and "amount"):
foreach($result as $id => $amount){
echo $id."==>".$amount."\n";
}
1==>10
2==>4
This is just to show that you already had the data you originally needed, though, the answer you accepted is a better way to deal with it.
You have the following to start with right?
$arr = array(
array('id'=>'1', 'amount'=>'5'),
array('id'=>'1', 'amount'=>'5'),
array('id'=>'2', 'amount'=>'1'),
array('id'=>'2', 'amount'=>'3')
);
Output
Array
(
[0] => Array
(
[id] => 1
[amount] => 5
)
[1] => Array
(
[id] => 1
[amount] => 5
)
[2] => Array
(
[id] => 2
[amount] => 1
)
[3] => Array
(
[id] => 2
[amount] => 3
)
)
Then you run it through the following algorithm:
$summedArr = array();
foreach ($arr as $amount) {
$summedArr['amount'][$amount['id']] += $amount['amount'];
$summedArr['id'][$amount['id']] = $amount['id'];
}
Now, disregarding the Notice warnings that are produced since you are referencing indexes that don't yet exist, this outputs the following:
Output
Array
(
[amount] => Array
(
[1] => 10
[2] => 4
)
[id] => Array
(
[1] => 1
[2] => 2
)
)
Do you see the keys, yet? Because I do.
Now iterate over the array:
foreach ($summedArr as $key => $value) {
echo $k . "==>" . $v . "\n";
}
Output
amount==>Array
id==>Array
That's not what you want, though. You want:
foreach ($summedArr as $key => $arr) {
foreach ($arr as $v) {
echo $key . "==>" . $v;
}
}
Why not use that method, and then reconstruct the array you want at the end?
$results = array();
foreach ($arr as $id=>$amount) {
$results[] = array('id' => $id, 'amount' => $amount);
}
Any other way of doing this would be more computationally expensive.

PHP Group array by values

I have an array like this:
Array (
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
[4] => ing_2_ing
[5] => ing_2_amount
[6] => ing_2_det
[7] => ing_2_meas
)
And I want to group the values into an array like this:
Array (
[0] => Array(
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
)
[1] => Array(
[0] => ing_2_ing
[1] => ing_2_amount
[2] => ing_2_det
[3] => ing_2_meas
)
)
There may be many other items named like that: ing_NUMBER_type
How do I group the first array to the way I want it? I tried this, but for some reason, strpos() sometimes fails:
$i = 1;
foreach ($firstArray as $t) {
if (strpos($t, (string)$i)) {
$secondArray[--$i][] = $t;
} else {
$i++;
}
}
What is wrong? Can you advice?
It depends what you are trying to achieve, if you want to split array by chunks use array_chunk method and if you are trying to create multidimensional array based on number you can use sscanf method in your loop to parse values:
$result = array();
foreach ($firstArray as $value)
{
$n = sscanf($value, 'ing_%d_%s', $id, $string);
if ($n > 1)
{
$result[$id][] = $value;
}
}
<?php
$ary1 = array("ing_1_ing","ing_1_amount","ing_1_det","ing_1_meas","ing_2_ing","ing_2_amount","ing_2_det","ing_2_meas");
foreach($ary1 as $val)
{
$parts = explode("_",$val);
$ary2[$parts[1]][]=$val;
}
?>
This creates:
Array
(
[1] => Array
(
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
)
[2] => Array
(
[0] => ing_2_ing
[1] => ing_2_amount
[2] => ing_2_det
[3] => ing_2_meas
)
)
What I'd do is something like this:
$result = array();
foreach ($firstArray as $value)
{
preg_match('/^ing_(\d+)_/', $value, $matches);
$number = $matches[1];
if (!array_key_exists($number, $result))
$result[$number] = array();
$result[$number][] = $value;
}
Basically you iterate through your first array, see what number is there, and put it in the right location in your final array.
EDIT. If you know you'll always have the numbers start from 1, you can replace $number = $matches[1]; for $number = $matches[1] - 1;, this way you'll get exactly the same result you posted as your example.

Categories