Am trying to loop through an array,with print_r I get the array below but am getting
"Notice: Undefined index: name"
error, when I ran my loop, dunno what am doing wrong?
Kindly help
Array
(
[0] => Array
(
[0] => Array
(
[ID] => 1
[name] => Paul
[email] => 32
)
[1] => Array
(
[ID] => 2
[name] => Allen
[email] => 25
)
[2] => Array
(
[ID] => 3
[name] => Teddy
[email] => 23
)
[3] => Array
(
[ID] => 4
[name] => 4
[email] => Mark
)
[4] => Array
(
[ID] => 5
[name] => Paul
[email] => 32
)
[5] => Array
(
[ID] => 6
[name] => Allen
[email] => 25
)
[6] => Array
(
[ID] => 7
[name] => Teddy
[email] => 23
)
[7] => Array
(
[ID] => 8
[name] => 4
[email] => Mark
)
[8] => Array
(
[ID] => 9
[name] => Paul
[email] => 32
)
[9] => Array
(
[ID] => 10
[name] => Allen
[email] => 25
)
[10] => Array
(
[ID] => 11
[name] => Teddy
[email] => 23
)
[11] => Array
(
[ID] => 12
[name] => 4
[email] => Mark
)
[12] => Array
(
[ID] => 13
[name] => Paul
[email] => 32
)
[13] => Array
(
[ID] => 14
[name] => Allen
[email] => 25
)
[14] => Array
(
[ID] => 15
[name] => Teddy
[email] => 23
)
[15] => Array
(
[ID] => 16
[name] => 4
[email] => Mark
)
[16] => Array
(
[ID] => 17
[name] => Paul
[email] => ibraq22#yahooo.com
)
[17] => Array
(
[ID] => 18
[name] => Opeyemi Adam
[email] => resfvr#yahoo.com
)
[18] => Array
(
[ID] => 19
[name] => Opeyemi Adam
[email] => info#ckdigital.net
)
[19] => Array
(
[ID] => 20
[name] => Paul
[email] => ibraq22#yahooo.com
)
)
)
My PHP Loop is below
foreach ($retr as $page) {
echo $page['name'];
//var_dump($page);
}
Regards
Your array is multidimentional, being a single element array containing a further 19 child elements.
If you just want to itterate the child elements, then run the loop over the top level elelemt, not the parent array:
foreach ($retr[0] as $page) {...}
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have data in json something like that:
stdClass Object
(
[contacts] => stdClass Object
(
[14] => stdClass Object
(
[data] => stdClass Object
(
[email] => veer#gmail.com
[first_name] => veer
[last_name] =>
[user_id] => 16
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 14
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => veer
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 92
[1] => 13
[2] => 12
[3] => 9
)
[files] => Array
(
)
)
[9] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.pratibha1432#gmail.com
[first_name] => Pratibha
[last_name] => Singh
[user_id] => 8
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 9
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => pratibha
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 93
[2] => 92
[3] => 91
[4] => 82
[5] => 15
[6] => 14
[7] => 13
[8] => 9
)
[files] => Array
(
)
)
[4] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.dev1432#gmail.com
[first_name] => Devesh
[last_name] => Singh
[user_id] => 7
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 4
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => devesh
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 93
[1] => 92
[2] => 15
[3] => 12
[4] => 11
[5] => 9
)
[files] => Array
(
)
)
[2] => stdClass Object
(
[data] => stdClass Object
(
[email] => admin#gmail.com
[first_name] => veronica
[last_name] =>
[user_id] => 1
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 2
[gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => admin
[profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 92
[2] => 15
[3] => 14
[4] => 13
[5] => 9
)
[files] => Array
(
)
)
)
[status] => success
)
and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails = array('email'=>$value->email);
return $emails;
}
}
but it return only single data:
Array
(
[email] => veer#gmail.com
)
i want to fetch all the email like that:
Array
(
[0] => Array
(
[emails] => veer#gmail.com
)
[1] => Array
(
[emails] => singh.pratibha1432#gmail.com
)
[2] => Array
(
[emails] => singh.dev1432#gmail.com
)
[3] => Array
(
[emails] => admin#gmail.com
)
)
how can I achieve this?
Define null array and in foreach run store every email in that variable.
$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails[] = array('emails'=>$contact->data->email;);
}
}
return $emails;
I have the follow array structure:
Array
(
[0] => Array
(
[id] => 10
[parent] => 0
[username] => PDV
[children] => Array
(
[0] => Array
(
[id] => 100
[parent] => 10
[username] => Terminal de Vendas
)
[1] => Array
(
[id] => 101
[parent] => 10
[username] => Clientes
)
[2] => Array
(
[id] => 102
[parent] => 10
[username] => Fechamento
)
[3] => Array
(
[id] => 103
[parent] => 10
[username] => Relatórios de Venda
[children] => Array
(
[0] => Array
(
[id] => 1004
[parent] => 103
[username] => Relatório de Venda 1
)
[1] => Array
(
[id] => 1005
[parent] => 103
[username] => Relatório de Venda 2
)
)
)
)
)
[1] => Array
(
[id] => 11
[parent] => 0
[username] => Estoque
[children] => Array
(
[0] => Array
(
[id] => 111
[parent] => 11
[username] => Cadastro de produtos
)
[1] => Array
(
[id] => 112
[parent] => 11
[username] => Entrada Produtos
)
[2] => Array
(
[id] => 113
[parent] => 11
[username] => Relatórios de Estoque
[children] => Array
(
[0] => Array
(
[id] => 1104
[parent] => 113
[username] => Relatório de Estoque A
)
[1] => Array
(
[id] => 1105
[parent] => 113
[username] => Relatório de Estoque B
)
)
)
)
)
[2] => Array
(
[id] => 13
[parent] => 0
[username] => Financeiro
[children] => Array
(
[0] => Array
(
[id] => 126
[parent] => 13
[username] => Notas de Entrada
)
[1] => Array
(
[id] => 127
[parent] => 13
[username] => Notas de Saída
)
[2] => Array
(
[id] => 128
[parent] => 13
[username] => Vendas Diárias
)
[3] => Array
(
[id] => 129
[parent] => 13
[username] => Balanço
)
[4] => Array
(
[id] => 130
[parent] => 13
[username] => Relatórios
[children] => Array
(
[0] => Array
(
[id] => 1301
[parent] => 130
[username] => Relatório 1
)
)
)
)
)
[3] => Array
(
[id] => 12
[parent] => 0
[username] => Sistema
[children] => Array
(
[0] => Array
(
[id] => 107
[parent] => 12
[username] => Ajuda...
[children] => Array
(
[0] => Array
(
[id] => 1008
[parent] => 107
[username] => Conteúdo...
)
[1] => Array
(
[id] => 1009
[parent] => 107
[username] => Licença
)
[2] => Array
(
[id] => 1100
[parent] => 107
[username] => Sobre...
)
)
)
[1] => Array
(
[id] => 118
[parent] => 12
[username] => Usuários
[children] => Array
(
[0] => Array
(
[id] => 1200
[parent] => 118
[username] => Cadastro
)
[1] => Array
(
[id] => 1303
[parent] => 118
[username] => Grupos
)
[2] => Array
(
[id] => 1201
[parent] => 118
[username] => Permissões
)
[3] => Array
(
[id] => 1202
[parent] => 118
[username] => Minha Conta
[children] => Array
(
[0] => Array
(
[id] => 13002
[parent] => 1202
[username] => Dados Pessoais
[children] => Array
(
[0] => Array
(
[id] => 130002
[parent] => 12002
[username] => TESTE 1
)
[1] => Array
(
[id] => 130003
[parent] => 12002
[username] => TESTE 2
)
)
)
[1] => Array
(
[id] => 12003
[parent] => 1202
[username] => Alterar Senha
[children] => Array
(
[0] => Array
(
[id] => 130004
[parent] => 12003
[username] => TESTE 3
)
)
)
[2] => Array
(
[id] => 1204
[parent] => 1202
[username] => Sair
)
)
)
)
)
[2] => Array
(
[id] => 125
[parent] => 12
[username] => Preferências
)
[3] => Array
(
[id] => 119
[parent] => 12
[username] => Configuração
)
)
)
)
I've been working for hours and my head is cracking. Please, can anyone give me a light on how to limit the depth of children to return?
I want get a part of this structure. For example: With base on ID 118, return no parents and 1 level of childrens from this ID.
Them call a function:
$result = TreePartByID($source, 118, null, 1);
$source Is the full array structure;
118 Is the ID to use as item base;
null Is the number of parents to return from the ID 118. In this case, null represents the some iten with ID 118;
1 Is a depth of childrens to return from ID 118. This case is only immediate childrens relative to ID 118;
$result:
Array
(
[0] => Array
(
[id] => 118
[parent] => 12
[username] => Usuários
[children] => Array
(
[0] => Array
(
[id] => 1200
[parent] => 118
[username] => Cadastro
)
[1] => Array
(
[id] => 1303
[parent] => 118
[username] => Grupos
)
[2] => Array
(
[id] => 1201
[parent] => 118
[username] => Permissões
)
[3] => Array
(
[id] => 1202
[parent] => 118
[username] => Minha Conta
[children] => true
)
)
)
)
The function:
parameters:
$ID = ID of iten defined as default;
$parents = Number of parents from the $ID. If null, retur the own $ID, else if 0(zero) return from the root and any number is the number of parents;
$childrens = Number of childrens from the $ID. if 0(zero) no return childrens;
function TreePartByID($ID, $parents=0, $childrens=1){
...
return $arrayResult;
}
TreePartByID(118) Result: o path completo referente ao item 118
Array
(
[0] => Array
(
[id] => 12
[parent] => 0
[username] => Sistema
[children] => Array
(
[0] => Array
(
[id] => 118
[parent] => 12
[username] => Usuários
[children] => Array
(
[0] => Array
(
[id] => 1200
[parent] => 118
[username] => Cadastro
)
[1] => Array
(
[id] => 1303
[parent] => 118
[username] => Grupos
)
[2] => Array
(
[id] => 1201
[parent] => 118
[username] => Permissões
)
[3] => Array
(
[id] => 1202
[parent] => 118
[username] => Minha Conta
[children] => Array
(
[0] => Array
(
[id] => 13002
[parent] => 1202
[username] => Dados Pessoais
[children] => Array
(
[0] => Array
(
[id] => 130002
[parent] => 12002
[username] => TESTE 1
)
[1] => Array
(
[id] => 130003
[parent] => 12002
[username] => TESTE 2
)
)
)
[1] => Array
(
[id] => 12003
[parent] => 1202
[username] => Alterar Senha
[children] => Array
(
[0] => Array
(
[id] => 130004
[parent] => 12003
[username] => TESTE
)
)
)
[2] => Array
(
[id] => 1204
[parent] => 1202
[username] => Sair
)
)
)
)
)
)
)
)
TreePartByID(1202, 1, 1); // retorna 1 item como parent(118), e filhos imediatos do ID 1202 (1 nível);
Array
(
[0] => Array
(
[id] => 118
[parent] => 12
[username] => Usuários
[children] => Array
(
[0] => Array
(
[id] => 1202
[parent] => 118
[username] => Minha Conta
[children] => Array
(
[0] => Array
(
[id] => 13002
[parent] => 1202
[username] => Dados Pessoais
[children] => true
)
[1] => Array
(
[id] => 12003
[parent] => 1202
[username] => Alterar Senha
[children] => true
)
[2] => Array
(
[id] => 1204
[parent] => 1202
[username] => Sair
)
)
)
)
)
)
I have an input category array with childs as follows
I want to convert it using a recursive function to another array like
OUTPUT NEEDED
['1'=>'fashion', '10' => 'fashion > women','23'=> 'fashion > women > clothing','29'=> 'fashion > women > clothing > dresses' ... and so on ]
My purpose is to use output array to populate select box options to adda category.
INPUT ARRAY
Array
(
[1] => Array
(
[id] => 1
[name] => fashion
[parent_id] => 0
[childs] => Array
(
[10] => Array
(
[id] => 10
[name] => women
[parent_id] => 1
[childs] => Array
(
[23] => Array
(
[id] => 23
[name] => clothing
[parent_id] => 10
[childs] => Array
(
[29] => Array
(
[id] => 29
[name] => dresses
[parent_id] => 23
[childs] => Array
(
)
)
[30] => Array
(
[id] => 30
[name] => jumpsuits
[parent_id] => 23
[childs] => Array
(
)
)
)
)
[24] => Array
(
[id] => 24
[name] => bags & accessories
[parent_id] => 10
[childs] => Array
(
)
)
[25] => Array
(
[id] => 25
[name] => shoes
[parent_id] => 10
[childs] => Array
(
)
)
[26] => Array
(
[id] => 26
[name] => watches
[parent_id] => 10
[childs] => Array
(
)
)
[27] => Array
(
[id] => 27
[name] => jewelery
[parent_id] => 10
[childs] => Array
(
)
)
[28] => Array
(
[id] => 28
[name] => eye-wear
[parent_id] => 10
[childs] => Array
(
)
)
)
)
[11] => Array
(
[id] => 11
[name] => men
[parent_id] => 1
[childs] => Array
(
)
)
[12] => Array
(
[id] => 12
[name] => kids
[parent_id] => 1
[childs] => Array
(
)
)
[13] => Array
(
[id] => 13
[name] => sports
[parent_id] => 1
[childs] => Array
(
)
)
[14] => Array
(
[id] => 14
[name] => bags
[parent_id] => 1
[childs] => Array
(
)
)
[15] => Array
(
[id] => 15
[name] => eyewear
[parent_id] => 1
[childs] => Array
(
)
)
[16] => Array
(
[id] => 16
[name] => watches & jewelery
[parent_id] => 1
[childs] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[name] => supermarket
[parent_id] => 0
[childs] => Array
(
[17] => Array
(
[id] => 17
[name] => food & beverages
[parent_id] => 2
[childs] => Array
(
[31] => Array
(
[id] => 31
[name] => breakfast
[parent_id] => 17
[childs] => Array
(
)
)
[32] => Array
(
[id] => 32
[name] => snacks
[parent_id] => 17
[childs] => Array
(
)
)
)
)
[18] => Array
(
[id] => 18
[name] => dairy products
[parent_id] => 2
[childs] => Array
(
)
)
[19] => Array
(
[id] => 19
[name] => beauty
[parent_id] => 2
[childs] => Array
(
)
)
[20] => Array
(
[id] => 20
[name] => homecare
[parent_id] => 2
[childs] => Array
(
)
)
[21] => Array
(
[id] => 21
[name] => baby world
[parent_id] => 2
[childs] => Array
(
)
)
[22] => Array
(
[id] => 22
[name] => pet world
[parent_id] => 2
[childs] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[name] => electronics
[parent_id] => 0
[childs] => Array
(
[33] => Array
(
[id] => 33
[name] => laptops
[parent_id] => 3
[childs] => Array
(
)
)
[34] => Array
(
[id] => 34
[name] => television
[parent_id] => 3
[childs] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] => mobiles & tablets
[parent_id] => 0
[childs] => Array
(
[35] => Array
(
[id] => 35
[name] => mobiles
[parent_id] => 4
[childs] => Array
(
)
)
[36] => Array
(
[id] => 36
[name] => tablets
[parent_id] => 4
[childs] => Array
(
)
)
)
)
[5] => Array
(
[id] => 5
[name] => baby & toys
[parent_id] => 0
[childs] => Array
(
)
)
[6] => Array
(
[id] => 6
[name] => home
[parent_id] => 0
[childs] => Array
(
)
)
[7] => Array
(
[id] => 7
[name] => perfumes & beauty
[parent_id] => 0
[childs] => Array
(
)
)
[8] => Array
(
[id] => 8
[name] => sports & fitness
[parent_id] => 0
[childs] => Array
(
)
)
[9] => Array
(
[id] => 9
[name] => automotive
[parent_id] => 0
[childs] => Array
(
)
)
)
I need an output array with key,the category id
You can create array by this way:
$array = array(
'category_1' => 'value',
'category_2' => array(
'subcategory_1' => 'value',
'subcategory_2' => 'value',
),
);
I have a long array from which I wish to fetch all the values and store it in a separate variable, and store each value in database.
The array that I have is:
Array
(
[success] => 1
[categories] => Array
(
[0] => Array
(
[category_id] => 39
[name] => BAGS
[categories] => Array
(
[0] => Array
(
[category_id] => 59
[name] => Handcrafted Purses
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 45
[parent_id] => 39
[name] => Laptop Bag
[categories] =>
[status] => 1
)
)
[status] => 1
)
[1] => Array
(
[category_id] => 40
[name] => BOXERS
[categories] => Array
(
[0] => Array
(
[category_id] => 56
[parent_id] => 40
[name] => Women Boxers
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 91
[parent_id] => 0
[name] => Business Corporate
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 92
[parent_id] => 91
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 93
[parent_id] => 92
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 60
[parent_id] => 0
[name] => Business Corporates
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 90
[parent_id] => 60
[name] => Art Cushions
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 67
[parent_id] => 60
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 77
[parent_id] => 67
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 76
[parent_id] => 67
[name] => Smart Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 86
[parent_id] => 60
[name] => Fashion Jewellery
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 88
[parent_id] => 86
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 61
[parent_id] => 60
[name] => Men Footwear
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 65
[parent_id] => 61
[name] => Canvas Loafers
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 87
[parent_id] => 60
[name] => Shawls And Stoles
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 89
[parent_id] => 87
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 15
[parent_id] => 0
[name] => ETHNIC WEAR
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 28
[parent_id] => 15
[name] => Designer Lehngas
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 2
[parent_id] => 15
[name] => Suits
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
)
I am able to fetch the outer values of this array by using this code:
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
}
}
I got values as:
39
40
91
60
15
16
38
57
But I also wish to access the inner most values of the array. Can anyone tell how I can create a nested loop?
If you now how deep your array is you can just simply add other foreach-loops within your main loop.
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
if(isset($category['categories'])){
foreach($category['categories'] as $category2)
{
echo $category2['category_id'];
echo "<br>";
if (isset($category2['categories'])){
foreach($category2['categories'] as $category3)
{
echo $category3['category_id'];
echo "<br>";
...
} }
}}
}
}
I have the following code for the array below
$arrCompany = array();
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany)){
$arrCompany['company'][] = $user['User']['company_id'];
}else{}
}
what I am trying to do is just have one entery of company id so I can add users under it but for some reasons its not working
here is my array
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1
[company_id] => 20
[type] =>
)
)
[1] => Array
(
[User] => Array
(
[id] => 6
[company_id] => 21
[type] =>
)
)
[2] => Array
(
[User] => Array
(
[id] => 7
[company_id] => 22
[type] =>
)
)
[3] => Array
(
[User] => Array
(
[id] => 14
[company_id] => 21
[type] =>
)
)
[4] => Array
(
[User] => Array
(
[id] => 15
[company_id] => 22
[type] =>
)
)
[5] => Array
(
[User] => Array
(
[id] => 16
[company_id] => 21
[type] =>
)
)
)
)
when I do var_dump...i get this
Array
(
[company] => Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 21
[4] => 22
[5] => 21
)
)
you can see the company has been repeated
$arrCompany = array('company'=>array());
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany['company'])){
$arrCompany['company'][] = $user['User']['company_id'];
}
}