How to string convert to array - php

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

Related

Merging two array with same values in php

I have two arrays.
Array 1
Array
(
[1] => 111,
[id1] => 1,
[2] => 11231,
[id2] => 2,
[3] => 12311,
[id3] => 3,
[4] => 11981,
[id4] => 4,
[5] => 11761,
[id5] => 5,
[6] => 11561,
[id6] => 6
)
Array 2
Array
(
[1] => 2,
[id1] => 1,
[2] => 2,
[id2] => 2,
[3] => 3,
[id3] => 3,
[4] => 4,
[id4] => 4,
[5] => 4,
[id5] => 5,
[6] => 6,
[id6] => 6
)
Id key is user id in both arrays
And numerical key in second array is manager id
I want to merge these two array in below format.
Merge array
Array
(
[2] => Array
(
[0] => 111
[1] => 1
),
Array
(
[0] => 11231
[1] => 2
),
[3] => Array
(
[0] => 12311
[1] => 3
),
[4] => Array
(
[0] => 11981
[1] => 4
),
Array
(
[0] => 11761
[1] => 5
),
[6] => Array
(
[0] => 11561
[1] => 6
)
)
Array inside array is the value of first array.
2,3,4,6 key is values from second array.
Those users who have same manager id will be merge in single array.
Array Walk Apply a user supplied function to every member of an array
array_walk($array2, function($value,$key) use($array1,&$result){
if(is_integer($key))
$result[$value][]=[$array1[$key],$array1['id'.$key]];
});
output
array (size=4)
2 =>
array (size=2)
0 =>
array (size=2)
0 => int 111
1 => int 1
1 =>
array (size=2)
0 => int 11231
1 => int 2
3 =>
array (size=1)
0 =>
array (size=2)
0 => int 12311
1 => int 3
4 =>
array (size=2)
0 =>
array (size=2)
0 => int 11981
1 => int 4
1 =>
array (size=2)
0 => int 11761
1 => int 5
6 =>
array (size=1)
0 =>
array (size=2)
0 => int 11561
1 => int 6

Sort sub-arrays so corresponding values within each sub-array are in numerical order?

I need a custom sort function that will take a 2 dimensional array of values, and sort the sub-arrays so that the corresponding values within each sub-array are in numerical order. Anything with a value of -1 can be ignored.
Since this is a bit difficult to explain using words, here is an example. Starting with the following:
[
["1st"] => [ [0] => 0 [1] => 0 [2] => 0 ]
["2nd"] => [ [0] => 2 [1] => 2 [2] => 3 ]
["3rd"] => [ [0] => 3 [1] => -1 [2] => -1 ]
["4th"] => [ [0] => 5 [1] => 6 [2] => 11 ]
["5th"] => [ [0] => -1 [1] => 3 [2] => 4 ]
["6th"] => [ [0] => -1 [1] => 4 [2] => -1 ]
["7th"] => [ [0] => -1 [1] => 5 [2] => 10 ]
["8th"] => [ [0] => -1 [1] => -1 [2] => 2 ]
["9th"] => [ [0] => -1 [1] => -1 [2] => 5 ]
]
["4th"][1] is higher than any of the other [1] column values, so it needs to be moved to the end. Next, the [2] row that has 2 values out of sequence. Once those are in their proper place, the array should look like this:
[
["1st"] => [ [0] => 0 [1] => 0 [2] => 0 ]
["8th"] => [ [0] => -1 [1] => -1 [2] => 2 ]
["2nd"] => [ [0] => 2 [1] => 2 [2] => 3 ]
["3rd"] => [ [0] => 3 [1] => -1 [2] => -1 ]
["5th"] => [ [0] => -1 [1] => 3 [2] => 4 ]
["9th"] => [ [0] => -1 [1] => -1 [2] => 5 ]
["6th"] => [ [0] => -1 [1] => 4 [2] => -1 ]
["7th"] => [ [0] => -1 [1] => 5 [2] => 10 ]
["4th"] => [ [0] => 5 [1] => 6 [2] => 11 ]
]
With the -1 values ignored, the [0] column now reads "0, 2, 3, 5", the [1] column reads "0, 2, 3, 4, 5, 6", and the [2] column reads "0, 2, 3, 4, 5, 10, 11".
This requires that the rows are "in agreement" so to speak, and will not work for every case. I generally understand what I need to do here, I'm just not sure how to write it as actual code.
I will be using this to determine the order in which elements will be displayed on a website, so I need a solution in PHP. (I will probably want a JavaScript version as well, but once I have something working in PHP, I can work that out myself).

