What I'm trying to do is compare the keys of the second level of a multidimensional array. here's an example:
$data = array(
array(
$a => $b,
$c => $d
),
array(
$e => $f,
$g => $h
)
)
How would I compare $a & $e?
Here, knowing that they have the same size and you want to compare sorted keys:
<?php
$data_1 = array_keys($data[0]);
$data_2 = array_keys($data[1]);
$size = count($data_1);
for($i=0;$i<$size;$i++)
{
if($data_1[$i]<$data_2[$i])
{//do smth
}
}
?>
$keys0 = array_keys($data[0]);
$keys1 = array_keys($data[1]);
if ($keys0[0] == $keys1[0])
{
...
}
Related
I want to sum the value of one key (qty) if has the same ID of n numbers of arrays inside an array,
in this example I have only 2 arrays but I could have 1000
$a = array(
array(
"id"=>1,
"qty"=>2
),
array(
"id"=>1,
"qty"=>4
)
...n arrays
);
I want the result like this...
$b = array(
array(
"id"=>1,
"qty"=>6
));
I want a function to pass variable $a with n numbers of arrays and returned $b. Thank you
resolved
$result = array();
foreach($array as $k => $v) {
$id = $v['id'];
$result[$id][] = $v['quantity'];
}
$new = array();
foreach($result as $key => $value) {
$new[] = array('id' => $key, 'quanity' => array_sum($value));
}
echo '<pre>';
print_r($new);
?>
I have array from my database and I loop with foreach in Codeigniter Controller
My code like this :
$day_total =31;
$no=1;
foreach ($attendance_2->result_array() as $attend_list) {
foreach ($rows2 as $i){
if ($i = $attend_list['sn']) {
$a = $attend_list['name'];
$b = $attend_list['pst_desc'];
$d = array();
for($m=1; $m <= $day_total; $m++){
$d[]=$m;
}
}
}
$data[] = array (
$no++,
$a,
$b,
$d
);
}
$output = array(
"draw" => $draw,
"recordsTotal" => $attendance_2->num_rows(),
"recordsFiltered" => $attendance_2->num_rows(),
"data" => $data
);
echo json_encode($output);
}
I want to array $d put on store Data[] and looks like below
$data[] = array (
$no++,
$a,
$b,
$d[0],
$d[1],
$d[2],
etc
);
how to do it?
If you want to get straight array then you need to used array_merge.
So change this line
$data[] = array (
$no++,
$a,
$b,
$d
);
To
$temp = array($no++,
$a,
$b);
$data[] = array_merge($temp,$d);
You also need to check empty condition for $a, $b and $d. As in your code, I can see you are getting all values in if condition, So there is no else condition for getting values.
I have 2 arrays a and b which may or may not have similar values.
$a = array('id' => 1, 'name' => 'John Doe', 'age' => 35);
$b = array('name' => 'John Doe', 'age' => 35);
I need to check whether the keys that are there in both the arrays contains same values or not using the array functions itself. Please help me.
NB: Array $a is always the parent array. If any key has to be popped, it would be only from $a.
You can use the following comparison based on array_intersect_assoc:
$b == array_intersect_assoc($a, $b)
This will be true when all of the $b key/value pairs occur in $a, false otherwise.
Use this code:
Use: array_diff_key
Remove duplicate key:
<?php
$a = array('id' => 1, 'name' => 'John Doe', 'age' => 35);
$b = array('name' => 'John Doe', 'age' => 35);
$c = array_diff_key($a, $b);
print_r($c); //Array ( [id] => 1 )
?>
Get duplicate key:
Use: array_intersect_key
<?php
function array_duplicate_keys() {
$arrays = func_get_args();
$count = count($arrays);
$dupes = array();
// Stick all your arrays in $arrays first, then:
for ($i = 0; $i < $count; $i++) {
for ($j = $i+1; $j < $count; $j++) {
$dupes += array_intersect_key($arrays[$i], $arrays[$j]);
}
}
return array_keys($dupes);
}
print_r(array_duplicate_keys($a, $b)); //Array ( [0] => name [1] => age )
?>
Get duplicate key and value:
<?php
function get_keys_for_duplicate_values($my_arr, $clean = false) {
if ($clean) {
return array_unique($my_arr);
}
$dups = $new_arr = array();
foreach ($my_arr as $key => $val) {
if (!isset($new_arr[$val])) {
$new_arr[$val] = $key;
} else {
if (isset($dups[$val])) {
$dups[$val][] = $key;
} else {
$dups[$val] = array($key);
}
}
}
return $dups;
}
print_r(get_keys_for_duplicate_values($a, $b));
//Array ( [id] => 1 [name] => John Doe [age] => 35 )
?>
If $b can have keys not present in $a, you can use array_intersect_key two times, with arguments in reversed order and check if both results are the same:
$ab = array_intersect_key($a, $b);
$ba = array_intersect_key($b, $a);
$allValuesEqual = ($ab == $ba);
use this:"it compare both key and value"
$result=array_diff_assoc($a1,$a2); //<-Compare both key and value, gives new array
example:
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","b"=>"pink");
$result=array_diff_assoc($a1,$a2);
print_r($result);
//result will be
Array ( [b] => green )
$arr1 = [1,2,3];
$arr2 = [1,2,3,4];
$arr3 = [1,2,3,4,5];
echo max( count($arr1), count($arr2), count($arr3) ); // returns 5
with max I do get the count, but don't know which array is larger. How do I get the larger array's reference ($arr3 in this case)?
You should use multidimensional array to store your all arrays then, loop through the multidimensional array and find your largest array,
$lgArraySize = 0; // used for comparing the size of array
$lgArray = array(); // used to store reference of largest array
foreach($arraylist as $array) {
if(count($array) > $largeArraySize) {
$lgArray = &$array;
$lgArraySize = count($array);
}
}
print_r($largeArray);
Here is the one liner code.(that you want)
function findMax( $row ){
return count($row);
}
$maxArrayPos = array_search(max( array_map("findMax", $multi )), array_map("findMax", $multi ));
print_r($multi[$maxArrayPos]);
You can make this as one liner.
It's better to use indexes for such arrays, so that traversal is easy.
You can get reference to the larger array as follows -
$arr;
$arr[0] = [6,2,1,12,32,11];
$arr[1] = [1,2,3,4];
$arr[2] = [1,2,3,4,4,4,1,1,1,1];
$count = -1;//will hold max count
$big;//larger array will be stored here
foreach ($arr as $key => $value) {
$curCount = count($arr[$key]);//curCount holds current array size
//checking for biggest array count
if($curCount>$count){
$count = $curCount;
$big = &$arr[$key];
}
}
var_dump($big);
Maybe the logic is too bulky though!
Is this what you need?
$arr1 = [1,2,3];
$arr2 = [1,2,3,4];
$arr3 = [1,2,3,4,5];
$countmax = [
"arr1" => $arr1,
"arr2" => $arr2,
"arr3" => $arr3,
];
$value = max($countmax);
print_r($value);
Oputput:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Try this :
function laregr(){
$numargs = func_num_args();
$large_array = array();
if($numargs){
$arg_list = func_get_args();
$max = 0;
foreach($arg_list as $arg){
if(is_array($arg)){
$big = count($arg);
if($big >= $max ){
$max = $big;
$large_array = $arg;
}
}
}
}
return $large_array;
}
$arr1 = [1,2,3,4,5,6];
$arr2 = [1,2,3,4,5];
$arr4 = [1,2,3,4, 5,6,7];
$max_array = laregr($arr1, $arr2, $arr4); // large($arr1, ...)
print_r($max_array);
You may like to store all arrays inside an array and would like to do a foreach I think.
function max_length($array) {
$max = 0;
foreach($array as $child) {
if(count($child) > $max) {
$max = count($child);
}
}
return $max;
}
check this why it is better approach .
//I have array like below:
$a =array('1,2,6');
$b =array('2,3,1');
//Then I using ArrayCombine :
$arr_combine = array_combine( $a, $b );
//OUTPUT:
//Array( [1,2,6] => 2,3,1 ) ;
how can I get array like below?
//OUTPUT:
array( 1=>2, 2=>3, 6=>1 );
If you have the array like that, then you have to explode the element.
$result = array_combine(explode(',', $a[0]), explode(',', $b[0]));
It's taking as complete one string due to your present quotes in arrays,
Should be,
$a = array('1','2','6'); // And not '1,2,6'
$b = array('2','3','1');
$arr_combine = array_combine( $a, $b );
DEMO.
And if you can't change the array & have the format like that only see #xdazz answer.
For your second question check like this
<?php
$x = array( 1 => '2', 2 => '3', 6 => '1') ;
$y = array( 1 => '2', 6 => '2' ) ;
$s = array();
foreach($x as $key=>$val)
{
if (array_key_exists($key,$y))
{
$s[$key] = $x[$key] + $y[$key];
}
}
var_dump($s);
?>
Try like this
<?php
$a =array('1,2,6');
$b =array('2,3,1');
$a = explode(',',$a[0]);
$b = explode(',',$b[0]);
var_dump($a);
var_dump($b);
var_dump(array_combine($a,$b));
?>