merge array values - PHP - php

I have two arrays:
First:
Array
(
[0] => Catalog No.:
[1] => H-B No.
)
Second array:
Array
(
[0] => Array
(
[0] => GG
[1] => 692
)
[1] => Array
(
[0] => VV
[1] => 693
)
)
I want o to merge them into one array to get this:
Array
(
[0] => Array
(
[0] => Catalog No.: GG
[1] => H-B No. 692
)
[1] => Array
(
[0] => Catalog No.: VV
[1] => H-B No. 693
)
)
I tried with array merge, but that works with keys and values, I want to merge only values from this two arrays into one, any help?

<?php
$first = ['Catalog No.:', 'H-B No.'];
$second = [['GG', 692],['VV', 693]];
$result = [];
foreach ($second as $key => $values) {
foreach ($values as $number => $value) {
if (!isset($first[$number])) {
continue;
}
$result[$key][] = $first[$number] . ' ' . $value;
}
}
var_dump($result);

Another way could be using array_map and prepend the values from $first to the current $arr
$result = array_map(function($arr) use ($first){
foreach($first as $key => $value) {
$arr[$key] = $value . ' ' . $arr[$key];
}
return $arr;
}, $second);
Php demo

Related

php - check multidimensional array for values that exists in more than 1 subarray

Following simplified multidimensional array given:
$input = Array
(
[arr1] => Array
(
[0] => JAN2016
[1] => MAI2013
[2] => JUN2014
}
[arr2] => Array
(
[0] => APR2016
[1] => DEC2013
[2] => JUN2014
}
[arr3] => Array
(
[0] => JAN2016
[1] => MAI2020
[2] => JUN2022
}
)
I want to check for elements, that exists in more than 1 subarray. An ideal output would be:
$output = Array
(
[JUN2014] => Array
(
[0] => arr1
[1] => arr2
)
[JAN2016] => Array
(
[0] => arr1
[1] => arr3
)
)
I'm currently stucked in a nested foreach because i need to look all silblings of the outer foreach and don't know how to accomplish that.
foreach($input as $k=>$values)
{
foreach($values as $value)
{
//check if value exists in array k+1....n
//if true, safe to output.
}
}
You are almost all the way there
$new = [];
foreach($input as $k=>$values) {
foreach($values as $value) {
$new[$value][] = $k;
}
}
The $new array should look just as you want it
Extended solution which filters subarrays:
$newArray = [];
foreach($input as $k=>$values)
{
foreach($values as $value)
{
$newArray[$value][] = $k;
}
}
print_r(array_filter(
$newArray,
function($v) { return 1 < count($v); }
));
Sample fiddle here.

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

Need to find the all nearby node elements for every node on the graph using PHP

