1

I have the following function on my site which grabs all the values from $vid_pix, and echos each them or any associated variables in a foreach loop.

Which works fine, however I have a variable - $Pt that also gets echoed out as well.

Right now though what I am trying to do is - skip the first value in $Pt. Also set a static value for the last instance since everything is being moved up 1, leaving the last with no value.

I've tried array_splice and unset, but it's not skipping the first $Pt value.

So if I had -

[vp 1]->[5]
[vp 2]->[10]
[vp 3]->[15]
[vp 4]->[20]

I would need -

[vp 1]->[10]
[vp 2]->[15]
[vp 3]->[20]
[vp 4]->[x]

(x = I would have to assign a static value for the last variable.)

My function (stripped down for simplicity)

$vid_pix = get_post_meta($v_Id, 'vid_pix', false);

foreach ($vid_pix as $vP) {
    $Pt = get_post_meta($vP, 'photo_time', false);
    array_splice($Pt, 0, 1);
    //unset($Pt[0]);
    $Pt = get_post_meta($vP, 'photo_time', true);
    echo $Pt;

    if (last -> $Pt) { // something like this for the last value
        $Pt = '5';
    }
 }

To put things into better context, here's the full code for the specific function I'm trying to achieve this within --

/*
This is for looping through the uploaded pictures
and sorting them and creating a text file.
*/

$vid_pix = get_post_meta($v_Id, 'vid_pix', false);

$data = "ffconcat version 1.0";
$line = '';

        usort( $vid_pix, function( $a, $b ){
            $aPor = (int) get_post_meta( $a, 'photo_order', true );
            $bPor = (int) get_post_meta( $b, 'photo_order', true );

            if ( $aPor === $bPor ) {
                return 0;
            }

            return ( $aPor < $bPor ) ? -1 : 1;
        } );

        foreach ($vid_pix as $vP) {
$filename = basename( get_attached_file( $vP ));
$Pt = get_post_meta($vP, 'photo_time', true);
$Por = get_post_meta($vP, 'photo_order', true);

$static_value=25;
$array=$Pt;

reset($array);//reset the internal pointer
while(false!==($key=key($array))&&null!==key($array)){//check for current key validity
    $next=next($array);//get the next value and move the pointer
    $array[$key]=$next&&isset($array[$key])?$next:$static_value;//assign the next value to the current key  if valid or the static value if false 
}

var_dump($Por);
var_dump($array);

// try to determine the pic of the placeholder image

if ($vP === end($vid_pix))
        $last_img = $thepath.'/'.$filename;


if ($vstyle === 'Custom') { // if custom timing is chosen

$slide_dur = "\r\nduration ".$Pt;

$filename = basename( get_attached_file( $vP ));
$line .= "file '".$thepath."/".$filename."'".$slide_dur."\r\n";


} else { // if custom timing is NOT chosen

$filename = basename( get_attached_file( $vP ));
$line .= "file '".$thepath."/".$filename."'".$slide_dur."\r\n";

}

$total_items = count($vid_pix);

if ($total_items > 1) { // if total items is more than one

// LAST LINE OF CONCAT TEXT FILE
$lastline = "file '".$last_img."'\r\nduration 2\r\nfile '".$last_img."'";

$isitone = "";
$solopic = "";


// PUT TOGETHER ALL THE LINES FOR THE TEXT FILE
$txtc = $data."\r\n".$line.$lastline;

} else { // if total items is less than one

$isitone = "true";
$solopic = "-loop 1 -probesize 10M -i ".$thepath."/".$filename;

}
}

// SAVE THE TEXT FILE
file_put_contents($thepath.'/paths.txt', $txtc);

UPDATE

var_dump results -

string(1) "7"
string(1) "2"
string(1) "6"
string(1) "9"

The following link contains the original code which saves the $Pt variable -- here

Saeed.Gh
  • 1,285
  • 10
  • 22
730wavy
  • 944
  • 1
  • 19
  • 57
  • 1
    how about array_shift() ? – TsV Sep 11 '18 at 14:13
  • 2
    Or don't drop it and use a for loop instead, iterating the array from the second element to the last. – DigiLive Sep 11 '18 at 14:21
  • @dnFer Because then Im going to have to change other things as well so im trying to keep it as simple as possible. – 730wavy Sep 11 '18 at 14:43
  • your example of what you'd like does not match your explanation. Your example implies you want to skip the first element, `vp 1`, but your explanation suggests that the output from `get_post_meta` is not what you want. Please clarify. – DevDonkey Sep 11 '18 at 14:58
  • Not skip the first element of $Vp, skip the first $Pt. $Vp is an ID number and $Pt is the number value entered in a form thats associated with that ID. So I want ID # 1 to have the $Pt value from ID # 2 ... makes sense now? @DevDonkey – 730wavy Sep 11 '18 at 15:12
  • Try my answer and let me know whether it helps. – Outsource WordPress Sep 22 '18 at 15:58
  • Have you even seen my answer? – Andreas Sep 24 '18 at 16:06

