CSV parse + multidim arrays - php

59516,?O?L?h,27,80,67,13,1,84.6,40,4,1,32632,??,3,5,32640,AES,3,5,32210,\
???i?Z??,1,1.2,32263,?,1,1.2,3?T,1,1.2,32104,,1,1,40012,??,1,0.8
How would I explode/list this line and pull any field that contains a five-character number and the field immediately preceding it?
i.e. $data['59516']['i'] would contain an array with
(32632=>1, 32640=>5, 32210=>5, 32263=>1.2,32104=>2, 40012=>1)

Not sure i fully understand your question, but it sounds like you want something like:
$items = str_getcsv($commaSeparatedList);
for ($i = 0; $i < count($items); $i++) {
if (strlen($items[$i]) == 5 && is_numeric($items[$i])) {
var_dump($items[$i]); // current match
if (isset($items[$i-1])) {
var_dump($items[$i-1]); // item and one before it
}
}
}

(?<=,)(\d+),(\d{5})
The $matches[1] has the value and the $matches[2] has your key.

Related

WP custom field contains 50 names separated by comma: how to just list the first 5?

As stated in the title, in my blog, I own a custom field that - for each post - contains 50 names, but it's way too much, so I'd like to just echo the first 5 names.
I'm trying with this, but it's not working properly...Where am I going wrong?
<?php
$players = get_post_meta($post->ID, 'Names_List', true);
$i = 1;
foreach($players as $player) {
if ($i < 6) {
echo $player;
}
$i++;
}
?>
You are trying to iterate through a string. This means that when you access $players[2] you will get the third character in the string $players.
You will need to convert the string into an array by using the explode function which will break the string into an array based on a character you tell it.
$string = 'This is a string, This is a string 2';
$array = explode(',', $string);
This will break the string into parts based on a comma, resulting in array as follows:
[ 'This is a string', 'This is a string 2' ]
Once you have turned your string into an array, you can then loop through the first 5 by using a for loop and setting it up to only run 5 times.
for($i = 0; $i < 5; $i++) { ... }
This will run the code between the brackets 5 times as we are saying:
Starting $i at 0, whilst $i is less than 5 - Run the code.
After running the code, $i++ will add 1 to $i and test the condition again.
The following code should be able to replace the code from the question and give you the results you want.
$players = get_post_meta($post->ID, 'Names_List', true);
$players_array = explode(',', $players);
for($i = 0; $i < 5; $i++) {
echo $players_array[$i];
if($i < 4) {
echo ',';
}
}
You can use array_slice to get first 5 elements.
Obviously you need to split the string by the comma delimiter with explode first
$players = get_post_meta($post->ID, 'Names_List', true);
$players_array = explode(',', $players);
$first_five = array_slice($players_array, 0, 5);
foreach($first_five as $player)
{
echo $player;
}

php not able to get array value

I have this problem where I can't get the value from the array in the for loop but I can access it in the while loop.
I can't seem to find a answer online, so help would be much appreciated.
while ($pos = strpos($logTxt, $char, $pos))
{
$t++;
$pos += strlen($char);
$positions[$t] = $pos;
}
for($i = 0; $i < sizeof($positions); $i++)
{
$beginLine = $lastEndLine;
$endLine = $positions[2];
$textToEcho = substr($logTxt,$beginLine,$endLine);
$lastEndLine = $endLine;
}
I think that this could be pretty easily fixed by using a foreach loop instead of a for loop, because it is an array.
foreach($positions as $position) {
$beginLine = $lastEndLine;
$endLine = $position;
$textToEcho = substr($logTxt,$beginLine,$endLine);
$lastEndLine = $endLine;
}
If you want to use a for loop still, I believe your problem is you are only referencing the 3rd position of the array (Key 2, as arrays start at 0), not what the loop is pointing to. You could fix it by doing this
for($i = 0; $i < sizeof($positions); $i++)
{
$beginLine = $lastEndLine;
$endLine = $positions[$i];
$textToEcho = substr($logTxt,$beginLine,$endLine);
$lastEndLine = $endLine;
}
Your $endLine always has third element from array, because of $positions[2]. Try changing it to $positions[$i]
You base problem is using constant index in $positions[2]. But your 1st line in for loop $beginLine = $lastEndLine; will always fail because $lastEndLine is not defined yet. You can use smth like
// beginLine // endLine
$textToEcho = substr($logTxt, $positions[$i-1], $positions[$i]);
of course you need $positions[-1] set to 0 before your first loop or smth like this (it's not clear what's happening before)
UPD I've tried your code and concluded
it will not work at all if $char is the first occurence in $logTxt
it does the nearly the same as explode() function

How to get first missing number out 100 from an array in php

