0

I'm working with array in PHP. My question is how to access PHP array values outside the for loop.

Here is the code.

<?php
$a = array("1","2");
for($i=0;$i<count($a);$i++){
#some core functionality DB related.
$val = $row['values'];
$b = explode(',',$val);
}
print_r($b);

$fin = array_combine($a,$b);
print_r($fin);
?>

I want to combine both the array, but I'm not getting array b. How to access the array values outside for loop?

Expected output:

Array ( [0] => 7 [1] => 6 ) // b array
Array ( [1] => 7 [2] => 6 ) // fin array
9
  • 1
    you have to combine array in side for loop because $b is taking last value fetched from database so i would suggest you combine value in for loop Commented May 10, 2017 at 6:57
  • when i try that array $a is getting looped twice. so its not working. Commented May 10, 2017 at 8:41
  • is $row['values'] having multiple value in code or not Commented May 10, 2017 at 8:43
  • yes it has and im assigning all values to $val Commented May 10, 2017 at 8:48
  • how you assigning all values because you are not concatenating the $val Commented May 10, 2017 at 8:54

3 Answers 3

1

Try appending all the results to the original array. Something like this.

<?php
$a = array("1","2");

for($i=0;$i<count($a);$i++){
#some core functionality DB related.
$val = $row['values'];
$temp_array = explode(',',$val);
    for($j=0;$j<sizeof($temp_array);$j++){
        array_push($a,$temp_array[$j]);
    }
}
print_r($a);

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

3 Comments

instead of directly assigning the explode value you can, use array_push to keep all the values in $b, you can edit the code to add the below value, array_push($b, explode(',',$val)); instead of $b = explode(',',$val);
Im getting the last value in the array @nandan. when i try your approach im getting blank page Aoxi
@nandan im using for loop for DB only as im getting values from a table. and i'm trying to assign that value to $a array as key=> value pair
0

I keep staring at the original process and it seems unnecessarily convoluted. Does this not do what you wish:

$a=["1","2"];
foreach($a as $k=>$v){
    // I don't know if you are using $k or $v in your DB actions
    // On 1st iteration, $k=0 & $v="1"; on 2nd: $k=1 & $v="2"
    $fin[$v]=explode(',',$row['values']);
}
var_export($fin);

If this is wrong please let me know how it is wrong so I can better understand the question.

2 Comments

@Vikky great. I just want to mention that if this was my project, I'd probably look into optimizing my process so that I only need to make 1 call to the database, rather than a call for each value in $a. ...I'll leave you to think about that possibility.
yes absolutely @mick. for simple understanding i have mention $a array, but in real time i get this from db and convert it to array. thank you for your help
0

If I'm not wrong you are looking for this code with above what I have discussed with you

<?php
$a = array("1","2");
$b = array();
for($i=0;$i<count($a);$i++){
#some core functionality DB related.
$b[] = $row['values'];
}
print_r($b);

$fin = array_combine($a,$b);
print_r($fin);

?>

2 Comments

yes but only thing is we have to declare $b = array(); before for loop to access the array. rest is same thanks @amit
@Vikky I have changed

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.