10 Answers10

1

Based on OP's comments, $Pt is an array of array. So we simply use array_shift to remove the first value (second level array). And, append an array of static value to the end:

$vid_pix = get_post_meta($v_Id, 'vid_pix', false);

foreach ($vid_pix as $vP) {
    $Pt = get_post_meta($vP, 'photo_time', false);

    // $Pt is an array of array
    // remove the first key (first array)
    $first_key = array_shift($Pt);

    // Add the static value to end of the array. eg: '5'
    $static_value = '5'; // define static value
    $Pt[] = array($static_value);

    // print them out
    var_dump($Pt);
}
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
  • @Rich it is very unclear what you are trying to achieve through your whole code, unless there are more inline comments. Anyways, based on your initial posted requirement, I believe that my could should help you achieve that – Madhur Bhaiya Sep 13 '18 at 03:18
  • @Rich I have updated the code in sync with your striped down sample code. Please check and let me know! – Madhur Bhaiya Sep 19 '18 at 17:20
  • I've tried your code and the output of var dump is -- `array(1) { [0]=> string(1) "5" } array(1) { [0]=> string(1) "5" } array(1) { [0]=> string(1) "5" } array(1) { [0]=> string(1) "5" } array(1) { [0]=> string(1) "5" }` but my actual values are - `7, 2, 6, 9, blank` – 730wavy Sep 20 '18 at 14:20
  • @Rich what is the output of var_dump of `$Pt` also ? – Madhur Bhaiya Sep 20 '18 at 14:21
  • it is --- `array(1) { [0]=> string(1) "7" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "6" } array(1) { [0]=> string(1) "9" } array(1) { [0]=> string(0) "" }` – 730wavy Sep 20 '18 at 14:35
  • @Rich so it is an array of array. And, you want to remove `"7"` (first value) ? – Madhur Bhaiya Sep 20 '18 at 15:25
  • yes, but the number will vary so Im looking to remove the first number and shift the other values so that key 1 = value 2 , key 2 = value 3 and so forth – 730wavy Sep 20 '18 at 15:31
  • @Rich please check edited answer now. See if it works for you. – Madhur Bhaiya Sep 20 '18 at 15:54
  • this is my output -- `array(1) { [0]=> array(1) { [0]=> string(1) "5" } } array(1) { [0]=> array(1) { [0]=> string(1) "5" } } array(1) { [0]=> array(1) { [0]=> string(1) "5" } } array(1) { [0]=> array(1) { [0]=> string(1) "5" } } array(1) { [0]=> array(1) { [0]=> string(1) "5" } }` – 730wavy Sep 20 '18 at 17:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180472/discussion-between-madhur-bhaiya-and-rich). – Madhur Bhaiya Sep 20 '18 at 18:03
1

UPDATE #1

Try using array_shift() like this:

foreach ($vid_pix as $vP) {
    $Pt2 = get_post_meta($vP, 'photo_time', false);
    $Pt = array_shift( $Pt2 ); // get first value
    $Pt2[] = 'static value';
    var_dump( $Pt, $Pt2 ); // test
}

UPDATE #2

If there's actually just one photo_time meta for each image/attachment (the attachment IDs are in the $vid_pix array), then this should work:

$Pt2 = [];
$n = count( $vid_pix );
for ( $i = 0, $j = 1; $i < $n; $i++, $j++ ) {
    $Pt2[] = ( $n === $j ) ? 'static value' :
        get_post_meta( $vid_pix[ $j ], 'photo_time', true );
}
echo implode( ', ', $Pt2 ); // test

UPDATE #3

Add the above ($Pt2) code above/before this foreach, then add the $i =>, and change the $Pt as you can see below:

