I have an array like so
$dataArray = array(
array( 20800, 21679, 0 ),
array( 15254, 0, 3726 ),
array( 17426, 2973, 0 ),
array( 4391, 37, 0 ),
array( 39194, 435, 0 )
);
I am creating a sub array from this. Any value greater than 2000 becomes a 1, otherwise it becomes a 0.
foreach ($dataArray as $row => $data) {
foreach ($data as $key => $value) {
if($value > 1999) {
$dataArray[$row][$key] = 1;
} else {
$dataArray[$row][$key] = 0;
}
}
}
This produces the following
$dataArray = array(
array( 1, 1, 0 ),
array( 1, 0, 1 ),
array( 1, 1, 0 ),
array( 1, 0, 0 ),
array( 1, 0, 0 )
);
Now what I am trying to do is produce another array that shows the positions of the 1's within the above array. Each row should be represented by a new array. The output I am looking for is this
$dataArray = array(
array( 1, 2 ),
array( 1, 3 ),
array( 1, 2 ),
array( 1 ),
array( 1 )
);
So I can see that row 1 has a 1 in position 1 and 2, row 2 has a 1 in positions 1 and 3 etc.
I wanted to make use of my current loop, so I am trying something like this
$position = array();
foreach ($dataArray as $row => $data) {
foreach ($data as $key => $value) {
if($value > 1999) {
$position[$row] = $key + 1;
$dataArray[$row][$key] = 1;
} else {
$dataArray[$row][$key] = 0;
}
}
}
But this seems way off according to my output. How can I achieve what I am after?
Thanks
<?php
$dataArray = array(
array( 20800, 21679, 0 ),
array( 15254, 0, 3726 ),
array( 17426, 2973, 0 ),
array( 4391, 37, 0 ),
array( 39194, 435, 0 )
);
$endResult = [];
foreach ($dataArray as $key => $value) {
foreach ($value as $subkey => $subvalue) {
if ($subvalue >= 2000) {
$endResult[$key][] = $subkey +1;
}
}
}
print_r($endResult);
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 1
[1] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 1
)
)
You could use array_map along with array_keys:
$dataArray = [
[1, 1, 0],
[1, 0, 1],
[1, 1, 0],
[1, 0, 0],
[1, 0, 0]
];
$onePositions = array_map(function($row) {
return array_map(function($position) { return $position + 1; }, array_keys($row, 1));
}, $dataArray);
echo '<pre>'; var_dump($onePositions); echo '</pre>';
Demo here
You can make use of the second parameter in the array_keys() function to get the positions after you populate the ones and zeros:
$position = array();
$binary = array();
foreach ($dataArray as $row => $data) {
// if you want to have the positions start at 1 instead of 0
// add a dummy value to the final array
$binary[$row][] = 0;
foreach ($data as $key => $value) {
if($value > 1999) {
$binary[$row][] = 1;
} else {
$binary[$row][] = 0;
}
}
$positions[] = array_keys($binary[$row], 1);
}
Demo Here
Related
I am trying to create one array out of the two. Two arrays may be different lengths therefore the combine result should accommodate that and fill gaps with null.
My understanding is to first find a bigger array and loop over that, fill the gaps in the smaller array, and once that is done, merge it.
This is what I have done so far, but it feels very clunky, to the point I am starting to think - there must be a better way - less loops and maybe use some of the php array helpers methods ?
<?php
$result_keys = [];
$result_data = [];
$bigger = null;
$smaller = null;
$array1 = [
[
'dog' => 2,
'cat' => 3,
],
[
'dog' => 4,
'cat' => 2,
],
[
'dog' => 2,
'cat' => 3
]
];
$array2 = [
[
'bird' => 7,
],
[
'bird' => 5
]
];
// find which array is bigger
if (count($array1) >= count($array2)) {
$bigger = $array1;
$smaller = $array2;
} else {
$bigger = $array2;
$smaller = $array1;
};
// loop over bigger array
foreach ($bigger as $i => $record) {
foreach ($record as $key => $value) {
if ($i === 0) {
$result_keys[] = $key;
};
$result_data[$i][] = $value;
}
// fill gaps in smaller array
if (!isset($smaller[$i])) {
foreach ($smaller[$i-1] as $key => $value) {
$smaller[$i][$key] = null;
}
}
};
// loop over smaller array
foreach ($smaller as $i => $record) {
foreach ($record as $key => $value) {
if ($i === 0) {
$result_keys[] = $key;
};
$result_data[$i][] = $value;
}
};
var_dump($result_keys);
var_dump($result_data);
// // expected result
// $result_keys = ['dog', 'cat', 'bird'];
// $result_data = [
// [2,3,7],
// [4,2,5],
// [2,3,null]
// ];
My solution to your problem:
$array1 = [
[
'dog' => 2,
'cat' => 3,
],
[
'dog' => 4,
'cat' => 2,
],
[
'dog' => 2,
'cat' => 3
]
];
$array2 = [
[
'bird' => 7,
],
[
'bird' => 5
]
];
// get the number of values of the biggest array
$count = $count = count($array1) >= count($array2) ? count($array1) : count($array2);
$values = [];
$keys = [];
for ($i = 0; $i < $count; $i++) {
$a1 = $array1[$i] ?? [];
$a2 = $array2[$i] ?? [];
$keys = array_unique(array_merge($keys, array_keys($a1), array_keys($a2)));
$values[] = array_values(array_merge(array_fill_keys($keys, null), $a1, $a2));
}
print_r($keys);
print_r($values);
Results (test here):
Array
(
[0] => dog
[1] => cat
[2] => bird
)
Array
(
[0] => Array
(
[0] => 2
[1] => 3
[2] => 7
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[2] => Array
(
[0] => 2
[1] => 3
[2] =>
)
)
I want to check each subarray for item2 as the value in the first element (index 0).
If so, I want to return the second element's value (index 1).
This is my multidimensional array:
$arr = Array
(
1 => Array
(
0 => 'item',
1 => 3,
2 => 20,
),
2 => Array
(
0 => 'item2',
1 => 1,
2 => 21,
),
7 => Array
(
0 => 'item3',
1 => 4,
2 => 26,
),
20 => Array
(
0 => 'item4',
1 => 1,
2 => 39,
),
22 => Array
(
0 => 'item1',
1 => 10,
2 => 39,
),
23 => Array
(
0 => 'item2',
1 => 11,
2 => 39,
)
);
The expected result is: [1,11]
I used this function but it doesn't return all of the results:
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
Of course, use can not have multiple array elements with same key (item2=>1,item2=>11) , but you can collect all values [1] (1,11)
For example
$arr = // your array()
$res = array();
foreach ($arr as $row) {
if (isset($row[0]) && $row[0] == 'item2') {
$res[] = $row[1];
}
}
var_dump($res);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to sum array in same key and different value.
Array
(
0 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
1 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 1
),
2 => Array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
),
3 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
4 => Array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
)
);
I want Output it
Array
(
0 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 5
),
1 => Array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 4
)
);
Thanks.
You could browse your array while creating a new one with keys like "locker_id-locker_model", then you can easily check if your locker exists in this array with array_key_exists function.
$inputArray = array( ... ); // this is your message array
$outputArray = array();
foreach( $inputArray as $locker )
{
if( !array_key_exists( $locker["locker_id"]."-".$locker["locker_model"], $outputArray ) )
{
$outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ] = array(
"locker_id" => $locker["locker_id"],
"locker_model" => $locker["locker_model"],
"qty" => 0
);
}
$outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ]["qty"]++;
}
var_dump( $outputArray );
$dataArr = array
(
0 => array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
1 => array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 1
),
2 => array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
),
3 => array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
4 => array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
)
);
$sumArr = array();
if(count($dataArr)>0){
foreach($dataArr as $data){
if(!isset($sumArr[$data['locker_id']])){
$sumArr[$data['locker_id']] = $data;
}
$sumArr[$data['locker_id']]['qty'] +=$data['qty'];
}
}
echo "<pre>";print_r($sumArr);
Considering $array1 is your first array,
$array2 = array();
foreach($array1 as $k=>$v) {
if(!isset($array2[$v['locker_id']])) {
$array2[$v['locker_id']] = $v;
} else {
$array2[$v['locker_id']]['qty'] += $v['qty'];
}
}
print_r($array2);
Try this
$arr is your array
$j = 0;
$new = array();
$new[0][qty];
for($i=0;$i<arraylength;$i++){
foreach($arr as $key => $value) {
if($arr[$i][lockerid] == $arr[$key][lockerid]) {
$new[$j][lockerid] = $arr[$key][lockerid];
$new[$j][lockermodel] = $arr[$key][lockermodel];
$new[$j][qty] = $new[$i][qty] + $arr[$key][qty];
J++;
}
}
}
$result = array();
foreach($array as $key => $value){
if(isset($value['locker_id'])){
$result[$value['locker_id']]['qty'] += $value['qty'];
}
else{
$result[$value['locker_id']] = $value;
}
}
You have to loop over your array and save it to another let's say $result. In $result you should put the locker_id as key and then you only have to verify if that key exists. If it is you add the qty value, if isn't you have to add the entire new item.
try this
$ARR_OUTPUT = array();
// $arr will contains your input array
foreach($arr as $arr_val)
{
$locker_id = $arr_val['locker_id'];
$locker_model = $arr_val['locker_id'];
$qty = $arr_val['locker_id'];
if(isset($ARR_OUTPUT[$locker_id][$locker_model]))
{
$ARR_OUTPUT[$locker_id][$locker_model] += $qty;
}
else
{
$ARR_OUTPUT[$locker_id][$locker_model] = $qty;
}
}
echo "<pre>";
print_r($ARR_OUTPUT);
$ARR_OUTPUT2 = array();
foreach($ARR_OUTPUT as $locker_id=>$arr_values)
{
foreach($arr_values as $locker_model=>$qty)
{
$arr_temp['locker_id'] = $locker_id;
$arr_temp['locker_model'] = $locker_model;
$arr_temp['qty'] = $qty;
$ARR_OUTPUT2[] = $arr_temp;
}
}
echo "<pre>";
print_r($ARR_OUTPUT2);
I have an array which contains following values.
array(
'dates' => array(
(int) 0 => '2013-04-22',
(int) 1 => '2013-04-23',
),
'publisherName' => array(
(int) 0 => 'Comp1',
(int) 1 => 'Comp2',
),
'loaded' => array(
(int) 0 => (int) 2189,
(int) 1 => (int) 37,
),
'clicks' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
),
'ctr' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
)
)
What I want to produce is getting company based data on different dates like the array below.
How am I able to create an array which is like;
array (
'2013-04-22'=>array(
'publisherName'=>'Comp1',
'loaded'=>2189,
'clicks'=>0,
'ctr'=>0),
'2013-04-23'=>array(
'publisherName'=>'Comp2',
'loaded'=>37,
'clicks'=>0,
'ctr'=>0)
...
)
Which contains daily stats but which comes from publishername field.
Any ideas?
Here's an example that doesn't rely on any keys, the only requirement is that date is the first array in your original array:
// $array is your original array
$dates = array_shift($array);
$output = array();
foreach ($array as $k => $a) {
foreach ($a as $i => $v) {
$output[$i][$k] = $v;
}
}
$output = array_combine($dates, $output);
Let the initial array be $a and the desired array $b;
Code:
$b = array();
$count = 0;
foreach($a['dates'] as $date) {
$b[$date] = array(
'name' => $a['publisherName'][$count],
'loaded'=> $a['loaded'][$count],
'clicks'=> $a['clicks'][$count],
'ctr'=> $a['ctr'][$count]
);
$count++;
}
Result:
Array
(
[2013-04-22] => Array
(
[name] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[name] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
<?php
$data = $yourarray;
$new = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($new);
?>
$newArr = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($newArr);
$new_arr = your array;
$finalArray = array();
foreach($new_arr['dates'] as $key => $value){
$finalArray[$value] = array(
'publisherName'=>$new_arr['publisherName'][$key],
'loaded'=>$new_arr['loaded'][$key],
'clicks'=>$new_arr['clicks'][$key],
'ctr'=>$new_arr['ctr'][$key]
);
}
Output :
Array
(
[2013-04-22] => Array
(
[publisherName] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[publisherName] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
I have an array $MyArray which has some elements which are also array (lets call them subarrays). I want to know how many elements the subarray with the most elements has. The problem is, that I don't know if the index exists:
max(
#count($MyArray[$i*7]),
#count($MyArray[$i*7+1]),
#count($MyArray[$i*7+2]),
#count($MyArray[$i*7+3]),
#count($MyArray[$i*7+4]),
#count($MyArray[$i*7+5]),
#count($MyArray[$i*7+6])
);
Struckture of $MyArray:
Array(
12 => array (
0 => array ( 0 => 0, 1 => 1, ),
1 => array ( 0 => 13, 1 => 1, ),
2 => array ( 0 => 15, 1 => 1, ),
3 => array ( 0 => 20, 1 => 1, ),
4 => array ( 0 => 69, 1 => 1, )
),
5 => array (
0 => array ( 0 => 55, 1 => 1, ),
1 => array ( 0 => 32, 1 => 1, ),
2 => array ( 0 => 12, 1 => 1, ),
3 => array ( 0 => 21, 1 => 5, )
),
....
)
Can this be done better (faster)?
edit: I know foreach and I don't want to loop over every element in this array. I just want an interval of it. # is used, because I don't know if $MyArray[$i*7 + x] is Null or an array.
$i is a element of [0, 1, 2, 3, 4] (sometimes also 5)
$biggest = 0;
foreach ($MyArray as $value) {
if ($biggest < count($value)) {
$biggest = count($value);
}
}
I see, you want the size of the biggest array in the array, correct?
Simple and old school approach:
<?php
$max = -1;
foreach($MyArray as $subarray)
{
$items = count($subarray);
if($items > $max)
$max = $items;
}
echo $max;
?>
This works best since you only want to know how many elements the subarray with the most elements has.
$max = 0;
foreach ($MyArray as $value) {
$max = max($max,count($value));
}
Try this:
$arr = array();
for ($j=0;$j<=6;$j++) {
if (isset($MyArray[$i*7+$j])) $arr[] = count($MyArray[$i*7+$j]);
}
$result = max($arr);
I don't know exactly what $i refers to though...
// get the interesting part of the array
$chunk = array_intersect_key($input_array, array_flip(range($i*7, $i*7+6)));
// max(count)
$max = $chunk ? max(array_map('count', $chunk)) : 0;