Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an array
array('a' => 1, 'b' => 2, 'c' => 3)
I want to get
array('a', 1, 'b', 2, 'c', 3)
How do I do that? Is it possible not to use each/loop ?
Use a foreach ($array as $key=>$value) then dump them into a new array.
$a = array('a' => 1, 'b' => 2, 'c' => 3);
foreach($a as $k=>$v) {
$b[] = $k;
$b[] = $v;
}
print_r($b);
// output - Array ( [0] => a [1] => 1 [2] => b [3] => 2 [4] => c [5] => 3 )
I figured, for the heck of it, a ridiculous one-liner(*):
$a = array('a' => 1, 'b' => 2, 'c' => 3);
$result = array_combine(range(0, count($a) * 2 - 1, 2), array_keys($a)) + array_combine(range(1, count($a) * 2, 2), array_values($a));
ksort($result);
print_r($result);
(*) It still needs ksort() as a separate step.
Update
Based on OP's comment, I realized it could be made more succinct (*)
$result = explode('=', http_build_query($a, '', '='));
(*) Sacrificing correctness
foreach is the recommended way but i don't know why you need it in a array function
I used array_walk instead of the foreach
<?php
$arr=array('a' => 1, 'b' => 2, 'c' => 3);
$new_arr = array();
array_walk($arr,function ($v,$k) use (&$new_arr) { $new_arr[]=$k;$new_arr[]=$v;});
print_r($new_arr);
output:
Array
(
[0] => a
[1] => 1
[2] => b
[3] => 2
[4] => c
[5] => 3
)
Here is an example if you do not what to use a loop:
$a = array('a' => 1, 'b' => 2, 'c' => 3);
$flip = array_flip($a);
$return = array_merge(array_values($a), array_values($flip));
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
$a = Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
$result = Array(
[0] => A
[1] => B
[2] => C
[3] => D
)
I want to change $a to $result to result I tried every way I know but it's hard to find the answer Is there any way?
Also, the $a array can grow and shrink.
1 = A, 2 = B, 3 = C, 4 = D, 5 = E, 6 = F etc...
You could have simply achieved using the array_map and the chr functions as:
I hope this is what you're looking for. No matter in which sequence the numbers are, it'll be taken care of:
<?php
$a = [1, 2, 3, 4];
$result = array_map(function ($v) {
return chr(64 + $v);
}, $a);
print_r($result);
?>
You can achieve the same using a foreach and the chr function as:
<?php
$a = [1, 2, 3, 4];
$result = [];
foreach($a as $v) {
$result[] = chr(64 + $v);
}
print_r($result);
?>
or you could simply use the chr function when using the array value as:
echo chr(64 + $a[$index]);
Output:
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
and for the input: $a = [2, 3, 1, 4]; result will be:
Array
(
[0] => B
[1] => C
[2] => A
[3] => D
)
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
Now I have an array looks like this
Array([0] => array([region]=>1[district]=>2[sell]=>3)
[1] => array([region]=>1[district]=>3[sell]=>6)
[2] => array([region]=>1[district]=>4[sell]=>9)
)
And I have an other array look like this
Array([0] => array([buy]=>3)
[1] => array([buy]=>4)
[2] => array([buy]=>5)
)
So the question is how can i combine two array to make it looks like this ?
or is there any method to push the second array into the first array?
Array([0] => array([region]=>1[district]=>2[sell]=>3[buy]=>3)
[1] => array([region]=>1[district]=>3[sell]=>6[buy]=>4)
[2] => array([region]=>1[district]=>3[sell]=>9[buy]=>5)
)
Do not forget about functional programming.
$existing = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 4, 'b' => 5, 'c' => 6],
['a' => 5, 'b' => 8, 'c' => 9],
];
$newItems = [
['d' => 3],
['d' => 4],
['d' => 5]
];
// let's run over those arrays and do array_merge over items
$result = array_map('array_merge', $existing, $newItems);
var_dump($result);
P.S. There is exist more simple way using array_replace_recursive
$result2 = array_replace_recursive($existing, $newItems);
Try this
$existing = array(
0 => array('region'=>1, 'district'=>2, 'sell'=>3),
1 => array('region'=>4, 'district'=>5, 'sell'=>6),
2 => array('region'=>5, 'district'=>8, 'sell'=>9),
);
$newItems = array(
0 => array('buy'=>3),
1 => array('buy'=>4),
2 => array('buy'=>5)
);
foreach($newItems as $i => $data){
$key = array_pop(array_keys($data));
$value = array_pop(array_values($data));
$existing[$i][$key] = $value;
}
echo '<pre>'; print_r($existing); echo '</pre>';
PhpFiddle Demo
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
So I have an array
array ( 1, 2, 2, 5, 4, 5, 4, 1, 1, 5 ) and I need to collect same array values and divide it to different arrays. So after this action from this array I should have 4 different arrays:
array ( 1, 1, 1 )
array ( 2, 2 )
array ( 5, 5, 5 )
array ( 4, 4 )
what is the best way to do it?
$result = [];
foreach (array_count_values($values) as $value => $occurrence) {
$result[] = array_fill(0, $occurrence, $value);
}
This assumes that the individual value identity doesn't matter, i.e. that you don't have objects whose individual instance you need.
Though I'm not sure why you'd need that particular array format then in the first place. Just use the result of array_count_values.
$input = array ( 1, 2, 2, 5, 4, 5, 4, 1, 1, 5 );
$output = array();
foreach ($input as $value)
$output[$value][] = $value;
var_dump($output)
You can make another array, with the distinct values as the array keys.
<?php
$array = array(1, 2, 2, 5, 4, 5, 4, 1, 1, 5);
$result = array();
foreach ($array as $val) {
if (!isset($result[$val])) { // Check if the index exists
$result[$val] = array();
}
$result[$val][] = $val;
}
print_r($result);
You can try something like below
<?php
$a = array ( 1, 2, 2, 5, 4, 5, 4, 1, 1, 5 );
$a = array_count_values($a);
$arr = array();
foreach($a as $k=>$v){
for($i = 0; $i < $v; $i++){
$arr[$k][] = $k;
}
}
print_r($arr );
?>
Output:
Array
(
[1] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[2] => Array
(
[0] => 2
[1] => 2
)
[5] => Array
(
[0] => 5
[1] => 5
[2] => 5
)
[4] => Array
(
[0] => 4
[1] => 4
)
)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have two arrays like
Array1
(
[0] => 1
[1] => 2
[2] => 3
)
Array2
(
[0] => a
[1] => b
[2] => c
)
i want make
Array3 Like
(
[0] => ([0]=>1 [1]=>a)
[1] => ([0]=>2 [1]=>b)
[2] => ([0]=>3 [1]=>c)
)
This is definitely not the prettiest way to do it, but since you haven't supplied any attempted code, I doubt anybody wants to bother with this question so here:
NOTE: As stated in the comments, you're going to have to make sure the two arrays are the same length and sort that out yourself.
$one = array(
'1',
'2',
'3'
);
$two = array(
'a',
'b',
'c'
);
$derp = array();
foreach($one as $key => $val) {
$derp[] = array(
$val,
$two[$key]
);
}
?>
Which returns
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
)
Using SPL's MultipleIterator
$arr1 = [1, 2, 3];
$arr2 = ['a', 'b', 'c'];
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($arr1));
$mi->attachIterator(new ArrayIterator($arr2));
$result = array();
foreach($mi as $details) {
$result[] = $details;
}
var_dump($result);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is an example array I want to split:
(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0)
How do I split it in 3 arrays like this?
(1,2,3,4,5,0) (6,7,0) (8,9,10,11,12,0)
Here's the solution. (Author said he wants to split at '0' instead of duplicate values)
function split_array_by_duplicate_values($a, $duplicate = 0)
{
$c = array_intersect($a, array($duplicate));
$r = array();
$i = 0;
foreach($c as $k => $v) {
$l = ++$k - $i;
$r[] = array_slice($a, $i, $l);
$i = $k;
}
return $r;
}
$a = array(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0);
print_r(split_array_by_duplicate_values($a));
returns
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 0
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 0
)
[2] => Array
(
[0] => 8
[1] => 9
[2] => 10
[3] => 11
[4] => 12
[5] => 0
)
)
Try this:
$array = array(1, 2, 3, 0, 4, 5, 6);
$array_0 = array_splice($array, 0, 4);
$array_1 = array_splice($array, 4);
I think what you're looking for is array_splice.
$array=array(1,2,3,0,4,5,6);
$newarray=array_splice($array,4);
print_r($array); //Array ( [0] => 1 [1] => 2 [2] => 3 [3]=>0);
print_r($newarray); //Array( [0]=>4 [1]=>5 [2]=>6)
If you want to split an array into two arrays in one multidimensional array, you could do
$array=array(1,2,3,0,4,5,6);
$newarray=array_chunk($array,4); //returns [[1,2,3,0],[4,5,6]]
You should use array split to split the array as you would like
<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>