I am trying to add elements in Array of Array as
$categoryTreeStructure[$grantCategoryName][$parentCategoryName][$subCategoryText]['text'] =$subCategoryText;
// GET CHILD CATEGORY URL
$categoryTreeStructure[$grantCategoryName][$parentCategoryName][$subCategoryText]['url'] = $childPageLink;
I tried to loop through each category as
foreach($categoryTreeStructure as $categoryName => $subCategories ){
echo $categoryName."<br>";
}
I am getting blank in $categoryName.
I need to loop through each category how can I do this?
foreach($categoryTreeStructure as $categoryName => $subCategories ){
foreach($subCategories as $key => $val){
foreach($val as $subkey => $subval){
echo $subval['text'] . "<br>";
echo $subval['url'];
}
}
}
The only way I can see it working is if you use nested loops
Your problem is the second dimension of your array isn't target. You array look like so :
Array
(
[] => Array
(
[] => Array
(
[foo] => Array
(
[text] => foo
[url] => Bar
)
)
)
)
If you want to accept you data you will nice a foreach like this :
foreach($categoryTreeStructure[$parentCategoryName] as $categoryName ){
print_r($categoryName);
}
This will output :
Array
(
[foo] => Array
(
[text] => foo
[url] => Bar
)
)
But in your exemple you wanted the direct text so :
foreach($categoryTreeStructure[$grantCategoryName][$parentCategoryName][$subCategoryText] as $categoryName ){
echo $categoryName;
}
This will output :
foo
Bar
This is the answer for your exemple as your exemple only seen to loop in the last array but if you have multiple subCategoryText as expected you will need to have nested loop like #Rossco pointed out :
foreach($categoryTreeStructure as $categoryName => $subCategories ){
foreach($subCategories as $key => $val){
foreach($val as $subkey => $subval){
echo $subval['text'] . "<br>";
echo $subval['url'];
}
}
}
Related
I don't know how to say it in the title, but you'll understand with an example.
I've a database, where I store data in column named propriety and another column value. When I do my SQL request, I get this:
Array
(
[0] => Array
(
[propriety] => propriety1
[value] => value1
)
[1] => Array
(
[propriety] => propriety2
[value] => value2
)
[2] => Array
(
[propriety] => propriety3
[value] => value3
)
I would like to display it like this one:
Propriety : Value
But when I do my 2 foreach:
foreach ($array as $key => $value) {
foreach ($value as $k => $v) {
echo $k . ' : ' . $v . '<br>';
}
}
It returns:
Propriety : Propriety1
Value : Value1
etc ..
You don't need two foreach loops to do this, it can be done with one:
foreach ($array as $row) {
echo $row['propriety'].' : '.$row['value'].'<br>';
}
Assuming you actually want the values returned by the database.
Here's an alternative extracting value and re-indexing by propriety:
foreach(array_column($array, 'value', 'propriety') as $propriety => $value) {
echo "$propriety : $value<br>";
}
This assumes that each propriety will be unique.
I have a multidimensional array in variable $comments, containing:
Array (
[0] => Array
(
[0] => 889
[1] => First comment
[2] => 8128912812
[3] => appr
)
[1] => Array
(
[0] => 201
[1] => This is the second comment
[2] => 333333
[3] => appr
)
// There is more...
)
How do I loop through this array and echo each value using for each?
foreach($arrayOfArrays as $array){
foreach($array as $index => $value){
echo $array[$index];
}
}
You should use two foreach loops as your array has to levels :
foreach ($comments as $comment)
foreach ($comment as $comment_data)
echo $comment_data;
If your array structre stay like the one you show, you can do this like follow :
foreach($comments as $comment) {
echo $comment[0];
echo $comment[1];
echo $comment[2];
echo $comment[3];
}
Just use two foreach loops. One inside the other
foreach($comments as $commentArray){
foreach($commentArray as $comment){
echo $comment;
}
}
Hope this helps you
$i=0;
$c=count($array);
while ($i<$c) {
foreach ($array[$i] as $comment_property) {
echo $comment_property;
}
$i++;
}
I have the following array stored in the wordpress options table and I need to get the value of each title
a:1:{s:14:"swd_line_items";a:3:{i:0;a:1:{s:5:"title";s:9:"asdfasdfa";}i:1;a:1:{s:5:"title";s:13:"asdf asdf ada";}i:2;a:1:{s:5:"title";s:29:"fffffffffffffffffffffffffffff";}}}
I've tried nested foreach loops but nothing I do seems to work. There must be a simple solution?
function swd_get_line_items() {
$line_items = get_option('line_items_array');
$items = array();
foreach( $line_items as $item => $value ) {
foreach ($value as $new => $v) {
$items[] = array(
$new => $v
);
}
}
return $line_items;
}
Hope this helps :)
$array = unserialize('a:1:{s:14:"swd_line_items";a:3:{i:0;a:1:{s:5:"title";s:9:"asdfasdfa";}i:1;a:1:{s:5:"title";s:13:"asdf asdf ada";}i:2;a:1:{s:5:"title";s:29:"fffffffffffffffffffffffffffff";}}}');
foreach($array['swd_line_items'] as $item) {
echo $item['title'];
}
get_option() perfectly unsezrializes an array so there is no need to do it as the two other answers suggested it.
Then what you have is a two dimensional array but you are perfectly browsing it with two nested foreach.
By the way here is the final output of your code:
As you can see you perfectly extracted the titles:
Array
(
[0] => Array
(
[0] => Array
(
[title] => asdfasdfa
)
)
[1] => Array
(
[1] => Array
(
[title] => asdf asdf ada
)
)
[2] => Array
(
[2] => Array
(
[title] => fffffffffffffffffffffffffffff
)
)
)
But the issue here is that you do not return this array but you return this one:
return $line_items;
Change it into
return $items;
you mean this ?
function swd_get_line_items($serialized_array)
{
$line_items = unserialize($serialized_array);
$items = array();
foreach ($line_items['swd_line_items'] as $key => $item)
{
$items[$key] = $item['title'];
}
return $items;
}
I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
Example: key['user_id'] this will print all user_id from array. is it possible? please help me thanks
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[user_id] => 1
)
[1] => Array
(
[user_id] => 2
)
)
[Post3] => Array
(
[0] => Array
(
[user_name] => 1
)
)
)
Here is my PHP code:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
You can print_r to achive the same results you want with your triple for each.
I'm trying to print array. All code working fine with foreach loop. But I'm trying to print with associated keys. Is it possible?
You can easily use recursion for such a problem. You can use something along the lines of:
function printValuesByKey($array, $key) {
if (!is_array($array)) return;
if (isset($array[$key]))
echo $key .': '. $array[$key] .'<br>';
else
foreach ($array as $v)
printValuesByKey($v, $key);
}
In your example:
printValuesByKey($array, 'user_id');
will print:
user_id: 1
user_id: 2
I'm trying to print array. All code working fine.But at last I'm getting `ArrayArray'. Can any one solve this problem. many many thanks
here is my array
Array
(
[Post1] => Array
(
[id] => 1
[title] => hi
)
[Post2] => Array
(
[0] => Array
(
[id] => 1
)
)
[Post3] => Array
(
[0] => Array
(
[id] => 1
)
)
)
Here is my PHP Code
foreach($post as $key => $value) {
foreach($value as $print => $key) {
echo "<br>".$key;
}
}
here is output
ID
Array
Array
Try this:
foreach($post as $key => $value) {
foreach($value as $print => $key) {
if (is_array($key)){
foreach($key as $print2 => $key2) {
echo "<br>".$key2;
}
}else{
echo "<br>".$key;
}
}
}
The to string method of an array is to return "Array".
It sounds like you want to view the array for debugging purposes. var_dump() is your friend :)
you are trying to print an array, resulting in Array.
If you want to print an array use print_r
I think the trouble for you is that you have $key in the outer loop and $key in the inner loop so its really confusing which $key you are talking about for starters.
You just want the stuff printed out to debug?
echo "<pre>" . print_r( $post , true ) . "</pre>\n";