Problem
Need to find the all nearby node elements for every node on the following graph using PHP
Graph
Input String
$string_arr = array (‘c1#c2#6’, ‘c2#c3#12’, ‘c2#c4#3’, ‘c3#c5#22’, ‘c3#c6#23’, ‘c4#c7#13’, ‘c5#c8#16’, ‘c6#c8#11’, ‘c6#c9#9’, ‘c7#c9#12’, ‘c9#c10#15’, ‘c8#c10#7’);
(Use above variable as input parameter for your code. And you can treat it as string or array or array of string)
Required OUTPUT:
Please print the array showing all node elements with their nearby node.
e.g.
Array(
[c1] => Array
(
[0] => c2
)
[c2] => Array
(
[0] => c1
[1] => c3
[2] => c4
)
)
My Code
<?php
$string_arr = array('c1#c2#6', 'c2#c3#12', 'c2#c4#3', 'c3#c5#22', 'c3#c6#23', 'c4#c7#13', 'c5#c8#16', 'c6#c8#11', 'c6#c9#9',
'c7#c9#12', 'c9#c10#15', 'c8#c10#7');
foreach ($string_arr as $k => $v) {
$tmp = explode('#', $v);
$new_array[$tmp[0]][] = $tmp[1];
#$new_array2[$tmp[0]][] = $tmp[2];
}
print_r($new_array);
?>
My Output
Array (
[c1] => Array
(
[0] => c2
)
[c2] => Array
(
[0] => c3
[1] => c4
)
[c3] => Array
(
[0] => c5
[1] => c6
)
[c4] => Array
(
[0] => c7
)
[c5] => Array
(
[0] => c8
)
[c6] => Array
(
[0] => c8
[1] => c9
)
[c7] => Array
(
[0] => c9
)
[c9] => Array
(
[0] => c10
)
[c8] => Array
(
[0] => c10
)
)
I think all you need to do is also add the items in the other way round, so you currently add...
$new_array[$tmp[0]][] = $tmp[1];
so also add with the values swapped
$new_array[$tmp[1]][] = $tmp[0];
To give...
foreach ($string_arr as $k => $v) {
$tmp = explode('#', $v);
$new_array[$tmp[0]][] = $tmp[1];
$new_array[$tmp[1]][] = $tmp[0];
}
<?php
$string_arr = array ('c1#c2#6', 'c2#c3#12', 'c2#c4#3', 'c3#c5#22', 'c3#c6#23', 'c4#c7#13', 'c5#c8#16', 'c6#c8#11', 'c6#c9#9', 'c7#c9#12', 'c9#c10#15', 'c8#c10#7');
$simplifyArr = array();
foreach ($string_arr as $key => $value) {
$simplifyArr[] = getnode($value);
}
$keysArr = array_unique(array_column($simplifyArr, 'parent'));
$outputArr = array();
foreach ($keysArr as $key) {
foreach ($simplifyArr as $value) {
if($value['parent'] == $key){
$outputArr[$key][] = $value['child'];
}
if($value['child'] == $key){
$outputArr[$key][] = $value['parent'];
}
}
}
echo "<pre>";
print_r($outputArr);
echo "</pre>";
function getnode($string)
{
$extract = explode('#', $string);
$arr['parent'] = $extract[0];
$arr['child'] = $extract[1];
$arr['weight'] = $extract[2];
return $arr;
}
?>

How do I get the array values from two arrays by using keys

My arrays are
$name=>
Array
(
[0] => General
[1] => General
[2] => Outdoors
[3] => Dining
[4] => Dining
[5] => Kitchen
[6] => Kitchen
)
$key1=>
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 11
[4] => 12
[5] => 17
[6] => 18
)
Array function
foreach ($key1 as $key => $value1) {
foreach ($name as $key => $value) {
echo $value "=>" $value1 ;
//echo "$value1";
}
}
Here I would like to print the values by using the same keys
if $name having the index as [0] and my $key1 also take the [0] value
i.e: my result should be in the form of
General => 1
General => 2
Outdoors => 7
Dining => 11
Dining => 12
Kitchen => 17
Kitchen => 18
You only need to iterate one array, not both of them:
foreach ($name as $key => $name_value) {
echo "$name_value => " . $key1[$key];
}
You can use a simple for loop to do this
for ($i = 0; $i < count($name); $i++) {
echo $name[$i] . '=>' . $key[$i]
}
The problem with your code is you're using the same variable $key for both foreachs, so the last one overwrites the value.
foreach ($key1 as $key => $value1) {
foreach ($name as $key => $value) {
echo $value "=>" $value1 ;
//echo "$value1";
}
}
You could make things easier by combining those two arrays, making $name array be the keys and $key1 array be the values
$newArray = array_combine($name, $key1);
foreach ($newArray as $name => $key) {
echo "{$name} =>{$key}";
}
This will work for you
<?php
$a1= array('General','Outdoors','Dining ');
$a2= array('1','2','3');
$newArr=array();
foreach($a1 as $key=> $val)
{
$newArr[$a2[$key]]= $val;
}
echo "<pre>"; print_r($newArr);
?>
output
Array
(
[1] => General
[2] => Outdoors
[3] => Dining
)
I am afraid this wont be possible if you want the output as associative array as same key name in an associative array is not allowed. It would be always overwritten if you are dealing with the associative arrays.
Although you may have something like this:
array_map(function($key, $val) {return array($key=>$val);}, $name, $key1)
Output:
Array ( [0] => Array ( [General] => 1 ) [1] => Array ( [General] => 2 ) [2] => Array ( [Outdoors] => 7 ) [3] => Array ( [Dining] => 11 ) [4] => Array ( [Dining] => 12 ) [5] => Array ( [Kitchen] => 17 ) [6] => Array ( [Kitchen] => 18 ) ).
But if you want the output in string format It is possible.
for ($i = 0; $i < count($key); $i++) {
echo $name[$i] . '=>' . $key[$i].'<br>';
}
Just change the foreach as follows...
foreach ($key1 as $key => $value1) {
echo $name[$key] ."=>". $value1."<br>";
}
replace the <br> with \n if you're running through the linux terminal. Also don't miss the '.' operator to concatenate the string..
Nested foreach won't do what you need... Good luck..

Merge multidimensional arrays in PHP

I would like create a list of values within a new array based on the same keys from the previous array. Basically, I would like to turn this array:
$old_array = Array (
[segment1] => Array (
[subsegment] => Array (
[number1] => 1413
[number2] => 306
)
)
[segment2] => Array (
[subsegment] => Array (
[number1] => 717
[number2] => 291
)
)
)
...into this array:
$new_array = Array (
[segment] => Array (
[subsegment] => Array (
[number1] => Array (
[0] => 1413
[1] => 717
)
[number2] => Array (
[0] => 306
[1] => 291
)
)
)
)
I tried the following:
$new_array = array ();
foreach ($old_array["segment"]["subsegment"] as $value) {
$new_array["segment"]["subsegment"][] = $value;
}
Unfortunately, this doesn't work. What do I need to do? Thanks.
This is very specific to your example $old_array:
$index = 1;
$new_array = array();
do {
if (!isset($old_array["segment" . $index]["subsegment"]))
break;
foreach ($old_array["segment" . $index]["subsegment"] as $key => $value) {
$new_array["segment"]["subsegment"][$key][] = $value;
}
$index++;
} while (true);
I understand you want all number1's in the same key, then all number 2's, and so on. try this:
$numberCount = count($old_array['segment1']['subsegment']);
foreach ($old_array as $segment)
{for ($i=1;$i<=$numberCount;$i++)
{$new_array['segment']['subsegment']['number' . $i][] = $segment['subsegment']['number' . $i];}}
this is assuming all subsegment have the same number of [numberx] keys

Categories