0

The problem at hand: I have one variable ZeroC that stores two arrays. I would like to send it to another PHP file using curl but only the first array sends. To get around this issue I tried imploding the variable into a string hoping to convert the two arrays into one long string, however, only the first array goes through. The implode function doesn't place a delimiter at the last item in the array (as seen in bold).

I can't concatenate the two arrays since they are stored in the same variable. If anyone has any ideas on how to solve this issue, thank you in advance.

        if($i==0){      
          $thisk= implode("%", $comments);
          print_r($thisk);
        }

Current output after using implode:

0%Correct def statement %0%Correct function Name %0%Correct parameter 0%Correct def statement %0%Correct function Name %0%Correct parameter

Before imploding it (as an Array):

Array ( [0] => 0 [1] => Correct def statement [2] => 0 [3] => Correct function Name [4] => 0 )

Array ( [0] => 0 [1] => Correct def statement [2] => 0 [3] => Correct function Name [4] => 0 )

0

2 Answers 2

0

One possible solution: You could simply add the delimiter to the end after imploding, and for the final result strip off the delimiter again.

Example

$thisk = "";
for ($i = 0; i < 2; ++$i) {
    $thisk .= implode("%", $comments) . "%";
}
$thisk = rtrim($thisk, "%");

Or a more complete solution based on your original question:

<?php
$ZeroC = [
    ['0', 'Correct def statement', '0', 'Correct function Name', '0'],
    ['0', 'Correct def statement', '0', 'Correct function Name', '0'],
];

$result = '';
foreach ($ZeroC as $pieces) {
    $result .= implode('%', $pieces) . '%';
}
$result = rtrim($result, '%');
echo $result;

The output will be:

0%Correct def statement%0%Correct function Name%0%0%Correct def statement%0%Correct function Name%0

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

1 Comment

Thank you but it did not solve the issue. I've updated the code if you mind taking a look again.
0

Just add the % at the end manually:

if ($i==0) {      
    $thisk = implode('%', $comments) . '%';
    print_r($thisk);
}

You may want to check if $comments is not empty to prevent a single % in the resulting string.

2 Comments

Thank you but even though it added the percent sign to the last value it also created another index pointing to null (which doesn't have a percent on it). And when I tried to explode it after imploding it still separated it into two arrays. Also, it is still only sending the first array to the back end.
So we'll have to see more of your code to get a better picture of the problem. A full working example.

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.