How to map an array to another array in php - php

I'm so confused with arrays. Can any one help me to solve this??
I have 4 arrays, these all are related.
My array structure is like:
Array 1:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 2:
Array ( [0] => 500 [1] => 500 [2] => 1 [3] => 2 [4] => 3 [5] => 3 )
Array 3:
Array ( [0] => 2 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 4:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
I have to map all 1 value from Array 1 to another arrays.

If you want to map the values of 4 arrays to each position, you can:
$arr1 = array(1, 2, 1, 1, 2, 1 );
$arr2 = array(500, 500, 1, 2, 3, 3 );
$arr3 = array(2, 2, 1, 1, 2, 1 );
$arr4 = array(1, 2, 1, 1, 2, 1 );
$results = array_map(function($v1, $v2, $v3, $v4) {
return array($v1, $v2, $v3, $v4);
}, $arr1, $arr2, $arr3, $arr4);
echo "<pre>";
print_r( $results );
echo "</pre>";
This will result to:
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 2
[3] => 1
)
[1] => Array
(
[0] => 2
[1] => 500
[2] => 2
[3] => 2
)
[2] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 1
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 2
[3] => 2
)
[5] => Array
(
[0] => 1
[1] => 3
[2] => 1
[3] => 1
)
)
Doc: http://php.net/manual/en/function.array-map.php

Related

How to string convert to array

I want to convert to this array
$string = "1,[1,2,3],[2,2,4],2,3";
To
Example
0 => 1,
1 => [
0 => 1,
1 => 2,
2 => 3
],
2 => [
0 => 2,
1 => 2,
2 => 4
],
3 => 2,
4 => 3
You can use json_decode() :
$string = "1,[1,2,3],[2,2,4],2,3";
$array = json_decode("[$string]", true);
print_r($array);
Output :
Array
(
[0] => 1
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 2
[1] => 2
[2] => 4
)
[3] => 2
[4] => 3
)
Above code tested here

Trying To Get a Custom Array With Looping PHP

Good day, i am trying to generate a csv with array below
array[0] => array(
["a"] => 1,
["b"] => 1,
["c"] => 1, --> the value is only 1 and 2
["d"] => user2,
),
array[1] => array(
["a"] => 1,
["b"] => 1,
["c"] => 2,
["d"] => user3,
),
array[2] => array(
["a"] => 2,
["b"] => 2,
["c"] => 1,
["d"] => user4,
),
array[3] => array(
["a"] => 2,
["b"] => 2,
["c"] => 2,
["d"] => user5,
)
edit: i want to get the 2 array with same number on "a" and "b" value, but different in "c" value
i tried my code below
$results = array();
for($i = 1 ; $i <= 10 ; $i++){
$j = $i+1;
$response = array(
array(
$i, $i, 1, "user".$i
),
array(
$i, $i, 2, "user".$j
)
);
array_push($results, $response);
}
but the array result is not as i wanted like above result. is there any solutions?, sorry for the confusion of this question, i am trying my best to ask. thank you for the help!.
1.You need to use % (modules operator) in your code to set 1 or 2 at third index of each of your child-array.
2.Don't create too many array/variables in your code.Do direct assignments
$results = array();
$j = 1;
for($i=1; $i<=10; $i++){
$j++;
$results[] = array($i, $i, 1, "user".$j);
$j++;
$results[] = array($i, $i, 2, "user".$j);
}
print_r($results);
Output: https://3v4l.org/GE7MD
I think you will have to work with a for loop and a manually increments value for your userx value like this.
$results = [];
$user = 1;
for($i = 1 ; $i <= 10 ; $i++){
$x = 1;
$user++;
$results[] = [$i, $i, $x, "user$user"];
$x = 2;
$user++;
$results[] = [$i, $i, $x, "user$user"];
}
print_r($results);
RESULT:
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => user2
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => user3
)
[2] => Array
(
[0] => 2
[1] => 2
[2] => 1
[3] => user4
)
[3] => Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => user5
)
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
[3] => user6
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 2
[3] => user7
)
[6] => Array
(
[0] => 4
[1] => 4
[2] => 1
[3] => user8
)
[7] => Array
(
[0] => 4
[1] => 4
[2] => 2
[3] => user9
)
[8] => Array
(
[0] => 5
[1] => 5
[2] => 1
[3] => user10
)
[9] => Array
(
[0] => 5
[1] => 5
[2] => 2
[3] => user11
)
[10] => Array
(
[0] => 6
[1] => 6
[2] => 1
[3] => user12
)
[11] => Array
(
[0] => 6
[1] => 6
[2] => 2
[3] => user13
)
[12] => Array
(
[0] => 7
[1] => 7
[2] => 1
[3] => user14
)
[13] => Array
(
[0] => 7
[1] => 7
[2] => 2
[3] => user15
)
[14] => Array
(
[0] => 8
[1] => 8
[2] => 1
[3] => user16
)
[15] => Array
(
[0] => 8
[1] => 8
[2] => 2
[3] => user17
)
[16] => Array
(
[0] => 9
[1] => 9
[2] => 1
[3] => user18
)
[17] => Array
(
[0] => 9
[1] => 9
[2] => 2
[3] => user19
)
[18] => Array
(
[0] => 10
[1] => 10
[2] => 1
[3] => user20
)
[19] => Array
(
[0] => 10
[1] => 10
[2] => 2
[3] => user21
)
)
I removed the array_push() as it is quicker (no function call) to do a simple $arr[] = $something;

