This is because you're re-assigning $dataPoints as a new array on each loop.
Change it to:
$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);
This will append a new array to the end of $dataPoints
This is because you're re-assigning $dataPoints as a new array on each loop.
Change it to:
$dataPoints[] = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);
This will append a new array to the end of $dataPoints
use array_merge($array1,$array2) make it simple use two array one for use in iteration and another for storing the final result. checkout the code.
$dataPoints = array();
$dataPoint = array();
$brands = array(
"COCACOLA","DellChannel","ebayfans","google","microsoft","nikeplus","amazon");
foreach($brands as $value){
$resp = GetTwitter($value);
$dataPoint = array("x"=>$resp['friends_count'],"y"=>$resp ['statuses_count']);
$dataPoints = array_merge($dataPoints,$dataPoint);
}
You're aaalmost there. Just add the level to the array creation :)
$levels = array('low', 'medium', 'high');
$attributes = array('fat', 'quantity', 'ratio', 'label');
foreach ($levels as $key => $level):
foreach ($attributes as $k =>$attribute):
$variables[$level][] = $attribute . '_' . $level; // changed $variables[] to $variables[$level][]
endforeach;
endforeach;
echo '<pre>' . print_r($levels,1) . '</pre>';
echo '<pre>' . print_r($variables,1) . '</pre>';
Output
Array
(
[low] => Array
(
[0] => fat_low
[1] => quantity_low
[2] => ratio_low
[3] => label_low
)
[medium] => Array
(
[0] => fat_medium
[1] => quantity_medium
[2] => ratio_medium
[3] => label_medium
)
[high] => Array
(
[0] => fat_high
[1] => quantity_high
[2] => ratio_high
[3] => label_high
)
)
$levels = ['low', 'medium', 'high'];
$attributes = ['fat', 'quantity', 'ratio', 'label'];
$result = [];
foreach ($levels as $level) {
$result[$level] = [];
foreach ($attributes as $attribute) {
$result[$level][] = $attribute . '_' . $level;
}
}
var_dump($result);
You can use php's array_chunk method. Notice its use in modified code below:
if($query->num_rows() > 0):
foreach($query->result() as $row):
$data[$row->id] = $row->shortname;
endforeach;
$data = array_chunk($data, 2);
return $data;
$nGroup = 1; $curGroup = '';
$nRow = 0;
foreach ($query->result() as $row){
if ($nRow++ % 2 == 0){ // change 2 to your limiter
$curGroup = 'Group ' . $nGroup++;
$data[$curGroup] = array();
}
$data[$curGroup][$row->id] = $row->shortname;
}
Something like that? Keep track of the row you're on, the current group your adding to, and the group name. Then, every 2nd (or Nth) set, switch groups.
Try this.
$j = -1;
$newArray = [];
foreach($items as $item){
if ($item['SUBMODEL_NAME']) {
$j++;
}
$newArray [$j][] = $item ;
}
Another way to do it-
$result = [];
foreach($array as $k=>$v){
$i=0;
if($v['SUBMODEL_NAME']==''){
$i++;
$k = $k-1;
}
$result[$k][$i]= $v;
}
$result = array_values($result);
print_r($result);
DEMO: https://3v4l.org/3aTgR
Two good options:
If the todo_id is unique, let it be the key:
$js_arr[$row['todo_id']] = $row['todo_content'];
Or for a multi-dimensional array, needed if you have more than just todo_content:
$js_arr[] = array('todo_content' => $row['todo_content'], 'todo_id' => $row['todo_id']);
Simply nest the items you want inside an array:
$js_arr[] = [
'todo_content' => $row['todo_content'],
'todo_id' => $row['todo_id']
];
The $js_arr[] part cannot ever be anything else, because any other syntax will not unconditionally add an element to the end of your multidimensional array.
$sql = "select category.name,group_concat(images.image_name) from category inner join images on (category.id = images.category_id) group by category.name";
$table = mysql_query($sql);
$arr = array();
while($row = mysql_fetch_row($table))
{
$row['image_name'] = explode(",", $row['image_name']);
$arr[] = $row;
}
The tool code you will need: mysqli. Now suppose you know it.
Solution 1
The slowest. Just fetch all the lines you need, then use a for loop to make it what you want.
Solution 2
Use GROUP_BY + GROUP_CONCAT function in sql.
I am in a hurry. So which do you like best, and do you need a further example?
I don't think this is a restrict Wordpress question, but you might try
$args = array(
'posts_per_page' => '-1',
'post_type' => 'work',
'orderby' => 'ID',
'order' => 'DESC',
);
$data = array('work' => array());
$loop = new WP_Query($args);
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post();
$id = $loop->post->ID;
$attachments=array();
if(get_field('work')):
while(has_sub_field('work')):
$attachment_id = get_sub_field('image');
$caption = the_sub_field('caption');
$image = wp_get_attachment_image_src( $attachment_id, 'work' );
$attachments[$attachment_id]=array("caption"=>$caption,"url"=>$image);
endwhile;
endif;
$data['work'][$id] = array(
'title' => apply_filters( 'the_title', $loop->post->post_title ),
'content' => apply_filters( 'the_content', $loop->post->post_content ),
'items' => $attachments;
);
endwhile;
endif;
wp_reset_postdata();
Put the second loop in a function:
$loop = new WP_Query($args);
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post();
$id = $loop->post->ID;
$data['work'][$id] = array(
'title' => apply_filters( 'the_title', $loop->post->post_title ),
'content' => apply_filters( 'the_content', $loop->post->post_content ),
'items' => get_acf_field( $id ),
);
endwhile;
endif;
function get_acf_field( $post_id ) {
$result = array();
if( get_field( 'work', $post_id ) ) {
while(has_sub_field('work')) {
$attachment_id = get_sub_field('image');
$caption = the_sub_field('caption');
$image = wp_get_attachment_image_src( $attachment_id, work );
$result[] = array( 'caption' => $caption, 'url' => $image );
}
}
return $result;
}
I hope this helps.