balkan7 Posted June 26, 2014 Share Posted June 26, 2014 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']."|"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/ Share on other sites More sharing options...
davidannis Posted June 26, 2014 Share Posted June 26, 2014 Looks like you want to loop through the votes first then loop through the values. Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483225 Share on other sites More sharing options...
balkan7 Posted June 26, 2014 Author Share Posted June 26, 2014 YesThis 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| Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483227 Share on other sites More sharing options...
ginerjm Posted June 26, 2014 Share Posted June 26, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483228 Share on other sites More sharing options...
ginerjm Posted June 26, 2014 Share Posted June 26, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483229 Share on other sites More sharing options...
balkan7 Posted June 26, 2014 Author Share Posted June 26, 2014 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("|"); Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483230 Share on other sites More sharing options...
DavidAM Posted June 26, 2014 Share Posted June 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483231 Share on other sites More sharing options...
balkan7 Posted June 26, 2014 Author Share Posted June 26, 2014 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); Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483232 Share on other sites More sharing options...
balkan7 Posted June 26, 2014 Author Share Posted June 26, 2014 (edited) 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] = votesdata[2] = values <button onClick="show_results('2','67|38|38|','Test1|Test2|Test3|','no'); return false;">Result</button> Edited June 26, 2014 by balkan7 Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483233 Share on other sites More sharing options...
Psycho Posted June 26, 2014 Share Posted June 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483238 Share on other sites More sharing options...
balkan7 Posted June 26, 2014 Author Share Posted June 26, 2014 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); Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483239 Share on other sites More sharing options...
Jacques1 Posted June 26, 2014 Share Posted June 26, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/289288-array-push-in-foreach-loop/#findComment-1483242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.