Get values from Multilevel Array in PHP [duplicate] - php

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'];

Related

Comma separated value from array [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
PHP - Multidimensional array to CSV
(3 answers)
How to write a multidimensional array to a csv file. PHP
(2 answers)
multidimensional irregular array to CSV in PHP
(1 answer)
Closed 3 years ago.
I need to explode stdentid array in comma separated value.currently the studentid array is in multidimensional array i need all the value should be in comma separated
The result should be for studentid array is
[studentid] => 36399,96500,96503,96509,96512 and so on..
Array
(
[started] => 1
[studentid] => Array
(
[1] => Array
(
[0] => 36399
)
[2] => Array
(
[0] => 96500
[1] => 96503
[2] => 96506
[3] => 96509
[4] => 96512
[5] => 96515
)
[3] => Array
(
[0] => 96501
[1] => 96504
[2] => 96507
[3] => 96510
[4] => 96513
)
[4] => Array
(
[0] => 96502
[1] => 96505
[2] => 96508
[3] => 96511
[4] => 96514
)
)
[name] => Test
[name_or_email] =>
[submitbtn] => 1
)
assuming your variable is $studentsResult (which contains the whole array you have posted)
in that case:
foreach($studentsResult['studentid'] as $studentId => $studentValues){
$tempStudents[$studentId] = join(',',$studentValues);
}
and then $tempStudents is what you're probably looking for\
If, however all those IDs needs to be under that ONE student, then you haven't said where is the ID, but you can follow the answer on flattening the array in one of the comments or do:
$tempStudents = [];
foreach($studentsResult['studentid'] as $someId => $studentValues){
$tempStudents = array_merge($tempStudents,$studentValues);
}
echo join(',',$tempStudents);

How do I to find values in a nested JSON array using PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I'm trying to find a value in a nested JSON using PHP. I've done a print_r() of it and get:
Array
(
[types] => Array
(
[0] => Array
(
[name] => Running
[arabicName] => الجاري
[categories] => Array
(
[0] => Array
(
[name] => Entertainment
[arabicName] => تسلية
[programs] => Array
(
[0] => Array
(
[id] => 375
[name] => Saalo Marteh
[arabicName] =>
[image] => http://plus.mtv.com.lb/Chrome/KPanel/Pictures/Programs/151120100928327.jpg
)
[1] => Array
(
[id] => 491
[name] => Celebrity Duets
[arabicName] =>
[image] => http://plus.mtv.com.lb/Chrome/KPanel/Pictures/Programs/151108084429774.jpg
)
I'm trying to get the value for name and id under the entertainment header.
I've tried the following but no luck:
echo $json['types']['categories'][0]['name'];
foreach ($json['types'][0]['categories'][0]['programs'] as $value) {
echo $value['id'] . ' ' . $value['name'];
}

Outputting values from a multi-dimensional array [duplicate]

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.

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

sort array by key value and display the key [duplicate]

This question already has answers here:
Sorting multidim array: prioritize if column contains substring, then order by a second column
(4 answers)
Sort multidimensional array by multiple columns
(8 answers)
Closed 9 years ago.
I have this Array
Array (
[0] => Array
(
[id] => 61
[testo] => articolo di maggio
[data] => 2013-05-03
[orario] => 00:00:00
[nome_files] => fabmad_1920x1200.jpg
[pubblicato] => 1
)
[1] => Array
(
[id] => 58
[testo] =>
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => 20130603100647_da_installare.rtf
[pubblicato] => 1
)
[2] => Array
(
[id] => 59
[testo] => Demo
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => eye_drops_water_2.jpg
[pubblicato] => 1
)
)
I want to sort it by "data".
I want to display the "data" and for each data the elements...
Try the ksort function or the array-multisort
Check the php documentation: http://php.net/manual/en/function.ksort.php
http://php.net/manual/en/function.array-multisort.php

Categories