-1

How to looping multi array using foreach()? Or how to define $looping on for()? I'm tired of using AND logic on foreach().

This is my code:

<?php
$b=["a","b","c","d","5"];
$a=["1","3","4","5"];
foreach($a as $a && $b as $b) {
    echo $a.$b;
}

// AND logic Error

$tipe=trim(fgets(STDIN));
$post=trim(fgets(STDIN));
if($tipe == "1") {
    $url="http://example.com/api"
    $postdata="post_data={$post[$x]}";
}

for($x=0;$x<10;$x++) {
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_exec($ch);
}
// $x not defined
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • 1
    Could you provide a sample of code? What have you ever tried? Also, I don't understand the second part of the question, with `$looping`. – Anthony Mar 15 '18 at 15:06
  • 1
    I bet the manual will be helpful for you. http://php.net/manual/en/control-structures.for.php and http://php.net/manual/en/control-structures.foreach.php – Andreas Mar 15 '18 at 15:08
  • 2
    `foreach($a as $a` $a is your array and you assign $a as your value. Meaning the value will overwrite the array. – Andreas Mar 15 '18 at 15:10
  • 2
    Possible duplicate of [PHP: Best way to iterate two parallel arrays?](https://stackoverflow.com/questions/10132210/php-best-way-to-iterate-two-parallel-arrays) – Andreas Mar 15 '18 at 15:12
  • `// $x not defined` Well `$x` is not used either – RiggsFolly Mar 15 '18 at 15:15
  • 1
    `Im tired using AND logic on foreach` and it does not work either `Parse error: syntax error, unexpected '&&' (T_BOOLEAN_AND), expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in` – RiggsFolly Mar 15 '18 at 15:17
  • On my code, if i using for() $x not defined, and if im using foreach, i can't looping 2 array – user3677931 Mar 15 '18 at 15:19
  • @user3677931..seems to me you're getting bigtime help... go read those tutorials and grow a spine, get your feet wet and hit your hit twice or more... then you get the experience of coding! http://idownvotedbecau.se/noresearch/ – ZF007 Mar 15 '18 at 17:34

2 Answers2

0

$x is not defined because the execution is stoped at your first foreach loop. You can't use foreach like you did but you can change it to:

$b = ["a","b","c","d","5"];
$a = ["1","3","4","5"];

foreach($a as $key => $value) {
    echo $value . $b[$key]; // just make sure to have at least the same number of elements in the $b array as the $a array has
}

However, it is hard to understand what exactly you want to achieve with this code and what it is supposed to do.

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • notice: Array to string conversion in /storage/sdcard1/telegram.php on line 70 PHP Notice: Array to string conversion. $postdata="chat_id={$cid}&photo={$jpg}&caption={$status}"; i want looping $jpg=array and $status=array on $postdata – user3677931 Mar 15 '18 at 15:46
0

You have to first take the max count of array values & then iterate the loop till max count, I hope this will help you

$b=["a","b","c","d","5"];
$a=["1","3","4","5"];

$max = ( count($a) > count($b) ) ? count($a) : count($b);

for( $i=0 ; $i<$max; $i++){
  $strA = (isset($a[$i])) ? $a[$i] : '';
  $strB = (isset($b[$i])) ? $b[$i] : '';

  echo $strA . $strB;
}
Gajanan Kolpuke
  • 155
  • 1
  • 1
  • 14