the below code searches a very long queried result, "body" and pulls out quoted numbers. I want to insert those numbers into a single column reference table. I had this working when it was a preg_match, but preg_match all has thrown a wrench in and it continues to bark about array-string conversion. Each "body" result can have multiple number results, and I believe this causing multiple arrays. Can anyone help me get this inserted?
$textToReplace = mysqli_query($conn, "SELECT body from T1");
while($row2 = $textToReplace->fetch_array()){
$body = $row2["body"];
preg_match_all('#"\d+"#', $body, $matches);
foreach($matches as $match) {
$id=$match;
var_dump($id);
// $sql = mysqli_query($conn, "INSERT into T2 VALUES($id)");
}
}
This is a snippet of the result of a var_dump on $id:
array (size=1)
0 => string '"10064905"' (length=10)
array (size=3)
0 => string '"10064788"' (length=10)
1 => string '"10064797"' (length=10)
2 => string '"10064807"' (length=10)
array (size=1)
0 => string '"10063833"' (length=10)
array (size=1)
0 => string '"10063824"' (length=10)
array (size=2)
0 => string '"10063805"' (length=10)
1 => string '"10063796"' (length=10)
preg_match_all() returns a 2-dimensional array. By default, $matches[0] contains all the full-regexp matches, the remaining $matches[n] contain the matches for capture group n. In your program, you need to iterate over $matches[0]:
foreach($matches[0] as $match) {
$id=$match;
var_dump($id);
$sql = mysqli_query($conn, "INSERT into T2 VALUES($id)");
}
Related
I am trying to array_merge id’s from one array into another, it’s merging but putting the new values on a new array line:
IDs to be added: $attachment_ids
array (size=2)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
IDs which already exist and will be added to: $existing_data
array (size=1)
0 => string '2589,2561,2432,2422' (length=19)
result of my array_merge:
$merged_data = array_merge($attachment_ids, $existing_data);
is
array (size=3)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
2 => string '2589,2561,2432,2422' (length=19)
My expected result is:
array (size=1)
0 => string '2620,2621,2589,2561,2432,2422'
If you just need the 1 string with all of the values, you could use implode() with the result you have so far...
$merged_data = [ implode(",", $merged_data) ];
Shortly array merge with array_map
$result=array_map(null,$array1,$array2);
array_merge() requires 2 arrays. The existing data is all in a string. With explode, this data can be converted into an array. With implode() a list is created after array_merge().
$attachment_ids = ['2620','2621'];
$existing_data = ['2589,2561,2432,2422'];
$existing_data = explode(',',$existing_data[0]);
$result = [implode(',',array_merge($attachment_ids,$existing_data))];
//$result: array(1) { [0]=> string(29) "2620,2621,2589,2561,2432,2422" }
Update
Alternatively, strings can be chained manually.
$result = [implode(',',$attachment_ids).','.$existing_data[0]];
I'm using preg_match_all() to get matches of the parameters of my url. Problem is matches function send me back an array of array. So it became difficult to explore it with a foreach function (to replace parameters by default parameters for example).
Is there a way to explore straight an array of array and send back the value of the second array, not the first, without passing by the first ?
I put you a simple example of my problem:
$var = "abababa";
preg_match_all("#(a)#", $var, $matches);
$args = array_slice ($matches, 1);
var_dump($args)
arg return me :
array (size=1)
0 =>
array (size=5)
0 => string 'a' (length=1)
1 => string 'a' (length=1)
2 => string 'a' (length=1)
3 => string 'a' (length=1)
And I wish it could return just
array (size=5)
0 => string 'a' (length=1)
1 => string 'a' (length=1)
2 => string 'a' (length=1)
3 => string 'a' (length=1)
Therefore to be able to replace args elements by othes (default elements for example).
Thanks if you can help me.
You can simply do this:
$var = "abababa";
preg_match_all("#(a)#", $var, $matches);
$args = array_slice ($matches[0], 1); // notice we are accessing the first element of $matches
var_dump($args);
This should produce your desired output.
I'm trying to replace this array:
$lts = ['20', '21'];
with an array from the database. I've tried a few things with no avail. Here's what I've got now:
$query = "SELECT id FROM members WHERE admin = '2'";
$lts = mysqli_fetch_all($con->query($query), MYSQLI_NUM);
When I var_dump it its giving me this:
array (size=2)
0 =>
array (size=1)
0 => string '20' (length=2)
1 =>
array (size=1)
0 => string '21' (length=2)
I need it to give me this:
array (size=2)
0 => string '20' (length=2)
1 => string '21' (length=2)
Any idea on how to get the latter array?
I just notice your returning array is same as the array you got,
never mind run this code
//run loop on your array after your query
for($i=0; $i<count($lts); $i++){
for($j=0; $j<count($lts[$i]); $j++){
$lts[$i]=$lts[$i][$j];
}
}
echo '<pre>';
print_r($lts);
I'm trying to create a simple PHP script that retrieves info from a string and puts it into an array. Ive looked around on some sites on multi capture regex for one pattern but can't seem to get the output im looking for
Currently this is my script.
$input = "username: jack number: 20";
//$input = file_get_contents("test.txt");
preg_match_all("/username: ([^\s]+)|number: ([^\s]+)/", $input, $data);
var_dump($data);
Which produces this output:
0 =>
array (size=2)
0 => string 'username: jack' (length=14)
1 => string 'number: 20' (length=10)
1 =>
array (size=2)
0 => string 'jack' (length=4)
1 => string '' (length=0)
2 =>
array (size=2)
0 => string '' (length=0)
1 => string '20' (length=2)
Im looking to get the data into the form of:
0 =>
array (size=x)
0 => string 'jack'
1 =>
array (size=x)
0 => string '20'
Or two different arrays where the keys correspond to the same user/number combo
You can use match-reset \K:
preg_match_all('/\b(?:username|number):\h*\K\S+/', $input, $data);
print_r($data[0]);
Array
(
[0] => jack
[1] => 20
)
RegEx Breakup:
\b => a word boundary
(?:username|number) => matches username or number. (?:..) is non-capturing group
:\h* => matches a colon followed optional horizontal spaces
\K => match reset, causes regex engine to forget matched data
\S+ => match 1 or more non-space chars
Or else you can use a capturing group to get your matched data like this:
preg_match_all('/\b(?:username|number):\h*(\S+)/', $input, $data);
print_r($data[1]);
Array
(
[0] => jack
[1] => 20
)
(?<=username:|number:)\s*(\S+)
You can use lookbehind here.See demo.
https://regex101.com/r/mG8kZ9/10
I have an array like the example below (from var_dump($tagged);:
array (size=1)
0 =>
array (size=7)
0 => string '#raining' (length=8)
1 => string '#raining' (length=8)
2 => string '#driving' (length=8)
3 => string '#hungrySoon' (length=11)
4 => string '#strangeworld' (length=13)
5 => string '#fruitweekFunky' (length=15)
6 => string '#kevins_rainbow_disco' (length=21)
7 => string '#raining' (length=8)
8 => string '#fruitweekFunky' (length=15)
I am simply after displaying the array as follows (From the most frequent first):
#raining
#fruitweekFunky
#driving ...etc
To achieve exactly what you've asked for (a descending ordered array of the highest occurences to the lowest) step by step:
Count the number of occurences:
$occurences = array_count_values($tagged[0]);
Sort the array (by value, because the number of occurrences is the current array value and the tag is the key - arsort() maintains the original keys):
arsort($occurences);
Get array of the keys for output (because the tags are currently keys):
var_dump(array_keys($occurences));
Fact is, $tagged is an array of arrays. So you need to take its first element.
Try :
$result = array_count_values(array_values($tagged[0]));
var_dump($result);