hi i have created watch points in this columns 1,2,3,4,5.....100 will come
Example: 1,2,4,5,34,56,100
from above 3 is missing first this number should return
$watchPoints = $videoWatchedData['watch_points'];
$fetArray = explode(",",$watchPoints); //unsorted 2,4,5,100,56,1,34
i want to sort the above one like this 1,2,4,5,34,56,100 and return first missing number.
What i have tried:
$sortFetchedArraysort = sort($fetArray ); //ksort,rosrt no one is working
$Expected = 1;
foreach ($sortFetchedArraysort as $Number){
if ($Expected != $Number) {
break;
}
$Expected++;
}
$percentageCount = $Number; // first missing number in my case output should return 3
exit;
Two problem i am facing one is sort not working second first missing number is not trturning.
Try this few code, check the live demo.
<?php
sort($array = explode(',', "10,1,2,4,5,6,25,36,75,100"));
print_r(current(array_diff(range(1, 100), $array)));
Hope this simple one, will be helpful for you. In your post you are sorting $fetArray but there is no need, you can check it like this.
<?php
ini_set('display_errors', 1);
$array=range(1,100);//your columns
//you should sort like this, but it is not at all required
$fetArray=array(2,4,5,100,56,1,34);
sort($fetArray);
//looping over array in which we are trying to find
foreach($array as $value)
{
//at the moment your that value is not present in array we will break from loop
if(!in_array($value, $fetArray))
{
break;
}
}
//at the moment we break from loop we will get the value which is not present
echo $value;
$watchPoints = "10,1,2,4,5,6,25,36,75,100";
$fetArray = explode(",", $watchPoints);
sort($fetArray);
for ($i = 0; $i < sizeof($fetArray); $i++) {
if ($fetArray[$i] != $i + 1) {
$missing = $i + 1;
break;
}
}
print($missing);

Issue with getting data when traversing through an array PHP with a variable

I asked a similar question earlier but I couldn't get a clear answer to my issue. I have a function "isParent" that gets 2 pieces of data. Each 1 of the 2 gets a string separating each value with a , or it just gets a plain int and then checks if the first value given is a parent of the second.
I pull the 2 bits of data in and explode them but when I go through my nested for loop and try to test
$toss = $arr1[$i];
print_r($toss);
It comes up blank. I have no idea what the issue is: Here is the full code of the function...
function isParent($parent, $child)
{
$parentArr = explode(',', $parent);
$childArr = explode(',',$child);
//Explode by Comma here. If array length of EITHER parentArr or childArr > 1 Then throw to an Else
if(count($parentArr) <= 1 && count($childArr) <= 1) //If explode of either is > 1 then ELSE
{
$loop = get_highest_slot(15);
for($i = $loop; $i > 0; $i--)
{
$temp = get_membership_from_slot($i,'id_parent','id_child');
if($temp['id_parent'] == $parent && $temp['id_child'] == $child)
{
return 1;
}
}
}
else //set up a for loop in here so that you traverse each parentArr value and for each iteration check all child values
{
$i = count($parentArr);
$c = count($childArr);
for(;$i >=0;$i--) //Loop through every parent
{
for(;$c >=0;$c--)
{
echo '<br>$i = ';
print_r($i);
echo '<br><br>Parent Arr at $i:';
$toss = $parentArr[$i];
echo $toss;
echo '<br>';
print_r($childArr);
echo '<br><br>';
if(isParent($parentArr[$i],$childArr[$c])) //THIS CAUSES AN INFINITE YES! Learn how to pull an array from slot
{
return 1;
}
}
}
}
return 0;
}
You are missing some code for the slot procedures. Apart from that, you probably need to use a different variable for the inner for loop. because $c will be 0 after the first iteration of $i.
Thanks for the help! The issue was in the recursive call back to the top of the function. It was tossed empty slots and when comparing 2 empty slots it returned a false positive. A quick !empty() check fixed it.

Return values of an array from behind (last element) php

say I have an array
$test_backwards=array("something1","something2","something3");
this is just a testing example and it's important to note that values will be added dinamically in my final array. so is it possible to dynamically return values from behind, namely starting from the last element?
something like this but backwards
for($i=0;$i<count($test_backwards);$i++) {
echo $test_backwards.'<br>';
}
Just start at the end and decrement your index:
for ($i = count($test_backwards) - 1; $i >= 0; $i--) {
echo $test_backwards[$i].'<br>';
}
or use array_reverse() (slower):
$test_backwards = array_reverse($test_backwards);
for ($i = 0; $i < count($test_backwards); $i++) {
echo $test_backwards[$i].'<br>';
}
You can also use array_pop(), if you do not need to keep this array. Or you can assign it to a temp array and then array_pop it, it will get and delete value from last.
$temp = $test_backwards;
while(($item = array_pop($temp)) !== NULL ) {
echo $item;
}

Categories