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>";.....
Related
I need to increase the value of the $ k variable every time a record of that loop is inserted in order to insert the array I have with the necessary values. Here I leave a part of the code to see if you can help me.
$k = 0;
foreach ($detalles as $d) {
$num = $d['stock'];
$val = floor($num/$limite);
for($i=0;$i<$limite;$i++) {
$arr[$i] = $val;
}
$arr[0] += $num - array_sum($arr);
$this->transac_detail_temp->save([
'id_trx_header'=> $id,
'producto' => $d['id_producto'],
'cantidad' => $arr[$k]++,
'tipo_transaccion'=> 2]
);
} // foreach detalles
I am not sure about your question but hope this will be you
Replace this code
'cantidad' => $arr[$k]++,
to
'cantidad' => $arr[$k],
and this code in last section
$k++;
so final code will be like this
$k = 0;
foreach ($detalles as $d) {
$k++; //added
//echo $k."<br>"; // if you want to check update value of $k
$num = $d['stock'];
$val = floor($num/$limite);
for($i=0;$i<$limite;$i++) {
$arr[$i] = $val;
}
$arr[0] += $num - array_sum($arr);
$this->transac_detail_temp->save([
'id_trx_header'=> $id,
'producto' => $d['id_producto'],
'cantidad' => $arr[$k], //update
'tipo_transaccion'=> 2]
);
}
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) ;
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];
The below is my array, where I need to replace the value of 'battle_health'
$battlepokemon= array();
$i = 1;
while($rows = mysql_fetch_assoc($res))
{
$path = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$rows['pkmn_id']."' ");
$pokemon = array(
'opponent_increment' => $i,
'id' => $rows['pkmn_id'],
'battle_poke'=> mysql_result($path,0,"path"),
'battle_level' => $rows['level'],
'battle_health' => $rows['health']
);
$i++;
$battlepokemon[]= $pokemon;
}
The code for replacement is:
$i = 1;
foreach ($battlepokemon as $key => $value)
{
if($value['opponent_increment'] == $opponent_increment)
{
$value['battle_health'] = 0;
echo "Data replaced!";
}
$i++;
}
print_r($battlepokemon);
The code above is working..from start to end.. but the value is not replaced with '0' as the code says!
I think I must have missed something!
You need to transfer the reference, not the values. Add a & to the following sentence
foreach ($battlepokemon as $key => &$value)
^
I tried this just for example
<?php
$arr = array('12', '34');
foreach($arr as $key => &$value){
$value = 0;
}
var_dump($arr);
?>
Hopes it can help you
You can achieve this with for Loop Because unlike foreach loop, it doesn't perform an array copy before transversal:
$arr = array('12', '34');
for($i = 0, $count = count($arr); $i < $count; $i++){
$arr[$i] = 0;
}
var_dump($arr);
Or If you want to do with Foreach only, you need to avoid new copy of array by passing the reference like:
$arr = array('12', '34');
foreach($arr as $key => &$value)
{
$value = 0;
}
var_dump($arr);
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);