How to read values in an array with multiple array values - php

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?

Related

In php for-each loop, If same parent key then merge value [duplicate]

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);

Separate each alphabet by using preg_match PHP

i know that this is a beginner level question, but i am stuck here and i need help.
i want to store each alphabet in an array. (Only Alphabets not integers)
let me show you the previous code that i am using
$str = "ab c45 d123ef";
preg_match_all('/./us', $str, $ar);
echo '<pre>';
print_r($ar);
its output
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 4
[5] => 5
[6] =>
[7] => d
[8] => 1
[9] => 2
[10] => 3
[11] => e
[12] => f
)
)
But it also separate integers... what i have to change in the preg_match expression, i want this output.
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 45
[5] =>
[6] => d
[7] => 123
[8] => e
[9] => f
)
)
preg_match_all('/[\d.]+|./us', $str, $ar);
[\d.] matches a digit or decimal point, the + quantifier after it matches a sequence of them.
Result:
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 45
[5] =>
[6] => d
[7] => 123
[8] => e
[9] => f
)
)
A way with preg_split:
$result = preg_split('/(?=\pL)|(?<=\pL)/', $str, -1, PREG_SPLIT_NO_EMPTY);
You obtain:
Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 45
[5] => d
[6] => 123
[7] => e
[8] => f
)
The current pattern matches two kind of positions, positions followed by a letter (?=\pL), and positions preceded by a letter (?<=\pL).

Read saved text from a file an array in php

I have created a script that generates an array.
This array needs to be stored so that if a user wants to export its contents( eg print ) it may be possible.
This is my array ($doc)
Array
(
[0] => Array
(
[0] => ------
[1] => ---Start----
[2] => -------
[3] => Generated on Saturday, 3rd, August 2013
)
[1] => Array
(
[0] => ------
[1] => -------
[2] => -------
[3] => -----------------------
)
[2] => Array
(
[0] => Tid
[1] => Policy
[2] => Budget
[3] => User
[4] => Capturetime
[5] => Country
)
[3] => Array
(
[0] => 1
[1] => ask
[2] => das
[3] => carol
[4] => 2013-07-09
[5] => Ethiopia
)
[4] => Array
(
[0] => 2
[1] => das
[2] => adsasd
[3] => ck
[4] => 2007-07-13
[5] => Slovakia
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[6] => Array
(
[0] => ------
[1] => ---End----
[2] => -------
)
)
Now I save the array to a file:
file_put_contents('array.txt', print_r($printdoc, true));
Then I would like to call back the contents of array.txt to the array $printdoc and use it as an array.
$printdoc = file_get_contents('array.txt', true);
I get $printdoc as a string instead of an array
Any help?
You can use serialize() to convert your array into a serializable string, then store it in a file. When you read the string from the file use unserialze() to convert it back into an array.

Nudging an array from it's zero item

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 )

PHP: How to pairing up an arrey values

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

Categories