Jump to content
balkan7

Array push in foreach loop

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']."|");
 }
?>

Share this post


Link to post
Share on other sites

Looks like you want to loop through the votes first then loop through the values.

Share this post


Link to post
Share on other sites

Yes
This is result of votes:  64|33|37|

This is result of values: Test1|Test2|Test3|

 

So i want this result push in array and get this result: ok64|33|37|Test1|Test2|Test3|

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Thank you for reply ginerjm.

Actually i need this result: ok64|33|37|Test1|Test2|Test3|
Because i send it to JSON:

echo json_encode($check);

After i get result i use javascript for each result:

votes = votes.split("|");
values = values.split("|");

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Hi Psycho

Thanks for reply i found solution:

$v = implode( "|", $votes );
$v = array($v);
$a = implode( "|", $answers );
$a = array($a);
    
$x = array_merge($check, $v, $a);
echo json_encode($x);

Share this post


Link to post
Share on other sites

Have you read any of the replies?

 

Several people have told you multiple times that your strange data format makes no sense, but you just keep going. What's the matter?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.