Add value from one element to other in php - php

This is probably a duplicate, but I couldn't find the answer I needed, maybe my wording is wrong.
Anyway, I have a two-dimensional array with hundreds of values, what I need is to insert a value from second element to the first element.
Example;
I have four elements in an array:
[0] = 1, [1] = 9, [2] = 9, [3] =5
I need to put these into a single element, so that it would turn
into this: [0] = 1995.
I have a feeling there might be something I could do with foreach, if so, maybe someone can explain to me, in detail, how that would exactly work?
OR
Maybe there's a function I'm not aware of.

You can use the following, using implode:
$arr = [1, 9, 9, 5];
$val = implode($arr);
unset($arr);
$arr[0] = $val;
demo: https://ideone.com/VTb7vO
To use the solution using implode on the whole multidimensional array, you can use the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
foreach ($arr as $key => $value) {
$val = implode($value);
$arr[$key] = $val;
}
demo: https://ideone.com/X6vueO
another, much shorter solution could be the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
$arr = array_map('implode', $arr);
demo: https://ideone.com/0ju8he

To concatenate each inner array of numbers you can use implode on each of it.
$newArray = array_map('implode', $array);
If it is executed on the array [[1, 2, 3], [1, 3], [1, 1, 1]] it will create the array ['123', '13', '111']. demo

$arr = [2,3,4];
$var = implode($arr, '');
var_dump( $var );
Two dimensional:
$cars = array
(
array(22,18),
array(15,13),
array(5,2),
array(17,15)
);
foreach ($cars as $val) {
$var1 = implode($val, '');
var_dump( $var1 );
}

$var= "";
foreach($arr as $element) {
$var .= $element;
}

Related

How to remove unique elements within multidimensional array?

How to remove dulicate keys from multidimensional array?
My array is as follows:
$array = [[0, 1, 2, 3, 4], [1, 2, 3, 4, 6]];
My desired array should be:
$array = [[0, 1, 2, 3, 4], [6]];
Here's a quick and dirty solution for you:
Walk through every element of the array recursively and if you've not seen an element set it to null (unsetting it doesn't work for some reason). Then filter the resulting sub-arrays.
$array = [[0, 1, 2, 3, 4], [1, 2, 3, 4, 6]];
$seen = [];
array_walk_recursive($array, function (&$v) use (&$seen) {
if (!array_key_exists($v, $seen) {
$seen[$v] = true;
} else {
$v = null;
}
});
$final = array_map('array_filter', $array);
If the functions array_diff() and array_values​​() are used, the solution can be delivered in one line of code:
$array = [[0, 1, 2, 3, 4], [1, 2, 3, 4, 6]];
$array[1] = array_values(array_diff($array[1],$array[0]));
var_export($array);
Output:
array (
0 =>
array (
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
),
1 =>
array (
0 => 6,
),
)
$serialize=array_map('serialize',$array);
$unique=array_unique($serialize);
$result=array_map('unserialize',$uniue);
print_r($result);

Rewriting an Array keys after sorting it

$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);
I have sorted the $arrs to change it to $arr3, $arr2, $arr1, My problem is that it kept its Array Key as it is, I want to rewrite these keys by its new order, so instead of
[2]$arr3 [1]$arr2 [0]$arr1
it becomes
[0]$arr3 [1]$arr2 [2]$arr1
I thought about explode()ing then implode()ing the array again But it didn't work because it is a MDArray like the following $arrs = implode(explode($arrs)); after arsort().
What is the best and shortest way to re[write][make] the array keys?
Just simply use array_values;
$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);
$arrs = array_values($arrs);
This will reset the keys based on the order.
You only need rsort if you don't need to keep key
$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
rsort($arrs);
print_r($arrs);
DEMO
I think you need to use rsort() instead of arsort() because, the latter will preserve the indices when sorting, while the first will reorder the values regardless of the keys.

Map/Merge data from a flat array into the rows of a 2d array [duplicate]