How to combine two array and chunk it if the key is same

I'am bulding an app with laravel, and I have a problem, I have two array in php :
Array1
(
[0] => 15
[1] => 15
[2] => 16
[3] => 16
[4] => 17
[5] => 17
[6] => 17
[7] => 17
)
Array2
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 0
[5] => 1
[6] => 2
[7] => 3
)
in this case i can not to use array_chunk because the value of array2 is dinamic and key of array1 must not be same if i combine it, so, how i can combine it to be like this :
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
Simple foreach loop:
$arr1 = [15,15,16,16,17,17,17,17];
$arr2 = [0,1,1,2,0,1,2,3];
$result = [];
foreach($arr1 as $k => $v){
$result[$v][] = $arr2[$k];
}
print_r($result);
The output:
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
$array1 =
[
0 => 15,
1 => 15,
2 => 16,
3 => 16,
4 => 17,
5 => 17,
6 => 17,
7 => 17,
];
$array2 =
[
0 => 0,
1 => 1,
2 => 1,
3 => 2,
4 => 0,
5 => 1,
6 => 2,
7 => 3,
];
$arr = array_unique($array1);
print_r($arr);
$newarray = [];
foreach($arr as $ar){
foreach($array1 as $key => $value){
if($ar == $value){
$newarray[$value][] =$array2[$key];
}
}
}
print_r($newarray);

Multiplying 2 array multidimensional and remove key

I have an multidimensional array which contain some numbers, and i want to multiplying value of array which contain key 0 with each other key inside one area array and erase key 0.
Array
Array
(
[0] => Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 6
[4] => 7
)
[1] => Array
(
[0] => 2
[1] => 7
[2] => 4
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 2
[4] => 5
)
)
Here's the result i want
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I was already combine it using foreach and for, but it still not working for me, any idea how to do this?
The solution using array_map and array_slice functions:
// $arr is your initial array
foreach ($arr as &$v) {
$multiplier = $v[0];
$v = array_map(function($val) use($multiplier){
return $multiplier * $val;
}, array_slice($v, 1));
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)

how we fill missing values with one array to another on the basis of index

Hello sir below is my multidimensional array that contain some missing values on specific index
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] =>
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] =>
)
)
and below is my second array
I want to put the array2 value of index 3 and 4 in to the $array1 index 3 and 4 .but i dnt want to replace whole array value.
I just want to replace the those value that are null in the $array1
$array2 = Array
(
[3] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[5] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
)
Required output below where i show the replace value in single qoutes )
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => '9'
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => '7'
)
This is it:
$array1 = array("2"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"3"=> array("1"=>"4", "2"=>"6", "3"=>""),
"4"=> array("1"=>"4", "2"=>"6", "3"=>"7"),
"5"=> array("1"=>"2", "2"=>"4", "3"=>"")
);
$array2 = array("3"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"5"=> array("1"=>"4", "2"=>"6", "3"=>"7")
);
foreach ($array1 as $key => $value) {
foreach ($value as $key2 => $value2) {
if($value2 == ""){
$array1[$key][$key2] = $array2[$key][$key2];
}
}
}
echo '<pre>';
print_r($array1);
echo '</pre>';
Output:
Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => 9
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => 7
)
)

Categories