foreach ($vid_pix as $i => $vP) {
  $filename = basename( get_attached_file( $vP ));
  $Pt = $Pt2[ $i ];
  ...
}
Sally CJ
  • 15,362
  • 2
  • 16
  • 34
  • my output looked like this -- `5491: static value
    5492: static value
    5493: static value
    5494: static value
    5495: static value`
    – 730wavy Sep 24 '18 at 13:33
  • my output looks like this - `string(2) "10" array(1) { [0]=> string(12) "static value" } string(1) "7" array(1) { [0]=> string(12) "static value" } string(1) "5" array(1) { [0]=> string(12) "static value" }` the numbers im using in this example are '10,7,5' – 730wavy Sep 24 '18 at 14:34
  • 1
    So the `$Pt2` was `[10, 7, 5]`? How about `$vid_pix`? What are their actual values? – Sally CJ Sep 24 '18 at 14:40
  • before the $vid_pix foreach loop, the output of vid_pix is -- `array(3) { [0]=> string(4) "5503" [1]=> string(4) "5504" [2]=> string(4) "5505" }` and $pt2 output -- `array(1) { [0]=> string(12) "static value" }
    array(1) { [0]=> string(12) "static value" }
    array(1) { [0]=> string(12) "static value" }`
    – 730wavy Sep 24 '18 at 15:16
  • this is the output -- `7, 5, static value7, 5, static value7, 5, static value` – 730wavy Sep 24 '18 at 16:34
  • 1
    What is the output you expected? – Sally CJ Sep 24 '18 at 16:36
  • in my function i have this line in the `$vid_pix foreach` -- `$slide_dur = "\r\nduration ".$Pt;` which outputs `duration (Number)` for each image/vid_pix. In that line when I change `$Pt` to your `$Pt2` it outputs -- `duration Array` for each line. – 730wavy Sep 24 '18 at 16:40
  • thank you. It works, now I have one more issue with this code, I'll be posting a new question. – 730wavy Sep 24 '18 at 17:32
  • 1
    You're welcome @Rich. But maybe I should add that, you no longer need the `$array` thing as in the code in the current question. (Because the `$Pt2` code already doing the same job.) – Sally CJ Sep 24 '18 at 17:49
  • I've cleaned up my code compared to the one in the question thank you. If you have a chance can you please check this question -- https://stackoverflow.com/questions/52484842/subtract-array-values-from-each-other-creating-new-array – 730wavy Sep 24 '18 at 17:54
  • Sorry, I actually deleted my answer [there](https://stackoverflow.com/q/52484842/9217760). Please ask the first commentator to post an answer. =) Cheers! – Sally CJ Sep 25 '18 at 03:10
  • your answer was correct though and the answer/code I am using. – 730wavy Sep 25 '18 at 12:39
  • I know it was correct! XD I mean, I'm glad it helped you. =) – Sally CJ Sep 25 '18 at 13:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180739/discussion-between-sally-cj-and-rich). – Sally CJ Sep 25 '18 at 13:32
0

Why not to do it in the simplest way?

$keys = array_keys($myHashArray);
$values = array_values($myHashArray);
array_shift($keys);
array_shift($values);
$result = array_combine($keys, $values);

Please correct me if I understood it wrong.

Alex
  • 4,621
  • 1
  • 20
  • 30
  • 1
    he wants to skip the first value of `get_post_meta`, not the parent array. I read it like you did. the question is ambiguous. Also, why do 2 checks? why not do `if(!$isFirst)`? – DevDonkey Sep 11 '18 at 15:00
  • Thank you, I've changed my answer – Alex Sep 11 '18 at 15:10
0

you could simply proceed like this:

$static_value=25;
$array=['vp 1'=>5,'vp 2'=>10,'vp 3'=>15,'vp 4'=>20];

reset($array);//reset the internal pointer
while(false!==($key=key($array))&&null!==key($array)){//check for current key validity
    $next=next($array);//get the next value and move the pointer
    $array[$key]=$next&&isset($array[$key])?$next:$static_value;//assign the next value to the current key  if valid or the static value if false 
}


var_dump($array);

output :

array(4) {
  ["vp 1"]=>
  int(10)
  ["vp 2"]=>
  int(15)
  ["vp 3"]=>
  int(20)
  ["vp 4"]=>
  int(25)
}
Elementary
  • 1,443
  • 1
  • 7
  • 17
  • I've updated my question with my full code, also with your suggestion but it didnt change anything. – 730wavy Sep 12 '18 at 21:37
  • @Rich i don't really understand what you are trying to achieve with this code however you don't use my code as expected.Either your replace all the `$array` occurrences simply by `$Pt` or you use `$array=&$Pt` instead of simply `$array=$Pt`. Moreover if `$Pt` is an array why are you trying to concatenate it to a string in your code? – Elementary Sep 12 '18 at 22:03
0

How about ?

$new_array = array_combine( // to make an array from $keys / values
  array_keys($old_array), // use the old same keys
  array_merge( // the array containing your new values
    array_splice($old_array, 1), // remove the first element
    [$static_value] // add the static value
  )
);

Check it in live here : https://repl.it/repls/PerfectUpsetAnalysts

mrbm
  • 2,164
  • 12
  • 11
