2
   $videoskey_list = explode(',',$result[$x]["videos_key"]);
   $videosname_list = explode(',',$result[$x]["videos_name"]); 

   foreach($videoskey_list as $videoskey => $videos_key && $videosname_list as $videosname => $videos_name) 
    {
        echo '  <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_name.' </button>';
    }

How do i use && in foreach. It should work, right? Or PHP do not support && in foreach?

Error

Parse error: syntax error, unexpected '&&' (T_BOOLEAN_AND),

4
  • 3
    No it doesn't!! Commented Jun 29, 2017 at 5:08
  • 1
    what? why would you do that.... && is a boolean comparison operator. Foreach takes an array as input... Commented Jun 29, 2017 at 5:08
  • Use the key of $videoskey_list to access the other array, you can't pair multiple arrays in a foreach. Commented Jun 29, 2017 at 5:10
  • The real issue here will come if those 2 arrays are not in sync, because there is no direct relationship between them, you could have a situation where one is longer then the other or the order is wrong. This may not be possible in your case but it's something to consider. It seems problematic to me. Commented Jun 29, 2017 at 5:14

3 Answers 3

5

If your keys and values are both means this should be work...

$videoskey_list = explode(',',$result[$x]["videos_key"]);
$videosname_list = explode(',',$result[$x]["videos_name"]);
foreach( $videoskey_list as $index => $videos_key ) {
   echo '  <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videosname_list[$index].' </button>';
}

EDITED: If we use array_combine The both array should be equal. Here We can use How many keys we have that much Output will get here.

In array_merge The both arrays are merged so we can't fine the same key and value.

Explanation For this Answer:

First we get an videoskey_list As key and Value. If match the Key with the Value. We can use videoskey_list's key as videosname_list's index. For example check here with this code.

$numbers = array('1','2','3');
$alpha = array('a','b','c');
foreach( $numbers as $index => $number ) {
  echo $number .'->'. $alpha[$index] .'<br />';
}
Sign up to request clarification or add additional context in comments.

6 Comments

Its wrong where you have define $videoskey_list @JoshPoor
@RJParikh No this just works like an array_combine But here the key is the main role so if we depend key means no problem in array_combine if don't have equal array it throws an error.
Your case will only work if $videoskey_list and $videosname_list both have same number of array values. If its different then its not work.
Hahaha Array_combine is also the same problem @RJParikh .In an array, combine wants equal array but here we can use how many keys we have and execute well.
Ok, I have checked its work if we use array_merge() and its not working with array_combine().
|
4
$videos_list = array_combine($videoskey_list, $videosname_list);
foreach($videos_list as $key => $name) {
    // ...
}

1 Comment

array combine can be difficult, it will blowup if the arrays are not the same size, that said your code is not technically wrong I would just count both arrays first if( count( $videoskey_list ) == count( $videosname_list ) ) and handle errors as needed.
2

You can use array_combine()

$videoskey_list = explode(',',$result[$x]["videos_key"]);
$videosname_list = explode(',',$result[$x]["videos_name"]); 

foreach (array_combine($videoskey_list, $videosname_list) as $videos_key => $videos_val) {
    echo '  <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_val.' </button>';
}

OR

Use array_merge()

foreach (array_merge($videoskey_list, $videosname_list) as $videoskey => $videos_val) {
        echo '  <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_val.' </button>';
    }

Demo

<?php
$videoskey_list = array('111','222','333');
$videosname_list = array('test','abc','xyz');

foreach (array_combine($videoskey_list, $videosname_list) as $videos_key => $videos_val) {
  echo '  <button id="playtrailer" class="playtrailer" data-src="'.$videos_key.'"> '.$videos_val.' </button>';
}

Output

<button id="playtrailer" class="playtrailer" data-src="111"> test </button>  
<button id="playtrailer" class="playtrailer" data-src="222"> abc </button>  
<button id="playtrailer" class="playtrailer" data-src="333"> xyz </button>

Demo Link: Click Here

2 Comments

Error in both answers Undefined variable: videos_name in
I have edited answer. Just to pass it on foreach and use it like $videos_val['videos_name'] @JoshPoor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.