Say I got this array:
array([0] => 0 [1] => 0 [2] => 1), how can I put this as the value of the nth key of another array, making
$array = array(
[0] => array([0] => 0 [1] => 0 [2] => 1)
)
in this example it is the 0th key of array $array
I had browsed with PHP array functions, but I got tired looking for a function that does the same thing
EDIT.
Doing: $array[0] = array(0, 0, 1); works great for first loop, but if I would like to add another array as subarray, say array(1, 1, 1), my output should be
$array = array(
[0] => array([0] => 0 [1] => 0 [2] => 1 [3] => 1 [4] => 1 [5] => 1)
)
Note that the new array, was added at the end of the last subarray.
Please help! thanks!
EDIT
$x=0;
while($x<count($WOE_CONTROL)) {
$j=0;
while($j<=30) {
if ($WOE_CONTROL[$x+3]&(1<<$j)) {
echo '<br />';
echo '<strong>'.$Castles[$j].'</strong>';
$castle_data_holder[0] = $WOE_CONTROL[$x];
$castle_data_holder[1] = $WOE_CONTROL[$x+1];
$castle_data_holder[2] = $WOE_CONTROL[$x+2];
$castle_db[$j] = $castle_data_holder;
unset ($castle_data_holder);
}
if ($x+4 < count($WOE_CONTROL)) {echo " ";}
$j=$j+1;
}
$x=$x+4;
}
Sorry, there, I am trying to extract all the data in $WOE_CONTROL which actually contains binary data, then store them temporarily to $castle_data_holder then finally, add it to $castle_db
Just set the value of $array[0] to the array. This produces a multidimensional array.
$array[0] = array(0, 0, 1);
Is this u looking for?
$array[] = array(0,0,1);
$array[] = array(1,1,1);
$array[] = array(2,2,2);
// etc...
print_r($array);
EDIT
$array[]['a1'] = array(1,1,1);
$array[]['a2'] = array(2,2,2);
$array[]['a3'] = array(3,3,3);
// etc...
print_r($array);
Finally this is u need
$a = array(1,1,1);
$b = array(2,2,2);
$c = array(3,3,3);
$value = array_merge($a,$b,$c);
print_r($value);
EDIT
$newArray = array();
foreach($yourArray as $key => $value)
{
// I say if $value is an array that u want to merge
$arrayToMerge = $value;
$newArray = array_merge($newArray,$arrayToMerge);
}
print_r($newArray);
This solved my problem:
if (empty($castle_db[$j])) {
$castle_db[$j] = $castle_data_holder;
}
else {
$castle_db[$j] = array_merge($castle_db[$j],$castle_data_holder);
}
it is going to check if the array is empty if not use merge.
Thanks everyone!
Related
$arrayinput = array("a", "b", "c", "d", "e");
How can I achieve the following output....
output:
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => b
[1] => c
)
[2] => Array
(
[0] => c
[1] => d
)
[3] => Array
(
[0] => d
[1] => e
)
[4] => Array
(
[0] => e
)
)
You can use this, live demo here.
<?php
$arrayinput = array("a","b","c","d","e");
$array = [];
foreach($arrayinput as $v)
{
$arr = [];
$arr[] = $v;
if($next = next($arrayinput))
$arr[] = $next;
$array[] = $arr;
}
print_r($array);
live example here: http://sandbox.onlinephpfunctions.com/code/4de9dda457de92abdee6b4aec83b3ccff680334e
$arrayinput = array("a","b","c","d","e");
$result = [];
for ($x = 0; $x < count($arrayinput); $x+=2 ) {
$tmp = [];
$tmp[] = $arrayinput[$x];
if ($x+1 < count($arrayinput)) $tmp[] = $arrayinput[$x+1];
$result[] = $tmp;
}
var_dump($result);
By declaring single-use reference variables, you don't need to call next() -- which is not falsey-safe and there is a warning in the php manual. You also won't need to keep track of the previous index or make iterated calls of count().
For all iterations except for the first one (because there is no previous value), push the current value into the previous subarray as the second element. After pushing the second value, "disconnect" the reference variable so that the same process can be repeated on subsequent iterations. This is how I'd do it in my own project.
Code: (Demo)
$result = [];
foreach (range('a', 'e') as $value) {
if ($result) {
$row[] = $value;
unset($row);
}
$row = [$value];
$result[] = &$row;
}
var_export($result);
If you always want to have 2 elements in each subarray and you want to pad with null on the final iteration, you could use a transposing technique and send a copy of the input array (less the first element) as an additional argument. This is a much more concise technique (one-liner) (Demo)
var_export(array_map(null, $array, array_slice($array, 1)));
I want to merge the keys of array based on values. This is my array.
Array
(
[1] => 1
[2] => 1
[3] => 1
[4] => 2
[5] => 0
[6] => 2
[7] => 2
)
I want output as
Array
(
[1,2,3] => 1
[4,6,7] => 2
[5] => 0
)
I have been brain storming entire day but couldn't find a solution. Any hint would be much appreciated.
WHAT I HAVE TRIED:
for($i=2;$i<=count($new);$i++){
if ($new[$i-1][1]==$new[$i][1]){
$same .= $new[$i-1][0].$new[$i][0];
}
}
echo $same;
But I am stucked. I am comparing the keys one by one but it's very complicated. I don't need the code. I only need the hint or logic. Anyone kind enough?
<?php
$old_arr = ["1"=>1,"2"=>1,"3"=>1,"4"=>2,"5"=>0,"6"=>2,"7"=>2];
$tmp = array();
foreach($old_arr as $key=>$value)
{
if(in_array($value, $tmp)){
$index = array_search($value, $tmp);
unset($tmp[$index]);
$tmp[$index.",".$key] = $value;
}else{
$tmp[$key] = $value;
}
}
ksort($tmp);
echo "<pre>";
print_r($tmp);
echo "</pre>";
?>
https://eval.in/529314
You can loop through array elements and create a new array with new structure. Please check the below code it may help you
$old_array = array(1=> 1,2 => 1,
3=> 1,
4 => 2,
5 => 0,
6 => 2,
7 => 2
);
$new_array = array();
foreach($old_array as $key => $value)
{
if(in_array($value,$new_array))
{
$key_new = array_search($value, $new_array);//to get the key of element
unset($new_array[$key_new]); //remove the element
$key_new = $key_new.','.$key; //updating the key
$new_array[$key_new] = $value; //inserting new element to the key
}
else
{
$new_array[$key] = $value;
}
}
print_r($new_array);
$arr = array(1 => 1, 2 => 1, 3 => 1, 4 => 2, 5 => 0, 6 => 2, 7 => 2);
$tmp = array();
foreach ($arr as $key => $val)
$tmp[$val][] = $key;
$new = array();
foreach ($tmp as $key => $val)
$new[implode(',', $val)] = $key;
First loop the original array through, creating a temporary array, where your original values are keys and values are the original keys as an array.
Then loop the temporary array, creating the new array, where the temporary array's values are imploded as keys.
There's no way to have an array of keys to a single value, but the other way around:
function flipWithKeyArray($arr){
$result = array();
foreach($arr as $key => $val){
if(!isset($result[$val]))
$result[$val] = array();
$result[$val][] = $key;
}
return $result;
}
This will flip your array and declare one array per value of your old array and then push the keys with the same value into each list.
For an array like this:
array(1=>1, 2=>1, 3=>1, 4=>2, 5=>2, 6=>2)
The result will look like this:
array(1=>array(1,2,3), 2=>array(4,5,6))
Hope it fits your need.
For example I have like more than 3 different arrays, with element like below:
1st array
hello-1
hi-1
2nd array
ok-two
hi-2
22-two
hello
3rd array
hi-3rd
hello3
And so on...
I want to combine this array in the order one by one. For example the expected output for the 3 arrays above would be:
hello-1
ok-two
hi-3rd
hi-1
hi-2
hello3
22-two
hello
I tried array_merge(). But it appends the 2nd array after the complete 1st array, which is not what I'm looking for, so here I'm kinda stuck and don't know which functions I can use here. Any hints or ideas?
This should work for you:
First I get the first element of each array into a sub array, then the second value into the next sub array and so on, that you get this structure of array:
Array
(
[0] => Array
(
[0] => hello-1
[1] => ok-two
[2] => hi-3rd
)
//...
)
After this you can just loop through each array value with array_walk_recursive() and get every value into your array.
<?php
$arr1 = [
"hello-1",
"hi-1",
];
$arr2 = [
"ok-two",
"hi-2",
"22-two",
"hello",
];
$arr3 = [
"hi-3rd",
"hello3",
];
$arr = call_user_func_array("array_map", [NULL, $arr1, $arr2, $arr3]);
$result = [];
array_walk_recursive($arr, function($v)use(&$result){
if(!is_null($v))
$result[] = $v;
});
print_r($result);
?>
output:
Array
(
[0] => hello-1
[1] => ok-two
[2] => hi-3rd
[3] => hi-1
[4] => hi-2
[5] => hello3
[6] => 22-two
[7] => hello
)
I have another way to solve this issue
<?php
$arr1 = array(
"hello-1",
"hi-1");
$arr2 = array("ok-two",
"hi-2",
"22-two",
"hello");
$arr3 = array(
"hi-3rd",
"hello3");
$max = count($arr1);
$max = count($arr2) > $max ? count($arr2) : $max;
$max = count($arr3) > $max ? count($arr3) : $max;
$result = array();
for ($i = 0; $i < $max; $i++) {
if (isset($arr1[$i])) {
$result[] = $arr1[$i];
}
if (isset($arr2[$i])) {
$result[] = $arr2[$i];
}
if (isset($arr3[$i])) {
$result[] = $arr3[$i];
}
}
print_r($result);
I am wondering is it possible to merge 2 arrays that look this:
$array1 = array("a","b","c");
$array2 = array('c'=>array("blah"=>"5", "moreblah"=>"5"));
$merge = array_merge($array1,$array2);
print_r($merge);
Running this will give me this output:
Array ( [0] => a [1] => b [2] => c [c] => Array ( [blah] => 5 [moreblah] => 5 ) )
But the output I want is something similar to this:
Array( [a]=>Array([blah]=>0, [moreblah]=>0), [b]=>Array([blah]=>0, [moreblah]=>0), [c]=>Array([blah]=>5, [moreblah]=>5))
So for the first array I want the values to become the keys and then blah and moreblah to be added and set as 0 if they are not present in array 2. Also for array 2 if there is a repeat such as c in the example array 2 would just overwrite the c index and create the output I wrote above.
Is this possible? If so can I do it with a built in method or would I have to use a for loop to try get it working?
Edit:As has been pointed out to be it is not possible. Can someone explain what type of function I would need to make to be able to get the output I would want?
Something like:
EDIT: This only works when you know the keys in the second array, is this known or not? Else i change the code..
$array1 = array("a", "b", "c");
$array2 = array('c' => array("blah" => "5", "moreblah" => "5"));
$newArray = "";
foreach ($array1 as $a1) {
if (key_exists($a1, $array2)) {
//check if blah and / or moreblah is set else set the value to 0 or something else
if (key_exists("blah", $array2[$a1])) {
$blah = $array2[$a1]["blah"];
} else {
$blah = 0;
}
if (key_exists("moreblah", $array2[$a1])) {
$moreblah = $array2[$a1]["moreblah"];
} else {
$moreblah = 0;
}
$newArray[$a1] = array("blah" => $blah, "moreblah" => $moreblah);
} else {
$newArray[$a1] = array("blah" => 0, "moreblah" => 0);
}
}
$array1 = array("a", "b", "c");
$structured = array_fill_keys($array1, array('blah' => 0, 'moreblah' => 0));
Should give you the array you want as a base. Then it's a matter of looping over your data and adding them where needed.
I have 2 arrays that I have made into an Associative Array. I also have combinations of winner positions = ie '1,2', '2,3', '1,3'. What I need to do is replace the position numbers with jersey numbers and put back into the same configuration as the combinations were written. For Example, I've set up my jersey, position, combo, and associative array:
$jersey = array('3','1','5','4');
$position = array('1','2','3','4');
$AssocArr = array_combine($position, $jersey);
$Combo = array('1,2','2,3','1,3');
I've set up a function to get the values from the keys:
function getVals($finishPosMap, $keys) {
foreach($keys as $key) {
$output[] = $finishPosMap[$key];
}
return $output;
}
The part I'm having issue with is putting them back into an array with the values instead of keys. This is what I've done so far:
foreach($Combo as $set=>$pCombo) {
$com = array($set=>(explode(',', $pCombo)));
foreach($com as $set=>$com){
$c = getVals($AssocArr, $com);
print_r($c);
}
}
print_r gives me:
array( [0] => 3 [1] => 1 )
array( [0] => 1 [1] => 5 )
array( [0] => 3 [1] => 5 )
Can anyone help me put it in the format:
array(0 => '3,1', 1 => '1,5', 2 => '3,5');
Thanks in advance for your help, and please let me know if you think there'd be a better way to do this. Thanks!
I think what you're missing is the array_intersect_key(); this should do it:
$jersey = array('3','1','5','4');
$position = array('1','2','3','4');
$AssocArr = array_combine($position, $jersey);
$Combo = array('1,2','2,3','1,3');
foreach ($Combo as &$value) {
$values = explode(',', $value, 2);
$new_values = array_intersect_key($AssocArr, array_flip($values));
$value = join(',', $new_values);
}
print_r($Combo);
It updates the $Combo array in-place and for each value, calculates the intersection with your associate array.
Demo