0
$vid_pix = get_post_meta($v_Id, 'vid_pix', false);
$i = 0;
foreach ($vid_pix as $vP) {
   if($i ==0){
continue;
}
    $Pt = get_post_meta($vP, 'photo_time', false);
    array_splice($Pt, 0, 1);
    //unset($Pt[0]);
    $Pt = get_post_meta($vP, 'photo_time', true);
    echo $Pt;

    if (last -> $Pt) { // something like this for the last value
        $Pt = '5';
    }
$i++;
 }

use this type of counter for skipping value....

0

As you already know you are getting photo_time of each vid_pix and in your question I see you need to get photo_time of next vid_pix for current vid_pix and ignore the first photo_time which is 5 in provided example and the last vid_pix (put something static instead)

[vp 1]->[5]
[vp 2]->[10]
...

I would need -

[vp 1]->[10]
[vp 2]->[15]
...

so I think its simple (I use vip_pix index as key, you can use whatever)

$pt_result = [];
for ($i = 0; $i < count($vid_pix) - 1; ++$i) {
    $pt_result[$vid_pix[$i]] = get_post_meta($vid_pix[$i+1], 'photo_time', true);
}
$pt_result[$vid_pix[$i]] = 25; //set last one statically
Saeed.Gh
  • 1,285
  • 10
  • 22
  • how would i use it with my current code ? $pt_result would replace $Pt? – 730wavy Sep 24 '18 at 13:36
  • @Rich pt_result is an array keeps vip_pix ID as key and poto_time as value. You can change it in the way you want. In first code provided you just echo pt. So you can echo $pt_result array in a loop too. In second code you push it in an array. $pt_result already is an array of pt – Saeed.Gh Sep 24 '18 at 13:51
0

You need to shift the array outside the loop since your $Pt array has single value. Try the code given below.

$newPt = array(); // initialize new array
$vid_pix = get_post_meta($v_Id, 'vid_pix', false);

foreach ($vid_pix as $vP) {
    $Pt = get_post_meta($vP, 'photo_time', false);

    // assign to new array
    $newPt[] = $Pt;
}

// remove first value
array_shift( $newPt );

// add static value to end of the array
$static = '5';
$newPt[] = array( $static );

// print updated array
var_dump($newPt);

Hope this helps.

Outsource WordPress
  • 3,749
  • 2
  • 10
  • 23
0

How about passing the results of get_post_meta($vP, 'photo_time', false); into a function that shifts the values and adds the static value to the end?

function shiftPt($input, $end = null) {
    $output = [];
    $prevKey = null;
    foreach ($input as $key => $value) {
        if ($lastKey) $output[$lastKey] = $value;
        $lastKey = $key;
    }
    $output[$key] = $end;
    return $output;
}

So essentially you could just wrap your existing function like so...

$pT = shiftPt(get_post_meta($vP, 'photo_time', false));

... and this would leave the last value in the array as null while shifting all of the original values up.

If you wanted to specify the final value (e.g. as 5) then you could do...

$pT = shiftPt(get_post_meta($vP, 'photo_time', false), 5);
Rob Sinton
  • 510
  • 5
  • 13
  • will this work if $Pt is an array attached to the $vid_pix array. – 730wavy Sep 22 '18 at 21:05
  • It would - although based on your example code above $Pt isn't "attached" to the $vid_pix array. In your code $Pt is a variable created and populated with the result of get_post_meta($vP, 'photo_time', false) - $vid_pix is irrelevant. – Rob Sinton Sep 22 '18 at 21:08
  • In my update I added a link to more code to show how $Pt is relevant to $vid_pix – 730wavy Sep 23 '18 at 15:24
0

You can use array_slice to remove one set of value from the array and use array_merge to merge it with the "x".

Then you just combine the keys and "new" values.

$arr = [
'vp 1'=>[5],
'vp 2'=>[10],
'vp 3'=>[15],
'vp 4'=>[20]];

$keys = array_keys($arr);
$values = array_merge(array_slice($arr, 1), ["x"]);

$new = array_combine($keys, $values);
var_dump($new);

This outputs:

array(4) {
  ["vp 1"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["vp 2"]=>
  array(1) {
    [0]=>
    int(15)
  }
  ["vp 3"]=>
  array(1) {
    [0]=>
    int(20)
  }
  ["vp 4"]=>
  string(1) "x"
}

https://3v4l.org/l7NBa

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • yes. I made `$arr = $Pt` and this was the output `array(1) { [0]=> string(1) "x" } array(1) { [0]=> string(1) "x" } array(1) { [0]=> string(1) "x" }` – 730wavy Sep 24 '18 at 16:32
  • In that case your question does not show what you got. I used the array from your question and got the output you want as you can see in the link. – Andreas Sep 24 '18 at 16:48