This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 years ago.
I have an array in php that looks like this:
print_r($myArray);
Array (
[0] => Array
(
[age] => 1
[time] => 2
[name] => james
[size] => 12
[hieght] => 13
)
[1] => Array
(
[age] => 3
[time] => 1
[name] => tim
[size] => 12
[hieght] => 13
)
[2] => Array
(
[age] => 1
[time] => 2
[name] => john
[size] => 132
[hieght] => 4
)
[3] => Array
(
[age] => 1
[time] => 2
[name] => logan
[size] => 12
[hieght] => 11
)
)
Im trying to loop though every item and save each "size" in a new array. I have looked into solutions for pulling out the "size" from each inner array but cant seem to get it right:
$all_sizes = array();
foreach($myArray as $value) {
foreach($value as $key => $val) {
if($key == "size") {
}
}
}
I am new to PHP so im struggling on the proper syntax for this situation.
Remove the next foreach()
$all_sizes = array();
foreach($myArray as $value => $getSize) {
$all_sizes[] = $getSize['size'];
}
print_r($all_sizes);
Related
I've been using codeigniter for years but there's a really big gap in between so i always find myself in situations where i forgot how to do things and its almost midnight here so my brain isn't working fast. Can someone show me how to echo the array i have and explain to me how they are processed in the foreach loops?
I have this code in my model to take the rows in 2 tables.
public function tag_genre(){
$result['tag'] = $this->db->get('tags')->result_array();
$result['genre'] = $this->db->get('genre')->result_array();
return $result;
}
And I have this in my controller
public function view_publish_story(){
$data = array('tag_genre' => $this->story_model->tag_genre(), 'title' => "New Story");
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');
}
I used print_r in my view and this is the result. Seeing this just confuses me more. It's been atleast 2 years since i even dealt with foreach loops.
Array ( [tag] => Array ( [0] => Array ( [tag_id] => 1 [tag_name] =>
LitRPG ) [1] => Array ( [tag_id] => 2 [tag_name] => Virtual Reality )
[2] => Array ( [tag_id] => 3 [tag_name] => Cyberpunk ) [3] => Array (
[tag_id] => 4 [tag_name] => Reincarnation ) [4] => Array ( [tag_id] =>
5 [tag_name] => Summoned Hero ) [5] => Array ( [tag_id] => 6
[tag_name] => Martial Arts ) [6] => Array ( [tag_id] => 7 [tag_name]
=> Slice of Life ) [7] => Array ( [tag_id] => 8 [tag_name] => Overpowered ) [8] => Array ( [tag_id] => 9 [tag_name] => Non-Human )
[9] => Array ( [tag_id] => 10 [tag_name] => Anti-hero ) ) [genre] =>
Array ( [0] => Array ( [genre_id] => 1 [genre_name] => action ) [1] =>
Array ( [genre_id] => 2 [genre_name] => adventure ) [2] => Array (
[genre_id] => 3 [genre_name] => comedy ) [3] => Array ( [genre_id] =>
4 [genre_name] => Drama ) [4] => Array ( [genre_id] => 5 [genre_name]
=> Fantasy ) [5] => Array ( [genre_id] => 6 [genre_name] => Historical ) [6] => Array ( [genre_id] => 7 [genre_name] => Horror ) [7] => Array
( [genre_id] => 8 [genre_name] => Psychological ) [8] => Array (
[genre_id] => 9 [genre_name] => Romance ) [9] => Array ( [genre_id] =>
10 [genre_name] => Sci-fi ) [10] => Array ( [genre_id] => 11
[genre_name] => Mystery ) [11] => Array ( [genre_id] => 12
[genre_name] => Tragedy ) [12] => Array ( [genre_id] => 13
[genre_name] => Short Story ) [13] => Array ( [genre_id] => 14
[genre_name] => Satire ) ) )
I've been looking at various questions regarding arrays from assoc to multidimensional and other stuff. Then i finally visited php manual for foreach loop and i finally was able to echo the array. The problem now is that although it echoes my array it also has an error for each, i can probably fix the error part by declaring it as a defined variable. My question is, is there any other better way? or any improvement on how i made my array to make it cleaner or easier to print?
foreach($tag_genre as $key => $value){
foreach($value as $values){
echo $values['tag_name'];
}
}
UPDATE: i changed the way i made the array.
This is now my model:
public function get_tags(){
$query = $this->db->get('tags')->result_array();
foreach($query as $row){
$result[$row['tag_id']] = $row['tag_name'];}
return $result;
}
public function get_genre(){
$query = $this->db->get('genre')->result_array();
foreach($query as $row){
$result[$row['genre_id']] = $row['genre_name'];}
return $result;
}
and my controller:
public function view_publish_story(){
$data = array('tag' => $this->story_model->get_tags(), 'genre' => $this->story_model->get_genre(), 'title' => "New Story");
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');
}
and in my view:
<tr>
<div class="form-group">
<td><label for="genre">Genre</label></td>
<?php
foreach($genre as $genre_id => $genre_name){
echo "<td><label class='checkbox-inline'><input type='checkbox' value=''>".$genre_name."</label><td>";
}
?>
</div>
</tr>
My problem now is that, how do i fit all these into my table? I just ruined my table right now since i echoed them all in a single line. How do i do it so that it is printed in 3 columns?
foreach($tag_genre as $key => $value){
foreach($value as $values){
echo '<pre>';
echo $values['tag_name'];
}
}
this will format your array in a way you can read it and understand it.
This question already has answers here:
Return single column from a multi-dimensional array [duplicate]
(7 answers)
Closed 5 years ago.
I got the next array:
Array (
[0] => Array ( [email] => xasxxxxxx#yahoo.com [btc] => 0.00287896 [tn] => 6.615 [address] => 2BY4HM [status] => pending )
[1] => Array ( [email] => xxxxx#yahoo.com [btc] => 0.04000 [tokens_given] => 5 [address] => xxXXxxxxxaaaxxXs [status] => pending )
[2] => Array ( [email] => xsxxxx#yahoo.com [btc] => 0.04000 [tokens_given] => 5 [address] => xxXXxxxxxaaaxxXs [status] => pending )
[3] => Array ( [email] => xssasas5#yahoo.com [btc] => 0.04000 [tokens_given] => 5 [address] => xxXXxxxxxaaaxxXs [status] => pending )
[4] => Array ( [email] => xxxxxx#yahoo.com [btc] => 0.04000 [tokens_given] => 5 [address] => xxXXxxxxxaaaxxXs [status] => pending )
)
How am I supposed to get all 'address'es in a variable as a string separated by comma?
something like this
$string = "1stadress, 2ndaddress, 3ndadress' etc
I tried:
$comma_separated = implode(",", $row['address']); // where $ row is the array above
and it failed.
You can use array_column and implode
echo implode(',',array_column($myarray,'address'));
Loop though array and collect the relevant members, I'd perefer using untermidiate array this way it can be reused if needed later
$buffer = [];
foreach($myArray as $id=>$data){
$buffer[] = $data['address'];
}
$output = implode(',',$buffer);
This question already has answers here:
How to count array with group by
(2 answers)
Closed 4 months ago.
I am creating a notification system. That results shown below returns all notifications for a particular user. I want to group the array by 'notify_id' and also count each item in a group.
Such that I'll have :
"Jack Bill is now following you", John Doe commented on your post 2",
"John Doe and 2 others commented on your post 1"
Array ( [0] =>
Array ( [id] => 1
[user_id] => 10
[type] => follow
[notify_id] => 13
[notify_date] => 1483444712
[firstname] => Jack
[surname] => Bill
[picture] => )
[1] =>
Array ( [id] => 10
[user_id] => 10
[type] => comment
[notify_id] => 2
[notify_date] => 1482159309
[firstname] => John
[surname] => Doe
[picture] => )
[2] =>
Array ( [id] => 8
[user_id] => 10
[type] => comment
[notify_id] => 1
[notify_date] => 1482159219
[firstname] => John
[surname] => Doe
[picture] => )
[3] =>
Array ( [id] => 6
[user_id] => 16
[type] => comment
[notify_id] => 1
[notify_date] => 1482159129
[firstname] => James
[surname] => Canon
[picture] => )
[4] =>
Array ( [id] => 5
[user_id] => 14
[type] => comment
[notify_id] => 1
[notify_date] => 1482159079
[firstname] => Sharon
[surname] => Abba
[picture] => ) )
You have two questions, I will try to answer both of them:
I want to group the array by 'notify_id'...
Assuming your array name is $a:
$return = array();
foreach($a as $val) {
if (!isset($return[$val['notify_id']])) {
$return[$val['notify_id']] = array();
}
$return[$val['notify_id']][] = $val;
}
print_r($return); // <-- Your array is grouped by notify_id
... and also count each item in a group.
Now you have your grouped by notify_id in $return so:
foreach($return as $k => $v) {
echo count($v) . ' values are present for notify #' . $k;
// It will display something like: 10 values are present for notify #1
}
Hope it will help, good luck!
This question already has answers here:
How do I retrieve results as multidimensional array from mySQL and PHP?
(4 answers)
Closed 9 years ago.
$this->load->library('opencloud');
$opencloud = new Opencloud;
$containers = $this->opencloud->list_containers();
print_r($containers);
The above code outputs the following array:
Array ( [0] => Array ( [name] => .CDN_ACCESS_LOGS [count] => 2 [bytes]
=> 606 ) [1] => Array ( [name] => Michael Grigsby [count] => 9 [bytes] => 891976 ) [2] => Array ( [name] => Random Photos [count] => 0 [bytes] => 0 ) [3] => Array ( [name] => hello [count] => 10 [bytes] =>
1129257 ) [4] => Array ( [name] => hello_world [count] => 1 [bytes] =>
659737 ) [5] => Array ( [name] => hi [count] => 0 [bytes] => 0 ) )
When I echo out $containers[1]['name'] I get: Michael Grigsby. My question is how do I get the script to output all of the name values rather than simply one?
You can echo the name of each array in this classic mode in PHP with a foreach loop like this:
foreach($containers as $container){
echo($container['name']);
}
<?php
foreach($mainarray as $onearray){
echo $onearray['name'];
}
?>
You can loop around the array with for loop in the following way:
for ($i = 0; $i < count($containers); $i++) {
echo $containers[$i]['name'];
}
Alternatively, you can use a foreach loop to directly get the content without any manipulation of array indexes.
This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 8 years ago.
I need to iterate through a dynamic array. The array will look something like the following:
Array
(
[2010091907] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
[away] => Array
(
[score] => Array
(
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[T] => 7
)
[abbr] => ARZ
[to] => 2
)
[weather] =>
[media] => Array
(
[tv] => FOX
[sat] => 709
[sathd] => 709
[radio] => Array
(
[home] => 153
[away] => 90
)
)
[bp] => 13
[yl] =>
[qtr] => Final
[down] => 0
[togo] => 0
[clock] => 00:26
[posteam] => ARZ
[note] =>
[redzone] =>
[stadium] => Georgia Dome
)
I need it to be dynamic and for testing purposes, I need to be able to call it via:
echo "Key: $key; Value: $value<br />\n";
I'm going to later take this information and place it into a mysql database, but for now, I need to brush up on arrays and figure out how to format the data.
Any help is appreciated.
I´d go for a recursive function: If a value is an array, call it again and otherwise display the key / value pair.
Something like (not tested):
function display_array($your_array)
{
foreach ($your_array as $key => $value)
{
if is_array($value)
{
display_array($value);
}
else
{
echo "Key: $key; Value: $value<br />\n";
}
}
}
display_array($some_array);