I need help with php!

I try to connect 2 database table on json encode…

<?php 

$result1 = mysqli_query($conn, “SELECT * FROM img”);
while($row1 = mysqli_fetch_assoc($result1))
$test1[] = $row1[“imageid1”];
$js_array1 = json_encode($test1);

$result2 = mysqli_query($conn, “SELECT * FROM img_not”);
while($row2 = mysqli_fetch_assoc($result2))
$test2[] = $row2[“imageid2”];
$js_array2 = json_encode($test2);

echo "var Uh3uR3QVwr = ". $js_array1 . $js_array2 . “;”;
?>

php result:
var Uh3uR3QVwr = [“l5FxqUBeyp8.png”,“ksdAs4LBRq8.png”][“ttttttttttttttttttt.png”,“wwwwwww.png”];

i need help how to fix it in this form:
var Uh3uR3QVwr = [“l5FxqUBeyp8.png”,“ksdAs4LBRq8.png”,“ttttttttttttttttttt.png”,“wwwwwww.png”];

can someon help me?

Take a look at the inbuilt PHP function array_merge()

1 Like

send code bro how to ?

$merged = array_merge($js_array1, $js_array2);
print_r($merged);
RESULT:

Array
(
    [0] => l5FxqUBeyp8.png
    [1] => ksdAs4LBRq8.png
    [2] => ttttttttttttttttttt.png
    [3] => wwwwwww.png
)

If you mean literally echo it on the console in the format you describe in your post then you could do something like:

$format = ""; // string variable to hold our values
foreach($merged as $value) {  // loop through array values and then
    $format .= "\"$value\","; // enclose each value in quotes and append a comma
}
$result = rtrim($format, ","); // trim the final comma from the end

echo "var Uh3uR3QVwr = [". $result . "];";
RESULT:

var Uh3uR3QVwr = ["l5FxqUBeyp8.png","ksdAs4LBRq8.png","ttttttttttttttttttt.png","wwwwwww.png"];
<?php

$result1 = mysqli_query($conn, "SELECT * FROM img");

while($row1 = mysqli_fetch_assoc($result1))

$test1 = $row1["imageid1"];

$js_array1 = json_encode($test1);

$result2 = mysqli_query($conn, "SELECT * FROM img_not");

while($row2 = mysqli_fetch_assoc($result2))

$test2 = $row2["imageid2"];

$js_array2 = json_encode($test2);

$format = "$js_array1, $js_array2"; // string variable to hold our values

foreach($merged as $value) { // loop through array values and then

$format .= "\"$value\","; // enclose each value in quotes and append a comma

}

$result = rtrim($format, ","); // trim the final comma from the end

echo "var Uh3uR3QVwr = [". $result . "];";

?>

result:
var Uh3uR3QVwr = [“l5FxqUBeyp8.png”, “wwwwwww.png”];

is short result i dont know why…
i need this:
var Uh3uR3QVwr = [“l5FxqUBeyp8.png”,“ksdAs4LBRq8.png”,“ttttttttttttttttttt.png”,“wwwwwww.png”];

You need to merge the arrays, then do a foreach loop on the new array.

$merged = array_merge($js_array1, $js_array2);
foreach($merged as $value) {...

its working thnks bro

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.