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.
Related
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
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
I have two array as below
Array 1
Array
(
[0] => Array
(
[ps_id] => 5
[product_id] => 2
[supplier_id] => 25
[cost] => 789.00
[name] => Mahesh
)
[1] => Array
(
[ps_id] => 6
[product_id] => 2
[supplier_id] => 2
[cost] => 12345.00
[name] => mayank
)
[2] => Array
(
[ps_id] => 7
[product_id] => 2
[supplier_id] => 1
[cost] => 123456.00
[name] => abc
)
[3] => Array
(
[ps_id] => 10
[product_id] => 2
[supplier_id] => 8
[cost] => 12000.00
[name] => mayank1
)
)
Array 2
Array
(
[0] => Array
(
[suppliers] => Mahesh
[suppliers_cost] => 789.00
)
[1] => Array
(
[suppliers] => mayank
[suppliers_cost] => 12345.00
)
[2] => Array
(
[suppliers] => mayank1
[suppliers_cost] => 12000.00
)
[3] => Array
(
[suppliers] => testtetstet
[suppliers_cost] => 123123
)
)
I want to compare above array by their suppliers and name key,
Means if this both key have same values than it will store into one new array and if those key are not match then they will store new different array.
Or might be it possible that both array will have different number of keys
I had tried like below
foreach ($existsProductSupplier as $key => $value) {
if (isset($supplier_data[$key])) {
}else{
$supplier_data[$key]['suppliers']='';
$supplier_data[$key]['suppliers_cost']='';
}
}
foreach ($supplier_data as $key => $value) {
if(in_array($value['suppliers_cost'],$existsProductSupplier[$key])){
//echo "string";
// print_r($value);
}else{
echo "string";
//print_r($value);
}
}
try this code it will help you
<?php
$arr1=Array
(
0 => Array
(
"ps_id" => 5,
"product_id" => 2,
"supplier_id" => 25,
"cost" => 789.00,
"name" => "Mahesh"
),
1 => Array
(
"ps_id" => 6,
"product_id" => 2,
"supplier_id" => 2,
"cost" => 12345.00,
"name" => "mayank"
),
2 => Array
(
"ps_id" => 7,
"product_id" => 2,
"supplier_id" => 1,
"cost" => 123456.00,
"name" => "abc"
),
3 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank1"
),
4 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank2"
)
);
$arr2=Array
(
0 => Array
(
"suppliers" => "Mahesh",
"suppliers_cost" => 789.00
),
1 => Array
(
"suppliers" => "mayank",
"suppliers_cost" => 12345.00
),
2 => Array
(
"suppliers" => "mayank1",
"suppliers_cost" => 12000.00
),
3 => Array
(
"suppliers" => "testtetstet",
"suppliers_cost" => 123123
)
);
foreach($arr1 as $key=>$value){
if(isset($arr2[$key])){
if($value['name']==$arr2[$key]['suppliers']){
$arrnew1[]=$value['name'];
}else{
$arrnew2[]=$value['name'];
}
}else{
$arrnew2[]=$value['name'];
}
}
print_r($arrnew1);
print_r($arrnew2);
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);
This question already has answers here:
How to sort an array of arrays in php?
(3 answers)
php - usort or array_multisort?
(3 answers)
Get the first N elements of an array?
(5 answers)
PHP: Any function that return first/last N elements of an array
(2 answers)
Closed 2 years ago.
I have the following array:
Array (
[0] => Array (
[count] => 9
[user_id] => 2
)
[1] => Array (
[count] => 25
[user_id] => 1
)
[2] => Array (
[count] => 20
[user_id] => 3 )
[3] => Array (
[count] => 6
[user_id] => 56 )
[4] => Array (
[count] => 2
[user_id] => 37 )
[5] => Array (
[count] => 1
[user_id] => 0
))
This is just a sample. The actual array will contain many more sub arrays.
I need to be able to obtain the top five values from "count" and store them with their associated "user_id".
The final result needs to look something like this:
Array (
[0] => Array (
[count] => 25
[user_id] => 1
)
[1] => Array (
[count] => 20
[user_id] => 3
)
[2] => Array (
[count] => 9
[user_id] => 2
)
[3] => Array (
[count] => 6
[user_id] => 56
)
[4] => Array (
[count] => 2
[user_id] => 37
)
[5] => Array (
[count] => 1
[user_id] => 0
) )
If this can be done by simply re ordering the array, that is fine.
Thanks!
You're looking for usort and array_slice.
Example:
<?php
$array = array(
array(
'count' => 9,
'user_id' => 2
),
array(
'count' => 25,
'user_id' => 1
),
array(
'count' => 20,
'user_id' => 3
),
array(
'count' => 6,
'user_id' => 56
),
array(
'count' => 2,
'user_id' => 37
),
array(
'count' => 1,
'user_id' => 0
)
);
function usort_callback($a, $b)
{
if ( $a['count'] == $b['count'] )
return 0;
return ( $a['count'] > $b['count'] ) ? -1 : 1;
}
usort($array, 'usort_callback');
$top5 = array_slice($array, 0, 5);
print_r($top5);
Outputs:
Array
(
[0] => Array
(
[count] => 25
[user_id] => 1
)
[1] => Array
(
[count] => 20
[user_id] => 3
)
[2] => Array
(
[count] => 9
[user_id] => 2
)
[3] => Array
(
[count] => 6
[user_id] => 56
)
[4] => Array
(
[count] => 2
[user_id] => 37
)
)
usort($array, function ($a, $b) { return $b['count'] - $a['count']; });
$top5 = array_slice($array, 0, 5);