I have 2 arrays to compare their indexes, how can i compare them by checking the empty indexes of first array and if so, then change the value of second array of same index.
it can be solved as 2D array by comparing the rows indexes.
array (size=5)
0 => string '' (length=0)
1 => string '10' (length=6)
2 => string '' (length=0)
3 => string '11' (length=3)
4 => string '' (length=0)
array (size=5)
0 => string '1' (length=4)
1 => string '2' (length=11)
2 => string '3' (length=11)
3 => string '4' (length=11)
4 => string '5' (length=10)
For instance, index 0 of first array is empty, so index 0 of second array incremented by 1. and so on
try this
<?php
$a=array('',10,11,'');
$b=array(1,2,3,4,5);
foreach($a as $k=>$v){
if($v==''){
$b[$k]=++$b[$k] ;
}
}
print_r($b);
?>
output
Array ( [0] => 2 [1] => 2 [2] => 3 [3] => 5 [4] => 5 )
Related
I have two arrays in PHP with a number of equal indices like this:
ARRAY 1 :
array (size=3)
0 =>
array (size=8)
0 => int 1
1 => int 17
2 => int 145
3 => string 'one' (length=3)
4 => string '#3546b140' (length=9)
5 => string '5' (length=1)
6 => string 'ONE' (length=3)
7 => string 'ONE' (length=3)
1 =>
array (size=8)
0 => int 2
1 => int 30
2 => int 224
3 => string 'two' (length=3)
4 => string '#3546b140' (length=9)
5 => string '6' (length=1)
6 => string 'TWO' (length=3)
7 => string 'TWO' (length=3)
2 =>
array (size=8)
0 => int 3
1 => int 31
2 => int 120
3 => string 'thr' (length=3)
4 => string '#3546b140' (length=9)
5 => string '7' (length=1)
6 => string 'THR' (length=3)
7 => string 'THR' (length=3)
This is the second array
array (size=3)
1 => int 1761693
2 => int 8911775
3 => int 3510858
This would be the desired result, look at the eighth position of each index, any ideas?
array (size=3)
0 =>
array (size=8)
0 => int 1
1 => int 17
2 => int 145
3 => string 'one' (length=3)
4 => string '#3546b140' (length=9)
5 => string '5' (length=1)
6 => string 'ONE' (length=3)
7 => string 'ONE' (length=3)
8 => int 1761693
1 =>
array (size=8)
0 => int 2
1 => int 30
2 => int 224
3 => string 'two' (length=3)
4 => string '#3546b140' (length=9)
5 => string '6' (length=1)
6 => string 'TWO' (length=3)
7 => string 'TWO' (length=3)
8 => int 8911775
2 =>
array (size=8)
0 => int 3
1 => int 31
2 => int 120
3 => string 'thr' (length=3)
4 => string '#3546b140' (length=9)
5 => string '7' (length=1)
6 => string 'THR' (length=3)
7 => string 'THR' (length=3)
8 => int 3510858
I have tried to do this but it does not work!!!
array_merge($array1,$array2);
and this
array_push($array1, $array2);
Iterating with a foreach but I can't position correctly the arrays by indices, any idea?
Loop through the first array and add the value from the second:
foreach ($array1 as $index => $subArray) {
// We use $index + 1 since the first starts with 0 and the second with 1
// We're also using $array1 since $subArray is just a copy so adding to that
// won't change the original array
$array1[$index][] = $array2[$index + 1];
}
This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed last month.
I need to convert some arrays into associative array. I need the values to be saved together.
'days1' =>
array (size=5)
0 => string '30' (length=2)
1 => string '90' (length=2)
2 => string '50' (length=2)
3 => string '50' (length=2)
4 => string '5' (length=1)
'price1' =>
array (size=5)
0 => string '5' (length=1)
1 => string '10' (length=2)
2 => string '12' (length=2)
3 => string '13' (length=2)
4 => string '1' (length=1)
'days2' =>
array (size=3)
0 => string '30' (length=2)
1 => string '90' (length=2)
2 => string '180' (length=3)
'price2' =>
array (size=3)
0 => string '6' (length=1)
1 => string '7' (length=1)
2 => string '10' (length=2)
So days1 needs to merge with price1 and create something like this
array(
30 => 5,
90 =>10,
50 => 12,
50 => 13,
5 => 1
)
Use array_combine -
array_combine ( array $keys , array $values ) -
More Info at : http://php.net/manual/en/function.array-combine.php
Btw this is not multidimensional array, just associative.
I got a array of array in PHP that look like this :
array (size=3)
0 =>
array (size=3)
0 => string 'abc' (length=3)
1 => string 'def' (length=3)
2 => string 'ghi' (length=3)
1 =>
array (size=3)
0 => string '01234' (length=5)
1 => string '01234' (length=5)
2 => string '01234' (length=5)
2 =>
array (size=3)
0 => string '98765' (length=5)
1 => string '98765' (length=5)
2 => string '98765' (length=5)
Now I want the first array to be the key of a assosiative array for the rest of the parent array, or kind :
array (size=2)
0 =>
array (size=3)
'abc' => string '01234' (length=5)
'def' => string '01234' (length=5)
'ghi' => string '01234' (length=5)
1 =>
array (size=3)
'abc' => string '98765' (length=5)
'def' => string '98765' (length=5)
'ghi' => string '98765' (length=5)
EDIT: But I can only get the first array like this to define the header :
$header = reset($tabOfTabs);
You can try this -
$indexes = array_shift($your_array); // pop out the first array to set the indexes
foreach($your_array as $key => $array) {
$your_array[$key] = array_combine($indexes, $array); // combine the keys & sub-arrays
}
Demo
I have var_dump() variable $fruit_array as below:
array (size=3)
0 =>
array (size=1)
'fruit_id' => string '2' (length=1)
1 =>
array (size=1)
'fruit_id' => string '1' (length=1)
2 =>
array (size=1)
'fruit_id' => string '3' (length=1)
I need to rename fruit_id to id and convert array string value to integer, example result:
array (size=3)
0 =>
array (size=1)
'id' => int 2
1 =>
array (size=1)
'id' => int 1
2 =>
array (size=1)
'id' => int 3
How can I do that ? thanks
You can do something like this:
<?php
foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = intval( $array[$k] ['fruit_id'] );
unset($array[$k]['fruit_id']);
}
I'm really stuck at a certain point in my project. I'm trying to convert a two-dimensional array into a three-dimensional array. Currently I´m at this point:
array (size=4)
0 =>
array (size=4)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
1 =>
array (size=4)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
2 =>
array (size=4)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
3 =>
array (size=4)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
What I would like to do is make a new array with key values of array[ ][3] (I hope this makes any sense) so something like this:
array (size=2)
0 => array (size=2)
0 => array (size=2)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => array (size=2)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
Right now this is the best way I can explain it, I'm not sure if I make any sense or if this is even possible, but if anyone could shed a light on this it would be much appreciated!
Thanks in advance,
Thomas
$array = array();
$array[] = array('foo','foo','foo',1);
$array[] = array('a','a','a',1);
$array[] = array('b','a','a',0);
$array[] = array('c','a','a',0);
$new_array =group_by_sub_array_key($array,3);
print_r($new_array);
function group_by_sub_array_key($array,$kn){
$new_array = array();
foreach($array AS $v){
$key=$v[$kn];
if(!array_key_exists($v[$kn],$new_array)){
$new_array[$key]=array();
}
unset($v[$kn]);
$new_array[$key][] = $v;
}
return $new_array;
}