Array is like..
array (
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
)
I want pair them like..
Array
( [0] => Array (
[0] => a
[1] => b)
[1] => Array (
[0] => c
[1] => d)
[2] => Array (
[0] => e
[1] => f)
[3] => Array (
[0] => g
[1] => h)
)
How can i do this? there are 8 values and I want 4 pairs from these 8 values. I am new to php so I don't really knw much about php and I searched google but can't find a solution for this.
You can use array_chunk():
print_r(array_chunk($arr, 2));
Demo
Related
This question already has answers here:
Merge all sub arrays into one [duplicate]
(3 answers)
Closed 2 years ago.
I have values like this multi-dimensional array structure, before for-each loop.
Array
(
[1234] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
[1] => Array
(
[0] => g
[1] => h
[2] => i
[3] => j
[4] => k
[5] => l
)
)
[5678] => Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
[1] => Array
(
[0] => g
[1] => h
[2] => i
[3] => j
[4] => k
[5] => l
[6] => m
)
)
)
EXPECTED Output If there is under the same key then merge values in for-each.
Array
(
[1234] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j
[10] => k
[11] => l
)
[5678] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j
[10] => k
[11] => l
[12] => m
)
)
Please let me know if there's any array function or any possible solution.
Above one is multi-dimensional array and expected is 2-dimensional array
You can do it with Splat Operator and array_merge:
$array = [
1234 => [
['a','b','c','d','e','f'],
['g','h','i','j','k','l'],
],
5678 => [
['a','b','c','d','e','f'],
['g','h','i','j','k','l','m'],
]
];
foreach($array as $key => $item){
$array[$key] = array_merge(...$item);
}
var_dump($array);
Arrays and Traversable objects can be unpacked into argument lists
when calling functions by using the ... operator.
Loop over each key's value one by one and do an array_merge for the current key by collecting all of them inside a single array.
<?php
foreach($data as $key => $value){
$data[$key] = [];
foreach($value as $subarray){
$data[$key] = array_merge($data[$key],$subarray);// keep merging them in this original array
}
}
print_r($data);
I was trying to perform KMP pattern match with multiple patterns that can be stored in array but cannot got a clue in accessing values in an array.
I have a sentence say, 'hello this is me doing something'. I exploded by space and got an array of words and stored in a variable $pattern. Now I have an array with lenght 6, having six words. Now I am using str_split function on each index to get letter from it. Code is as follows
$a="hello this is me doing something";
$b=explode(" ",$a);
print_r ($b);
Got an output
Array ( [0] => hello [1] => this [2] => is [3] => me [4] => doing [5] => something )
as expected
Then I tried splting each array using str_split function
$pattern=array();
for($i=0;$i<count($b);$i++){
$pattern[$i]=str_split($b[$i]);
}
print_r($pattern);
I got following output
Array ( [0] => Array ( [0] => h [1] => e [2] => l [3] => l [4] => o ) [1] => Array ( [0] => t [1] => h [2] => i [3] => s ) [2] => Array ( [0] => i [1] => s ) [3] => Array ( [0] => m [1] => e ) [4] => Array ( [0] => d [1] => o [2] => i [3] => n [4] => g ) [5] => Array ( [0] => s [1] => o [2] => m [3] => e [4] => t [5] => h [6] => i [7] => n [8] => g ) )
Now I want every pattern to be different array which can be accessed and called separately. Like if I have [0] => Array ( [0] => h [1] => e [2] => l [3] => l [4] => o )
The expected output should be stored in different array variables dynamically.
How can I get this?
I have two array. I want to merge these two array with the same index.
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
And I want this after merge
Array
(
[0] => a
[1] => 1
[2] => b
[0] => 2
[1] => c
[2] => 3
)
in php you can do simply
$arr=array();
$arr_one=array('a','b','c');
$arr_two=array(1,2,3);
array_push($arr,$arr_one);
array_push($arr,$arr_two);
print_r($arr);
I have a theoretical question that I cannot seem to figure out. Imagine I have the following data in a database:
Main Sub1 Sub2
a x y
x t u
u f g
I want to make a multidimensional array in PHP/mYSQL by essentially asking "what is each 'main' component made of?"
The result would be something like this:
Array
(
[0] => a
(
[0] => x
(
[0] => t
[1] => u
)
(
[0] => f
[1] => g
)
[1] => y
)
)
My efforts result in lots of arrays, instead of a multidimensional array.
You can use references to solve this, although the results will get a bit messy:
$res = [];
foreach ($rows as $row) {
// check if we have each sub component
if (!isset($res[$row['sub1']])) {
$res[$row['sub1']] = $row['sub1'];
}
if (!isset($res[$row['sub2']])) {
$res[$row['sub2']] = $row['sub2'];
}
// build new component with references to the sub components
$res[$row['main']] = [&$res[$row['sub1']], &$res[$row['sub2']]];
}
print_r($res);
Output
Array
(
[x] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[y] => y
[a] => Array
(
[0] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[1] => y
)
[t] => t
[u] => Array
(
[0] => f
[1] => g
)
[f] => f
[g] => g
)
You can clean up the results by filtering out only the arrays:
print_r(array_filter($res, 'is_array'));
Output
Array
(
[x] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[a] => Array
(
[0] => Array
(
[0] => t
[1] => Array
(
[0] => f
[1] => g
)
)
[1] => y
)
[u] => Array
(
[0] => f
[1] => g
)
)
I have an array encoded in UTF-8, sorta like this:
Array
(
[0] => אלף
[1] => בית
[2] => גימל
[3] => דלת
[4] => הא
[5] => ואו
)
Is it possible to leave the 0 array item empty and start the array at 1? I sorta wanna nudge everything over, and the array would then look like this:
Array
(
[0] =>
[1] => אלף
[2] => בית
[3] => גימל
[4] => דלת
[5] => הא
[6] => ואו
)
Thanks!
Use array_unshift, example:
<?php
$arr=array(1,2,3);
array_unshift($arr,null);
print_r($arr);
?>
Prints Array ( [0] => [1] => 1 [2] => 2 [3] => 3 )