How to map an array to another array in 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

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

PHP: sorting multiple arrays by common value

I have multiple arrays that have a different structure but they all got a column named "round" and "eventnumber". How do I merge them all into one array where they are ordered by an array with the round and then listed by the eventnumber? (I have made the arrays shorter than they really are)
$a =
Array (
[0] => Array ( [id] => 1 [eventid] => 3 [round] => 1 [eventnumber] => 1 )
[1] => Array ( [id] => 2 [eventid] => 3 [round] => 2 [eventnumber] => 11 )
)
$b =
Array (
[0] => Array ( [id] => 1 [eventid] => 7 [round] => 1 [eventnumber] => 5 )
[1] => Array ( [id] => 2 [eventid] => 8 [round] => 1 [eventnumber] => 3 )
[2] => Array ( [id] => 3 [eventid] => 8 [round] => 2 [eventnumber] => 6 )
)
$c =
Array (
[0] => Array ( [id] => 1 [eventid] => 6 [round] => 2 [eventnumber] => 2 )
[1] => Array ( [id] => 2 [eventid] => 5 [round] => 1 [eventnumber] => 4 )
)
Desired Result:
$rounds =
Array (
[0] => Array ( [id] => 1 [eventid] => 3 [round] => 1 [eventnumber] => 1 )
[1] => Array ( [id] => 2 [eventid] => 8 [round] => 1 [eventnumber] => 3 )
[2] => Array ( [id] => 2 [eventid] => 5 [round] => 1 [eventnumber] => 4 )
[3] => Array ( [id] => 1 [eventid] => 7 [round] => 1 [eventnumber] => 5 )
)
Array (
[0] => Array ( [id] => 1 [eventid] => 6 [round] => 2 [eventnumber] => 2 )
[1] => Array ( [id] => 3 [eventid] => 8 [round] => 2 [eventnumber] => 6 )
[2] => Array ( [id] => 2 [eventid] => 3 [round] => 2 [eventnumber] => 11 )
)
I have looked for answers but cant seem to get this to work.
Thanks for taking the time to help :)
I have replaced your arrays to a,b and c to be simpler. I started by merging the arrays and then sorting them with a comparison function:
$a =
Array (
0 => Array ( "id" => 1, "eventid" => 3, "round" => 1, "eventnumber" => 1 ) ,
1 => Array ( "id" => 2, "eventid" => 3, "round" => 2, "eventnumber" => 11 )
);
$b =
Array (
0 => Array ( "id" => 1, "eventid" => 7, "round" => 1, "eventnumber" => 5 ),
1 => Array ( "id" => 2, "eventid" => 8, "round" => 1, "eventnumber" => 3 ),
2 => Array ( "id" => 3, "eventid" => 8, "round" => 2, "eventnumber" => 6 )
);
$c =
Array (
0 => Array ( "id" => 1, "eventid" => 6, "round" => 2, "eventnumber" => 2 ),
1 => Array ( "id" => 2, "eventid" => 5, "round" => 1, "eventnumber" => 4 )
);
$result = Array();
for ($i = 0; $i < 3; ++$i){
if (isset($a[$i])) {
array_push($result, $a[$i]);
}
if (isset($b[$i])) {
array_push($result, $b[$i]);
}
if (isset($c[$i])) {
array_push($result, $c[$i]);
}
}
function custom_sort($x,$y) {
if ($x['round'] == $y['round']){
return $x['eventnumber']>$y['eventnumber'];
}
return $x['round']>$y['round'];
}
usort($result, "custom_sort");
print_r($result);
To create a $rounds array that separates each round in a different array you can do:
$rounds = Array();
foreach($result as $row){
if (!isset($rounds[$row["round"]])){
$rounds[$row["round"]] = Array();
}
array_push($rounds[$row["round"]], $row);
}
print_r($rounds);
This naturally assumes the previous $results array. I didn't join all the code so that its clearer the separating part.

Categories