Jump to content

Recommended Posts

I need help for get this result in foreach loop.

ok64|33|37|Test1|Test2|Test3|

 

But with my looping i get this result:

ok64|Test1|33Test2|37|Test3|

<?
$check = array('ok');
foreach($results as $rows) {
  array_push($check, $rows['votes']."|");
  array_push($check, $rows['value']."|");
 }
?>
Link to comment
https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/
Share on other sites

Your code looks strange to me. What is that first line supposed to represent? I don't know what that syntax is.

 

Secondly - why do you want to load your $check array with values followed by the pipe (|) symbol? What purpose does that serve you - combining data with an arbitrary character? Plus you have alternating values in your array - a vote, then a value, a vote, then a value. Pictorially you will have this:

 

$check[0] 'vote1|'

$check[1] 'value1|'

$check[2] 'vote2|'

$check[3] 'value2|'

$check[4] 'vote3|'

and so on.

 

How will this be useful to you?

I see we overlapped in our responses...

 

So you have two inputs - votes and values. What exactly are these? Strings with the pipe symbol separating the individual values? But you want to move them out of strings and into an array? Your example is confusing since the inputs look like the same thing as the desired output, and they both look like strings.

 

If you inputs are string then do this:

$votes_ar = explode('|',$vote_results);
$values_ar = explode('|',$value_results);
Now you will have two arrays containing your two results. Although, if you are building those input strings yourself, why are you making strings to begin with?

To get what you are asking for: ok64|33|37|Test1|Test2|Test3|

 

$check = array();
foreach($results as $rows) {
  $check[$rows['votes']] = $rows['value'];
}

$out = 'ok' . implode('|', $check) . '|' . implode('|', array_keys($check)) . '|';
echo $out;
That's probably not the way I would send that data, though. JSON is capable of representing arrays and objects. So I would probably just json_encode the $check array instead of building the string. But I'm not a JavaScript guru, so I can't explain how to use it from there.

 

How do you know (in JS) which part of the string is votes and which is values? Why not just send those two arrays from PHP?

 

$out = array('votes' => array(), 'values' => array());
foreach($results as $rows) {
  $out['votes'][] = $rows['votes'];
  $out['values'][] = $rows['values'];
}

echo json_encode($out);
That should give you (in JS) an object with two array properties (votes and values), then you don't need to split them in JS.

i have tried with this but i get this result: "ok","67|","38|","38|","Test1|","Test2|","Test3|"

Pls help me to get this result: "ok","67|38|38|","Test1|Test2|Test3|"

<?
$answers = array();
$votes = array();
$check = array('ok');

foreach($results as $rows) {
   $votes[] = $rows['votes']."|";
   $answers[] = $rows['value']."|";
  }

$test = array_merge($check, $votes, $answers);

echo json_encode($test);

Thank you for reply DavidAM

With this code i get result from array:

function(data) {
 if (data[0] == "ok") {
  show_results(id,data[1],data[2], 'voted');
}

data[1] = votes
data[2] = values

<button onClick="show_results('2','67|38|38|','Test1|Test2|Test3|','no'); return false;">Result</button>
Edited by balkan7

Yeah, I'm a bit confused as to why you would want the data in that format. Should definitely use a more logical structure. I would suggest something like this

 

array
(
    [status] => 'ok',
 
    ['results'] => array
    (
        'test1' => '67',
        'test2' => '37',
        'test3' => '37'
    )
 
)

 

Then encode it in JSON and send to JS which will decode it into an array.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.