This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 5 months ago.
I want to add the values of array b to array a:
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
Result should be:
$result = [[1, 2, 3],[4, 5, 6],[7, 8, 9]];
I am trying this (and lots of other stuff) but don't get it.
foreach ($a as $el) {
$i = 0;
$el[] = $b[$i];
$i++;
}
Here we are using array_walk to achieve desired output. Hope this will be helpful.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
array_walk($a,function(&$value,$key) use($b){
array_push($value, $b[$key]);
});
print_r($a);
This should be as simple as:
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
foreach($a as $key => &$arr){
$arr[] = $b[$key];
}
This is not hard.
<?php
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
for($i = 0; $i < count($b);$i++) {
array_push($a[$i],$b[$i]);
}
?>
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
$result = $a;
foreach ($a as $key => $val) {
if(!empty($b[$key])) {
array_push($result[$key], $b[$key]);
}
}
var_export($result);
Here is the clever one-liner that no one else thought of:
Code: (Demo)
$a = [[1, 2],[4, 5],[7, 8]];
$b = [3, 6, 9];
var_export(array_map('array_merge',$a,array_chunk($b,1)));
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 4,
1 => 5,
2 => 6,
),
2 =>
array (
0 => 7,
1 => 8,
2 => 9,
),
)
This approach splits $b into the same structure as $a, then it is simple to merge them together.
If your input arrays are relatively small, a foreach() is probably the best choice. As your input array grows in size, I believe you will find that my approach will have increasing performance benefits over foreach() (though I'll be honest I am basing this on other similar answers I have provided and I haven't actually benchmarked this case).

Check if array contains values of another array in the same order

I have two arrays, the first being:
[4, 6, 2, 7, 5, 1]
and the second being:
[2, 7, 5]
How can I determine if the first array contains the SAME values (and in the SAME order) of the second array. In this case the function would return TRUE because the sequence 2, 7, 5 in fact is present in the first array. It would return FALSE for 2, 5, 7. The values exist in the first array, but not in that exact order.
Is there an existing PHP function for this? If no, how should I achieve this?
For common case of array contents:
$ar1 = [2, 4, 6, 2, 4, 7, 5, 1,];
$ar2 = [2, 7, 5];
// Find point where sub-array can start
$keys = array_keys($ar1, $ar2[0]);
foreach($keys as $k)
// Check that sub-array is the same as the second array
if(array_slice($ar1, $k, count($ar2)) == $ar2)
{ echo "Wow!"; break; }
If you array is just numbers, i would suggest a trick:
$a1 = [4, 6, 2, 7, 5, 1];
$a2 = [2, 7, 5];
// convert both arrays to strings,
// add `,` in the beginning and end, see #splash58 comment
$a1_str = ',' . implode(',', $a1) . ',';
$a2_str = ',' . implode(',', $a2) . ',';
// check with strpos:
echo strpos($a1_str, $a2_str) !== false? 'Eq' : 'Not eq';

PHP compare arrays

I have two arrays. $arrayOld and $arrayNew and I want to compare these arrays and only select the values that are not in $arrayNew.
I don't want the values that are in $arrayNew only. So I don't think array_diff() is gonna help me.
$arrayOld = [1, 2, 3, 4, 5]
$arrayNew = [1, 4, 5, 6, 7]
So it only needs to return 2 and 3 and not 6 or 7.
use array_diff, to accomplish this. As you need to difference between the array and need data from Old array so you need to use the old array as the first parameter of the array_diff.
Note: Array diff only returns from the first array which is not in second array.
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
$n = array_diff($arrayOld, $arrayNew);
print_r($n);
Result: Online Check
Array
(
[1] => 2
[2] => 3
)
If you need a new keys for the output array just use array_values. The new key start from 0.
$arr = array_values($n);
Use below code
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
print "<pre>";
print_r(array_diff($arrayOld, $arrayNew));
OUTPUT:
Array
(
[1] => 2
[2] => 3
)
use this code.
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));
$compare = array();
$i=1;
foreach($arrayOld as $k=>$v){
if(!in_array($v, $arrayNew)){
$compare[$i] = $v;
$i++;
}
}
$i=$i;
foreach($arrayNew as $k=>$v){
if(!in_array($v, $arrayOld)){
$compare[$i] = $v;
$i++;
}
}
use array_diff function
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));

Categories