Outputting values from a multi-dimensional array [duplicate] - php

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.

Related

Get values from Multilevel Array in PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 8 days ago.
I have an array that I have run json_decode on it...
How can I echo the category_name Hoops from $myarray?
The following is the output of
echo"<pre>;
print_r($myarray);
echo"</pre>;
Array
(
[Althetic] => Array
(
[0] => Array
(
[category_id] => 1
[category_name] => Balls
[parent_id] => 0
)
[1] => Array
(
[category_id] => 2
[category_name] => Hoops
[parent_id] => 0
)
[2] => Array
(
[category_id] => 3
[category_name] => Strings
[parent_id] => 0
)
)
[rawRequest] => Hello world
[hasError] => 1
[errorMessage] => OK
)
I tried
echo $myarray[Althetic][1]->[category_name];
Without any results :(
T
This is a multidimensional array, -> is used to access elements of object.
$myarray['Althetic'][1]['category_name'];

Looping thought multilevel array php [duplicate]

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);

php | Multidimensional array sorting [duplicate]

This question already has answers here:
Sorting a multidimensional array in PHP? [duplicate]
(3 answers)
How do I Sort a Multidimensional Array in PHP [duplicate]
(10 answers)
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 9 years ago.
I have an array and need to be sorted (based on id):
Array
(
[0] => Array
(
[qty] => 1
[id] => 3
[name] => Name1
[sku] => Model 1
[options] =>
[price] => 100.00
)
[1] => Array
(
[qty] => 2
[id] => 1
[name] => Name2
[sku] => Model 1
[options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>.
[price] => 209.00
)
)
Is it possible to sort my array to get output (id based)?
Array
(
[0] => Array
(
[qty] => 2
[id] => 1
[name] => Name2
[sku] => Model 1
[options] => Color: <em>Black (+10$)</em>. Memory: <em>32GB (+99$)</em>.
[price] => 209.00
)
[1] => Array
(
[qty] => 1
[id] => 3
[name] => Name1
[sku] => Model 1
[options] =>
[price] => 100.00
)
)
Thanks!
Try like
$id_arr = array();
foreach ($my_arr as $key => $value)
{
$id_arr[$key] = $value['id'];
}
array_multisort($id_arr, SORT_DESC, $my_arr);
You can also place SORT_ASC for assending order.Better you add ORDER BY id to the query through which you are getting this array of results
function cmp($a, $b) {
return $a["id"] - $b["id"];
}
usort($arr, "cmp");//$arr is the array to sort

php echo nested object [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I want to 'echo' a little bit more nested object.
I saw several posts to this question - but this is freaking me out.
I got an Array/Object named 'arrResult' and the print_r(arrResult) output is:
Array
(
[status] => stdClass Object
(
[code] => 0
[message] => Success
)
[result] => Array
(
[0] => stdClass Object
(
[base] => stdClass Object
(
[id] => 3
[created] => 2012-11-11 12:11:07
[start] => 2012-11-11
)
[pos] => Array
(
[0] => stdClass Object
(
[id] => 4
[invoices_id] => 3
[article_id] => 1
[quantity] => 1
[unit] => Monate
[pos_txt] => Paketname
)
)
[summ] => stdClass Object
(
[net] => 2.52
[discount] => 0
[tax] => 0.47899159663865
[gross] => 3
[rounded_net] => 2.52
)
)
[1] => stdClass Object
(
[base] => stdClass Object
(
[id] => 2
[created] => 2012-11-11 12:10:39
[start] => 2012-11-11
)
[pos] => Array
(
[0] => stdClass Object
(
[id] => 3
[invoices_id] => 2
[article_id] => 2
[quantity] => 1
[unit] => Monate
[pos_txt] => Paketname2
)
)
[summ] => stdClass Object
(
[net] => 5.04
[discount] => 0
[tax] => 0.95798319327731
[gross] => 6
[rounded_net] => 5.04
)
)
)
)
I want to echo all [pos].
Something like 'echo arrResult[result][0][pos][0]->pos_txt', 'echo arrResult[result][0][pos][1]->pos_txt',...
I thought about this:
foreach ((array)$arrResult['result'] as $key => $contract) {
foreach ($contract as $key => $objPos){
echo $objPos->pos_txt;
}
}
My brain doesn't get it.
Can someone help me with this?
You can use
echo "<pre>";
foreach(array_map(function($v){ return $v->pos ;}, $data['result']) as $list)
{
foreach($list as $objPos)
echo $objPos->pos_txt,PHP_EOL;
}
Output
Paketname
Paketname2
See Live Demo
Use xdebug extension, it's something what you must have for development anyway, it slightly changes how var_dump works and I believe it will solve your problem
see xdebug documentation
$num = count($arrResult['result']);
for ($i=0; $i <= $num; $i++) {
echo $arrResult['result'][$i]->pos[0]->pos_txt;
}

Iterate through array and get key and value [duplicate]

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);

Categories