I am trying to insert batch query in code igniter, I am not able to make array_merge work. Don't know whats the problem. M getting blank array.
$epin_amt = $this->input->post('amount');
$qty = $this->input->post('qty');
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
array_merge($data, $array);
}
print_r($data) ; // Produce : array( )
You have to assign the merged array back to your $data variable:
<?php
$epin_amt = /*$this->input->post('amount')*/ 5;
$qty = /*$this->input->post('qty')*/6;
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
$data = array_merge($data, $array);
}
print_r($data) ;
array_merge returns array. You need something like this:
$result = array_merge($data, $array);
You are merging the arrays but it's not done by reference so you're throwing the resulting array away. array_push() it instead, that will keep adding the arrays to your $data array:
<?php
$epin_amt = 10;
$qty = 20;
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
array_push($data, $array);
}
print_r($data) ;
Related
how to create dummy data based on what we give number like this format
for example when i give $var=2, so it will create two, when i give $var=100, it will create 100 like this using arrays in php
create like this based on number given, give 2 create like this
[{"email":"test#test.com"},{"email":"test#test.com"}]
give 4 create like this
[{"email":"test#test.com"},{"email":"test#test.com"},{"email":"test#test.com"},{"email":"test#test.com"}]
Use array_fill:
$emails = array_fill(0, 100, 'test#test.com');
With your structure it is:
$emails = array_fill(0, 100, ['email' => 'test#test.com']);
You could do this:
<?php
$dummy = array();
$dummyAmount = 100;
for($i = 0; $i < $dummyAmount; $i++){
$dummy[] = array("email" => "test#test.com");
}
?>
Try this:
$a = [];
$var = 100;
for( $i = 0; $i < $var; ++$i ) {
$a[] = [
'email' => 'test#test.com'
];
}
$json = json_encode($a)
Well, use a loop with as many circles as you need.
And inside the loop, create your email addresses.
Like a so:
$amount = 100;
$emails = [];
for ($i = 0; $i < $amount; $i++) {
$emails[] = ['email' => 'test#test.com' ];
}
And then json encode it to get your expected output:
$emailJSON = json_encode($emails);
I have a string:
$content = "test,something,other,things,data,example";
I want to create an array where the first item is the key and the second one the value.
It should look like this:
Array
(
[test] => something
[other] => things
[data] => example
)
How can I do that? It's difficult to search for a solution because I don't know how to search this.
It's very similar to this: Explode string into array with key and value
But I don't have a json array.
I tried something like that:
$content = "test,something,other,things,data,example";
$arr = explode(',', $content);
$counter = 1;
$result = array();
foreach($arr as $item) {
if($counter % 2 == 0) {
$result[$temp] = $item;
unset($temp);
$counter++;
} else {
$temp = $item;
$counter++;
continue;
}
}
print_r($result);
But it's a dirty solution. Is there any better way?
Try this:
$array = explode(',',$content);
$size = count($array);
for($i=0; $i<$size; $i++)
$result[$array[$i]] = $array[++$i];
Try this:
$content = "test,something,other,things,data,example";
$data = explode(",", $content);// Split the string into an array
$result = Array();
$size = count($data); // Calculate the size once for later use
if($size%2 == 0)// check if we have even number of items(we have pairs)
for($i = 0; $i<$size;$i=$i+2){// Use calculated size here, because value is evaluated on every iteration
$result[$data[$i]] = $data[$i+1];
}
var_dump($result);
Try this
$content = "test,something,other,things,data,example";
$firstArray = explode(',',$content);
print_r($firstArray);
$final = array();
for($i=0; $i<count($firstArray); $i++)
{
if($i % 2 == 0)
{
$final[$firstArray[$i]] = $firstArray[$i+1];
}
}
print_r($final);
$content = "test,something,other,things,data,example";
$x = explode(',', $content);
$z = array();
for ($i=0 ; $i<count($x); $i+=2){
$res[$x[$i]] = $x[$i+1];
$z=array_merge($z,$res);
}
print_r($z);
I have tried this example this is working file.
Code:-
<?php
$string = "test,something|other,things|data,example";
$finalArray = array();
$asArr = explode( '|', $string );
foreach( $asArr as $val ){
$tmp = explode( ',', $val );
$finalArray[ $tmp[0] ] = $tmp[1];
}
echo "After Sorting".'<pre>';
print_r( $finalArray );
echo '</pre>';
?>
Output:-
Array
(
[test] => something
[other] => things
[data] => example
)
For your reference check this Click Here
Hope this helps.
You could able to use the following:
$key_pair = array();
$arr = explode(',', $content);
$arr_length = count($arr);
if($arr_length%2 == 0)
{
for($i = 0; $i < $arr_length; $i = $i+2)
{
$key_pair[$arr[$i]] = $arr[$i+1];
}
}
print_r($key_pair);
$content = "test,something,other,things,data,example";
$contentArray = explode(',',$content);
for($i=0; $i<count($contentArray); $i++){
$contentResult[$contentArray[$i]] = $contentArray[++$i];
}
print_r( $contentResult);
Output
Array
(
[test] => something
[other] => things
[data] => example
)
$contentResult[$contentArray[1]] = $contentArray[2];
$contentResult[$contentArray[3]] = $contentArray[4];
$contentResult[$contentArray[5]] = $contentArray[6];
I need to use Json array in the php code.
The problem is that I'm in a for loop and need to separate the array in 2 and then want to merge it. but so far it didn't work.
I use it to have a graph (jqxChart).
Here is my code
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
$time[$i] = (hexdec($hour[$i]));
$orders1[] = array(
'OrderDate' => $time[$i],
);
}else{
$hour[$i] = $hour[$i] + 1;
$orders2[] = array(
'ProductName' => $hour[$i],
);
}
}
$orders[] = array_merge_recursive( $orders1[], $orders2[] );
}
echo json_encode($orders);
Thanks
try this code,
$orders1 = array();
$orders2 = array();
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
....
$temp1 = array(
'OrderDate' => $time[$i],
);
array_push($orders1, $temp1);
}else{
....
$temp2 = array(
'ProductName' => $hour[$i],
);
array_push($orders2, $temp2);
}
}
}
$orders = array_merge( $orders1, $orders2 );
echo json_encode($orders);
Remove the square brackets. Instead of:
$orders[] = array_merge_recursive($orders1[], $orders2[]);
^^ ^^ ^^
Just put:
$orders = array_merge($orders1, $orders2);
From the queried database, we got this
foreach ($child_posts as $child_post) {
$child_id = $child_post->ID;
$dayOfWeek = get_post_meta($child_id,'wpcf-day-of-week', true);
$time = get_post_meta($child_id,'wpcf-time', true);
$class[] = array('day' => $dayOfWeek, 'time' => $time, 'value' => $child_id);
}
{"classes":[
{"day":"7","time":"1500","value":13574},
{"day":"7","time":"1800","value":13573},
{"day":"4","time":"1900","value":11346},
{"day":"6","time":"1100","value":11494},
{"day":"5","time":"1800","value":11362},
{"day":"7","time":"1700","value":13572},
{"day":"6","time":"1600","value":11498},
{"day":"6","time":"1500","value":11496}]}
Day = Mon - Sun
Time = Military time
Value = something I need, but do not need sorting
I need to sort first by day, then by time. And get back the database in the format like how it was
{"classes":[
{"day":"4","time":"1900","value":11346},
{"day":"5","time":"1800","value":11362},
{"day":"6","time":"1100","value":11494},
{"day":"6","time":"1500","value":11496},
{"day":"6","time":"1600","value":11498},
{"day":"7","time":"1500","value":13574},
{"day":"7","time":"1700","value":13572},
{"day":"7","time":"1800","value":13573}]}
I tried using the method in the php manual but I still can't really get it.
This is what I did
foreach ($class as $key => $row) {
$day[$key] = $row['day'];
$time[$key] = $row['time'];
$value[$key] = $row['value'];
}
$class[] = array_multisort($day, SORT_DESC, $time, SORT_ASC, $class);
I'm know what I should expect, but I don't know how to go around getting it. Hopefully someone can help out here :)
My orignial script:
$childargs = array(
'post_type' => 'class',
'numberposts' => -1,
'meta_query' => array(array('key' => '_wpcf_belongs_instructor_id', 'value' => $instructor_post_id))
);
$child_posts = get_posts($childargs);
//$child_posts = types_child_posts(‘class’);
foreach ($child_posts as $child_post) {
$child_id = $child_post->ID;
$dayOfWeek = get_post_meta($child_id,'wpcf-day-of-week', true);
$time = get_post_meta($child_id,'wpcf-time', true);
$class[] = array('day' => $dayOfWeek, 'time' => $time, 'value' => $child_id);
}
echo json_encode(
array("classes" => $class)
)
?>
<?php
$json = '{"classes":[
{"day":"7","time":"1500","value":13574},
{"day":"7","time":"1800","value":13573},
{"day":"4","time":"1900","value":11346},
{"day":"6","time":"1100","value":11494},
{"day":"5","time":"1800","value":11362},
{"day":"7","time":"1700","value":13572},
{"day":"6","time":"1600","value":11498},
{"day":"6","time":"1500","value":11496}]}';
$array = json_decode($json,true);
$temp = $array['classes'];
foreach($temp as $key=>$value)
{
$day[] = $value['day'];
$time[] = $value['time'];
}
array_multisort($day,SORT_ASC,$time,SORT_ASC,$temp);
$array['classes'] = $temp;
echo json_encode($array);
?>
try changing your code to .I assume that $classes contains the json_decoded content.
$temp = $classes['classes'];
foreach ($temp as $key => $row) {
$day[] = $row['day'];
$time[] = $row['time'];
$value[] = $row['value'];
}
array_multisort($day, SORT_DESC, $time, SORT_ASC, $temp);
$classes['classes'] = $temp;
echo json_encode($classes);
To sort an array of rows returned from MYSql by one column.
The problem here is to sort courses by session (1,2) and period (1,2,3) If the session = 3 it means both so in preparing the table for sorting and printing if session == 3 then insert a row 1, and a row 2. Then do the sort. The table contents are in $coursesr.
$ar = array();
if ($order == "session"){
for ($i = 0; $i < count($coursesr); $i++){
$ar[0][] = (($coursesr[$i]['session'])*10)+$coursesr[$i]['period'];
$ar[1][] = $i;
}
$b = array_multisort($ar[0],SORT_ASC,SORT_NUMERIC,$ar[1],SORT_ASC,SORT_NUMERIC);
}else{
for ($i = 0; $i < count($coursesr); $i++){
$ar[0][] = ucwords($coursesr[$i]['lastname']);
$ar[1][] = $i;
}
array_multisort($ar[0],SORT_ASC,SORT_NATURAL,$ar[1],SORT_ASC,SORT_NUMERIC);
}
Then to get it out of the $coursesr array you have to compute the index...
for ($i = 0; $i < $num; $i++){
$r = $coursesr[$ar[1][$i]]; // simplify reference
echo "<tr>";.....
I have an array with a single value collected from database.
Within a while loop I wish to explode this for every two values.
Example:
$data = array('20;40;60;80;100;150;200;300;500;1000');
I want to explode this value and end up with the following loop:
$lowprice = "20";
$highprice = "40";
I couldn't find any examples of this.
Many thanks everyone who answered!
You can use preg_match_all().
Example:
$text = '20;40;60;80;100;150;200;300;500;1000';
preg_match_all("/([^;]+);([^;]+)/", $text, $pairs, PREG_SET_ORDER);
foreach ($pairs as $pair) {
// ...
$lowvalue = $pair[1];
$highvalue = $pair[2];
// ...
}
If you really must use explode and a while loop, the following will also work:
$text = '20;40;60;80;100;150;200;300;500;1000';
$data = explode(';', $text);
$i = 0;
$count = count($data);
while ($i < $count) {
// ...
$lowvalue = $data[$i++];
$highvalue = $data[$i++];
// ...
}
If you just want the first and second values as your low/high:
$data = array('20;40;60;80;100;150;200;300;500;1000');
list($lowprice,$highprice) = explode(';',current($data));
echo '$lowprice=',$lowprice,PHP_EOL;
echo '$highprice=',$highprice,PHP_EOL;
If you want an array of lows and highs:
$data = array('20;40;60;80;100;150;200;300;500;1000');
$lowprices = $highprices = array();
$data = explode(';',current($data));
$dataCount = count($data);
var_dump($data);
for ($i=0; $i < $dataCount; $i += 2) {
$lowprices[] = $data[$i];
$highprices[] = $data[$i+1];
}
echo '$lowprices=';
var_dump($lowprices);
echo '$highprices=';
var_dump($highprices);
EDIT
Why not start out with a proper array of values in the first place: it would simplify this a lot
$data = array(20,40,60,80,100,150,200,300,500,1000);
list($lowprice,$highprice) = $data;
echo '$lowprice=',$lowprice,PHP_EOL;
echo '$highprice=',$highprice,PHP_EOL;
or
$data = array(20,40,60,80,100,150,200,300,500,1000);
$lowprices = $highprices = array();
$dataCount = count($data);
var_dump($data);
for ($i=0; $i < $dataCount; $i += 2) {
$lowprices[] = $data[$i];
$highprices[] = $data[$i+1];
}
echo '$lowprices=';
var_dump($lowprices);
echo '$highprices=';
var_dump($highprices);
Explode initially on the ;. Then in a for loop incrementing by 2, you can assign the current and next values to a sub-array:
$initarray = explode(";", "20;40;60;80;100;150;200;300;500;1000");
$num = count($initarray);
// Main array to hold subarrays
$outputarray = array();
for ($i=0; $i<$num; $i=$i+2) {
// Add the current pair ($i and $i+1) to a sub-array
$outputarray[] = array("lowprice"=>$initarray[$i], "highprice"=>$initarray[$i+1]);
}
Outputs:
Array
(
[0] => Array
(
[lowprice] => 20
[highprice] => 40
)
[1] => Array
(
[lowprice] => 60
[highprice] => 80
)
[2] => Array
(
[lowprice] => 100
[highprice] => 150
)
[3] => Array
(
[lowprice] => 200
[highprice] => 300
)
[4] => Array
(
[lowprice] => 500
[highprice] => 1000
)
)
You may use loop like this one (using explode() and array_shift()):
$data = explode( ';', '20;40;60;80;100;150;200;300;500;1000');
while( count( $data) > 1){
$lowprice = array_shift( $data);
$highprice = array_shift( $data);
}
Or use for loop (should be more effective than calling count() in every iteration):
$count = count( $data);
for( $i = 0; $i < $count; $i+=2){}