The output of the first array as the value for the update in the third array
Array
(
[0] => Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 1
[3] => 1
[4] => 1
[5] => 1
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => 0
[3] => 0
[4] => 0
[5] => 1
)
)
The output of the second array as a key for updating the third array
Array
(
[0] => Array
(
[0] => 4
)
[1] => Array
(
[0] => 5
)
[2] => Array
(
[0] => 6
)
[3] => Array
(
[0] => 7
)
[4] => Array
(
[0] => 8
)
[5] => Array
(
[0] => 9
)
This is the third array with 48 keys
Array
(
[1] => Array
(
[1] => 1
)
[2] => Array
(
[1] => 1
)
[3] => Array
(
[1] => 1
)
[4] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => Array
(
[4] => 1
)
[11] => Array
(
[4] => 1
)
[12] => Array
(
[4] => 1
)
[14] => Array
(
[5] => 1
)
[15] => Array
(
[5] => 1
)
[17] => Array
(
[6] => 1
)
[18] => Array
(
[6] => 1
)
[20] => Array
(
[7] => 1
)
[21] => Array
(
[7] => 1
)
[23] => Array
(
[8] => 1
)
[24] => Array
(
[8] => 1
)
[27] => Array
(
[9] => 1
)
[29] => Array
(
[10] => 1
)
[30] => Array
(
[10] => 1
)
[32] => Array
(
[11] => 1
)
[33] => Array
(
[11] => 1
)
[34] => Array
(
[12] => 1
)
[36] => Array
(
[12] => 1
)
[38] => Array
(
[13] => 1
)
[39] => Array
(
[13] => 1
)
[42] => Array
(
[14] => 1
)
[44] => Array
(
[15] => 1
)
[45] => Array
(
[15] => 1
)
[46] => Array
(
[16] => 1
)
[47] => Array
(
[16] => 1
)
[48] => Array
(
[16] => 1
)
The problem is that I can not use one as a key and the other as a value to update the third array
Code I have tried :
foreach($arraykey as $key => $value){
for ($i=0;$i < count($array3) ; $i++){
if ($value2==$i){
$array3[$value2]=$arrayval[0][0][$i];
}
}
}
You can use array_combine(), for example like this:
<?php
$a = [1,2,3,4];
$b = [4,5,6,7];
$c = array_combine($b,$a);
print_r($c);
?>
result:
Array (
[4] => 1
[5] => 2
[6] => 3
[7] => 4 )
I have this php array:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 1
)
[1] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[2] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 0
[10] => 0
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[4] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 0
)
)
I want i new array:
1) if element [0] exists in the array I want to control elements 8, 9 and 10 and if one of this is 1 I want to have 1 in the final array, but I don't want to have the same array 2 times (the index [0] is the key).
My final array shound be:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 1
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 0
)
)
I have only one time the index 2505 and 2505 and index 8 and 9 and 10 are 1 for the 2505 and the index 8 and 9 are 1 for the 2525.
this does the job: (assuming that your first array is $arrays and your desired result is $result)
$result = array();
foreach($arrays as $array)
{
if(!isset($result[$array[0]]))
{
$result[$array[0]] = $array;
}
else
{
$result[$array[0]][8] = $array[8] == 1 ? 1 : $result[$array[0]][8];
$result[$array[0]][9] = $array[9] == 1 ? 1 : $result[$array[0]][9];
$result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10];
}
}
but wherever you get your data from, e.g. a database, I would use element[0] as the key, so you don't get multiple entries in $arrays. Another option would be to create a class which holds your data...
I am working with multi dimensional array is like below with php,
$return_array= Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 19
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 42
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 7
)
[3] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
On 0th and 2nd indexs -> from sub array of that indexes -> 0th index are common "3_Mar_2017" ,I want to sum that two indexes and want result as shown below,
$final_return = Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 26
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 49
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
My tried code with loop as below,
$tem_array = array();
$final_return = array();
foreach ($return_array as $unique) {
if (!in_array($unique[0], $tem_array)) {
array_push($tem_array, $unique[0]);
$final_return[] = $unique;
} else {
$index = array_search($unique[0], $tem_array);
for ($i = 1; $i < count($unique); $i++) {
$final_return[$index][$i] = $final_return[$index][$i] + $unique[$i];
}
}
}
but if array size will large then ,may be it will take time is there any simple solution.
can any person help me to get this required result with minimum code ?
I will appreciate best answer.
hope this is what you are looking for
$temp1 = array(); $result = array();
foreach ($myArray as $temp) {
if (!in_array($temp[0], $temp1)) {
array_push($temp1, $temp[0]); $result[] = $temp;
} else {
$id = array_search($temp[0], $temp1); for ($i = 1; $i <= count($temp); $i++) {
$result[$id][$i] = $result[$id][$i] + $temp[$i];
}
}
}
your first array would look like
Array
(
[0] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
[2] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
)
and the result be like
Array
(
[0] => Array
(
[0] => 13
[1] => 2
[2] => 4
[3] => 6
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
)
I have unknown depth of array but i want to make simple two dimensional array as following , earlier i think my question doesn't make exact sense so i edited this , please help
private function arrayDepth($a) {
$arr = array();
foreach ($a as $key => $val) {
if (is_array($val)) {
$this->arrayDepth($val);
}
else {
$arr[] = $a;
}
}
}
my current array is
Array
(
[0] => Array
(
[0] => Array
(
[0] => 41
[uid] => 41
[1] => 16
[pid] => 16
[2] => 30
[oid] => 30
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:41:31
[updateDate] => 2014-05-26 16:41:31
)
[1] => Array
(
[0] => Array
(
[0] => 42
[uid] => 42
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:45:49
[updateDate] => 2014-05-26 16:45:49
)
[1] => Array
(
[0] => 44
[uid] => 44
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:42:01
[updateDate] => 2014-05-26 16:42:01
)
[2] => Array
(
[0] => 47
[uid] => 47
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:45:19
[updateDate] => 2014-05-26 16:45:19
)
[3] => Array
(
[0] => Array
(
[0] => 51
[uid] => 51
[1] => 16
[pid] => 16
[2] => 32
[oid] => 32
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:47:27
[updateDate] => 2014-05-26 16:47:27
)
)
)
)
)
i need my output like all array which contain some value into a single array like
array(
[0] => Array
(
[0] => 41
[uid] => 41
[1] => 16
[pid] => 16
[2] => 30
[oid] => 30
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:41:31
[updateDate] => 2014-05-26 16:41:31
)
[1] => Array
(
[0] => 42
[uid] => 42
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:45:49
[updateDate] => 2014-05-26 16:45:49
)
[2] => Array
(
[0] => 44
[uid] => 44
[1] => 16
[pid] => 16
[2] => 31
[oid] => 31
[3] => 1
[value] => 1
[4] => 0
[optval] => 0
[5] => 2014-05-26 16:42:01
[updateDate] => 2014-05-26 16:42:01
) ...... and so on .....
so finally i solved this issue, if you guys have any better solution please provide
function arrayDepth($a,&$arr) {
foreach ($a as $key => $val) {
if(is_array($val))
if (is_array(#$val[0])) {
arrayDepth($val,$arr);
}
else {
$arr[] = $val;
}
}
}
arrayDepth($aNonFlat,$arr);
var_dump($arr);
My array :
<?php
$hoppa = array
(
"0" => array
("0","0","0","0","0","0","0","0","0","0"),
"1" => array
("0","0","0","0","0","0","0","0","0","0"),
"2" => array
("1","0","0","1","0","0","0","0","0","0"),
"3" => array
("1","0","0","1","0","1","1","1","1","0"),
"4" => array
("1","1","1","1","0","0","0","0","1","0"),
"5" => array
("1","0","0","1","0","1","1","1","1","0"),
"6" => array
("1","0","0","1","0","1","0","0","1","0"),
"7" => array
("1","0","0","1","0","1","1","1","1","0"),
"8" => array
("0","0","0","0","0","0","0","0","0","0"),
"9" => array
("0","0","0","0","0","0","0","0","0","0")
);
?>
My array's output:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[2] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[3] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[5] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[6] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[7] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[8] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[9] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
)
it will give 0 (zero) as result
echo $hoppa[1][1];
it will give 1 (one) as result
echo $hoppa[2][0];
I dont want 0 (zero) values to show in the results. I want php script to return only 1 as results. Which functions should use? or can you give me a sample?
Use:
foreach($hoppa as $k => $v) {
$hoppa[$k] = array_filter($v);
}
This results in:
php > print_r($hoppa);
Array
(
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
[0] => 1
[3] => 1
)
[3] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[8] => 1
)
[5] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[6] => Array
(
[0] => 1
[3] => 1
[5] => 1
[8] => 1
)
[7] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[8] => Array
(
)
[9] => Array
(
)
)