Making a nested array in php - php

Probably my question is too much common or easy for you, but I really have no idea how to do it. Trying all everything I know and lots of googling didn't help me.
I just need a nested array.
Here is my PHP code:
EDITED
Corrected: $data['product_names'][$language['language_id']][] = array(
$data['product_names'] = array();
foreach ($data['languages'] as $language) {
$product_names_info = $this->model_catalog_category->getCategoryMultiLang($this->request->get['product_id'], $language['language_id']);
if ($product_names_info) {
$data['product_names'][$language['language_id']][] = array(
'category_id' => $product_names_info['category_id'],
'language_id' => $product_names_info['language_id'],
'name' => $product_names_info['name']
);
}
}
print_r($data['product_names']);
The result that I get:
Array
(
[5] => Array
(
[0] => Array
(
[category_id] =>
[language_id] =>
[name] =>
)
)
[2] => Array
(
[0] => Array
(
[category_id] =>
[language_id] =>
[name] =>
)
)
[4] => Array
(
[0] => Array
(
[category_id] =>
[language_id] =>
[name] =>
)
)
...
The result should look like this:
Array
(
[0] => Array
(
[language_id] => 1
[category_id] => 8
[name] => book
)
[1] => Array
(
[language_id] => 5
[category_id] => 188
[name] => magazine
)
...
)
Array
(
[0] => Array
(
[language_id] => 1
[category_id] => 8
[name] => buch
)
...
UPDATED
The result of print_r($product_names_info); inside foreach ($data['languages'] as $key => $language) {
Array
(
[0] => Array
(
[language_id] => 5
[category_id] => 8
[name] => Gecelik
)
[1] => Array
(
[language_id] => 5
[category_id] => 188
[name] => Sabahlık
)
...
)
Array
(
[0] => Array
(
[language_id] => 2
[category_id] => 8
[name] => لباس خواب
)
[1] => Array
(
[language_id] => 2
[category_id] => 188
[name] => Sabahlık
)
Thanks for any kind help.

$data['product_names'] = array();
foreach ($data['languages'] as $language) {
$product_names_info = $this->model_catalog_category->getCategoryMultiLang($this->request->get['product_id'], $language['language_id']);
if ($product_names_info) {
foreach ($product_names_info as $key => $value) {
$data['product_names'][$language['language_id']][] = array(
'category_id' => $value['category_id'],
'language_id' => $value['language_id'],
'name' => $value['name'],
);
}
}
}
print_r($data['product_names']);
I think you were missing foreach for the data you fetched into var $product_names_info

Related

PHP array with n level deep

Assuming I have an array and recursively want to do some task. I have spent one day for this unable to solve this. May be my logic is not good.
Any help on how to do such thing in an efficient way would save my days.
I have an array with n level deep, looking like:
Array
(
[0] => Array
(
[name] => user1
[email] => user1#demo.com
[depth] => 1
)
[1] => Array
(
[name] => user2
[email] => user2#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user2.1
[email] => user2.1#demo.com
[depth] => 2
)
)
)
[2] => Array
(
[name] => user3
[email] => user3#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user3.1
[email] => user3.1#demo.com
[depth] => 2
[0] => Array
(
[0] => Array
(
[name] => user3.1.1
[email] => user3.1.1#demo.com
[depth] => 3
)
)
)
[1] => Array
(
[name] => user3.2
[email] => user3.2#demo.com
[depth] => 2
)
)
)
)
I want to change above array in exactly this format:
array(
0 => array(
'name' => 'user1',
),
1 => array(
'name' => 'user2',
'children' => array(
0 => array(
'name' => 'user2.1',
) ,
) ,
) ,
2 => array(
'name' => 'user3',
'children' => array(
0 => array(
'name' => 'user3.1',
'children' => array(
0 => array(
'name' => 'user3.1.1',
) ,
) ,
) ,
1 => array(
'name' => '3.2',
)
) ,
) ,
)
Edited:
I am using this code and working fine if i want to show data in tree format but unable to push data into array as i want.
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
displayArrayRecursively($value, $indent . '-->');
} else {
if ($key == 'name')
{
echo "$indent $value <br>";
}
else {
continue;
}
}
}
}
}
displayArrayRecursively($userListArray);
Can anyone suggest me how to do this?
Thank you
This function will do what you want:
function process_nodes($nodes) {
$new = array();
foreach ($nodes as $node) {
$new[] = array('name' => $node['name']);
if (isset($node[0])) {
$new[count($new)-1]['children'] = process_nodes($node[0]);
}
}
return $new;
}
print_r(process_nodes($data));
Output:
Array
(
[0] => Array
(
[name] => user1
)
[1] => Array
(
[name] => user2
[children] => Array
(
[0] => Array
(
[name] => user2.1
)
)
)
[2] => Array
(
[name] => user3
[children] => Array
(
[0] => Array
(
[name] => user3.1
[children] => Array
(
[0] => Array
(
[name] => user3.1.1
)
)
)
[1] => Array
(
[name] => user3.2
)
)
)
)

PHP group multidimensional array by repeated values

