1

I have following code:

foreach(array_combine($names, $hashids) as $name => $hashid) {
echo '<tr>
<td>'.$counter.'</td>
<td>'.$name.'</td>
<td>'.$hashid.'</a></td>
</tr>';
}

Now I want to combine one more array and iterate it's values in foreach. That's three arrays in a foreach statement. can anyone help me in adding it. Like how can we add multiple arrays in foreach statement.

For example I want something like:

foreach(array_combine($names, $hashids, $photoids) as $name => $hashid => $photoid) {
echo '<tr>
<td>'.$counter.'</td>
<td>'.$name.'</td>
<td>'.$hashid.'</td>
<td>'.$photoid.'</td>
</tr>';
}

But above code gives me error. The error I get is

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /homepages/4/d864452909/htdocs/public_html/app/index.php on line 58

7
  • share what this returns print_r(array_combine($names, $hashids, $photoids) ); Commented Mar 14, 2021 at 14:29
  • @AlwaysSunny I have added in question. Commented Mar 14, 2021 at 14:32
  • no you didn't I meant print the result of those three array combine before foreach loop AND share with us Commented Mar 14, 2021 at 14:33
  • @AlwaysSunny It doesn't return anything for three array values. But for two arrays combine it returns following Array ( [Sample] => JdpmLLdx5me7K [Big Buck Bunny 60fps 4K - Official Blender Foundation Short Film.mp4] => lWJQjwrVRZdXn ) Commented Mar 14, 2021 at 14:39
  • Exactly, what I wanted to point you that array_combine doesn't do combine of 3 array at once. so you need to share the full code with us so we can help you. :) see here: php.net/manual/en/function.array-combine.php Commented Mar 14, 2021 at 14:40

2 Answers 2

2

If I were you I will do this way with simple foreach() loop,

<?php
$names = ['Sample','Big Buck Bunny 60fps 4K - Official Blender Foundation Short Film.mp4'];
$hashid = ['JdpmLLdx5me7K','lWJQjwrVRZdXn'];
$photoids = ['url_1_of_the_image','url_2_of_the_image'];
$result = [];
foreach ($names as $key => $value) {
    $result[$key] = array(
        'name'  => $names[$key],
        'hashid' => $hashid[$key],
        'photoid'    => $photoids[$key],
    );
}
foreach($result as $k => $v) {
    echo '<tr>
         <td>'.$k.'</td>
         <td>'.$v['name'].'</td>
         <td>'.$v['hashid'].'</td>
         <td>'.$v['photoid'].'</td>
         </tr>';
}

WORKING DEMO: https://3v4l.org/61Tcr OR https://3v4l.org/133c3

Sign up to request clarification or add additional context in comments.

1 Comment

@Andy glad it helps you :)
1

You can merge the objects as below

$arr = array();

foreach ($names as $value) {
    array_push($arr, $value);
}

for($x = 0; $x < count($arr); $x++) {
  foreach($arr[$x] as $y => $val) {
    $arr[$x] = (object) array_merge((array) $arr[$x], (array) $hashids[$x]);
    $arr[$x] = (object) array_merge((array) $arr[$x], (array) $photoids[$x]);
  }
}

for($x = 0; $x < count($arr); $x++) {
echo '<tr>
<td>'.$arr[$x]->counter.'</td>
<td>'.$arr[$x]->name.'</td>
<td>'.$arr[$x]->hashid.'</td>
<td>'.$arr[$x]->photoid.'</td>
</tr>';
}

Comments

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.