0

I am using return array function to return multiple query results in PHP. The function I used is: $main_ar=return array($query_result1,$query_result2). It is giving me two arrays at once like below:

Array ( [word_id] => 3 [main_word] => happy [n1] => Jason [v1] => plays [n2] => football  ) 
Array ( [word_id] => 4 [main_word] => dog [n1] => Carter  [v1] => plays [n2] => fetch_ball )

Now I want to combine these two array so that n1 index of first array can be printed with other indexes of second array. As a result I can get Jason dog Jason Carter Jason plays Jason fetch_ball etc. But using foreach loop is not helping me in here. It is giving 2 index values at the same time:

foreach ($main_ar as $value) {
    foreach ($value as $last) {
        echo $last['n1'];
    }
}

Output is: JasnCarter. What's the possible solution?

1 Answer 1

1

Get n1 from the first array and loop the second one.

<?php 
$first=$array[0]['n1'];
foreach( $array[1] as $key=>$value ){ 

   //skip word_id
   if($key == 'word_id')continue; 

   echo $first.' '.$value;
  }
Sign up to request clarification or add additional context in comments.

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.