I have a simple two dimensional array like this:
Array
(
[0] => Array
(
[id] => 1
[name] => John
[company] => One
[price] => 12.22
)
[1] => Array
(
[id] => 1
[name] => John
[company] => Two
[price] => 14.33
)
[2] => Array
(
[id] => 2
[name] => Mike
[company] => One
[price] => 15.11
)
[3] => Array
(
[id] => 2
[name] => Mike
[company] => Two
[price] => 10.12
)
[4] => Array
(
[id] => 3
[name] => Paul
[company] => One
[price] => 42.22
)
[5] => Array
(
[id] => 3
[name] => Paul
[company] => Two
[price] => 56.62
)
[6] => Array
(
[id] => 3
[name] => Paul
[company] => Three
[price] => 16.12
)
)
I need to group id and name, then create an array with different values something like this:
Array
(
[0] => Array
(
[id] => 1
[name] => John
[companies] => array (
array(
[company] => One
[price] => 12.22
)
array(
[company] => Two
[price] => 14.33
)
)
)
[1] => Array
(
[id] => 2
[name] => Mike
[companies] => array (
array(
[company] => One
[price] => 15.11
)
array(
[company] => Two
[price] => 10.12
)
)
)
[2] => Array
(
[id] => 3
[name] => Paul
[companies] => array (
array(
[company] => One
[price] => 42.22
)
array(
[company] => Two
[price] => 56.62
)
array(
[company] => Three
[price] => 16.12
)
)
)
)
What is the best way to do it with PHP?
This is my attemp:
<?php
$items=array();
$temp = 0;
$companies = array('uno','dos','tres');
foreach ($values as $value) {
if ($temp == $value['id'] )
continue;
else
$temp == $value['id'];
foreach ($companies as $key => $company){
foreach ($values as $item){
if ($item['id'] == $temp && $item['company'] == $key)
$value['company'][$key] = $item['price'];
}
}
$items[] = $value;
}
Use the id as the key for the new array, then just append a new array with the next company and price
foreach($array as $v) {
$result[$v['id']]['id'] = $v['id'];
$result[$v['id']]['name'] = $v['name'];
$result[$v['id']]['companies'][] = array('company' => $v['company'],
'price' => $v['price']);
}
If you need to re-index it (probably not):
$result = array_values($result);

Create associative array with value as key

I have this array with information, which I want to create a new associative array with. Each key in the associative array should be the "name" from the old one. And in each new key, I want the corresponding information to be collected.
Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
[2] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
[3] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[4] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
This is the array I'm trying to create:
Array
(
["Ben"] => Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[2] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
["Evan"] => Array
(
[0] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
)
["Steve"] => Array
(
[0] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
)
)
$newArr = array();
foreach($myArr as $value) {
$name = $value['name'];
if (isset($newArr[$name])) {
$newArr[$name][] = $value;
}
else {
$newArr[$name] = array($value);
}
}
Use a foreach loop to create a new array:
$newArr = [];
foreach($myArr as $key => $value){
$newArr[$myArr[$key][$value['name']]][] = $myArr[$key];
}

The difference between output return and print_r

This is a controller in codeigniter, i have problem in return on function get_reunits() when run function marge_orderfun(). when i run function get_reunits() with print_r in output(print_r($hotel_data);) i give following output(it is what that i want):
Array (
[0] => Array (
[name] => 11
[price] => 77192276
[extra] => 11
[hotel_id] => 77192276
)
[1] => Array (
[name] => 11
[price] => 77192276
[extra] => 11
[hotel_id] => 77192276
)
) Array (
[0] => Array (
[name] => 666666666
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
[1] => Array (
[name] => 99999
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
[2] => Array (
[name] => 777777
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
[3] => Array (
[name] => 1221
[price] => 15190364
[extra] => 11
[hotel_id] => 15190364
)
) Array (
[0] => Array (
[name] => 666666666
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
[1] => Array (
[name] => 99999
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
[2] => Array (
[name] => 777777
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
[3] => Array (
[name] => 1221
[price] => 11
[extra] => 33
[hotel_id] => 15183965
)
)
When use from return $hotel_data; for output and run function marge_orderfun() i get this output:
{"reunits":[{"name":"11","price":"77192276","extra":"11","hotel_id":"77192276"},{"name":"11","price":"77192276","extra":"11","hotel_id":"77192276"}]}
This is my controller:
function get_reunits(){
//$tourf_id = $this->input->post('tour_name');
$tourf_id = '102';
$query_r = $this->db->order_by('id','desc')->get_where('tour_foreign_residence', array('relation' => $tourf_id));
foreach($query_r->result() as $idx=>$val){
$hotel_id = $val->hotel_id;
$query = $this->db->get_where('tour_foreign_units', array('hotel_id' => $hotel_id));
if($query->num_rows() > 0){
$hotel_data = array();
foreach ($query->result() as $index=>$row) {
$hotel_data[] = array(
'name' => $row->name,
'price' => $row->price,
'extra' => $row->extra,
'hotel_id' => $row->hotel_id
);
}
}else{
return 0;
}
//print_r($hotel_data);
return $hotel_data;
}
}
function marge_orderfun(){
//$guide = array('guide' => $this->get_gr());
//$residence = array('residence' => $this->get_residence());
$reunits = array('reunits' => $this->get_reunits());
echo json_encode(array_merge(/*$guide, $residence,*/$reunits));
}
What do i do for a output in marge_orderfun() as output in print_r in get_reunits()?
You're outputting two different things, actually. print_r is dumping out the structure/contents of a PHP array in a nicely readable format. your marge_orderfun is dump out a string of text which happens to be in JSON format. This json string USED to be a PHP array, but now it's just a blob of text.
Maybe this code helps:
$a = array();
$b = print_r($a, true);
echo $b;

array difference

I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;

Categories