I have this array(output of var_dump()):
array (size=32)
2 => &
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '1' (length=1)
'id' => string '2' (length=1)
'options' => string '2' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '2' (length=1)
'children' =>
array (size=3)
3 => &
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '2' (length=1)
'id' => string '3' (length=1)
'options' => string '3' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '3' (length=1)
'children' =>
array (size=2)
8 => &
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '3' (length=1)
'id' => string '8' (length=1)
'options' => string '1' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '8' (length=1)
'children' =>
array (size=2)
11 => &
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '8' (length=1)
'id' => string '11' (length=2)
'options' => string '3' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '11' (length=2)
As you can see some of key are passed by reference, I want change this array to regular array, like this:
array (size=32)
0 =>
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '1' (length=1)
'id' => string '2' (length=1)
'options' => string '2' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '2' (length=1)
'children' =>
array (size=3)
0 =>
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '2' (length=1)
'id' => string '3' (length=1)
'options' => string '3' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '3' (length=1)
'children' =>
array (size=2)
0 =>
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '3' (length=1)
'id' => string '8' (length=1)
'options' => string '1' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '8' (length=1)
'children' =>
array (size=2)
0 =>
array (size=3)
'data' =>
array (size=3)
'parent_id' => string '8' (length=1)
'id' => string '11' (length=2)
'options' => string '3' (length=1)
'attr' =>
array (size=2)
'rel' => string 'container' (length=9)
'id' => string '11' (length=2)
Just json_encode and json_decode
$array = json_decode(json_encode($array), true);
Also you could use this function.
function deReferencing($value)
{
if (is_array($value))
{
$return = array();
foreach ($value as $key => $item)
{
unset($value[$key]);
if (is_numeric($key)) {
$return[] = deReferencing($item);
} else {
$return[$key] = deReferencing($item);
}
}
}
else
$return = $value;
return $return;
}
$array = deReferencing($array);
Related
I have a database structure like this:
ID name sort parent
1 item1 1 0
2 subitem1 2 1
3 subsubitem1 1 2
4 subitem2 1 1
I write the database into an array
array (size=4)
0 =>
array (size=4)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
1 =>
array (size=4)
'id' => string '2' (length=1)
'name' => string 'subitem1' (length=8)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
2 =>
array (size=4)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
3 =>
array (size=4)
'id' => string '4' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
and restructure that array to set up child-parent relations with this function:
function generateNavArray($arr, $parent = 0)
{
$items = Array();
foreach($arr as $item)
{
if($item['parent'] == $parent)
{
$item['child'] = isset($item['child']) ? $item['child'] : GenerateNavArray($arr, $item['id']);
$items[] = $item;
}
}
return $items;
}
and the generated array looks like this
array (size=1)
0 =>
array (size=5)
'id' => string '1' (length=1)
'name' => string 'item1' (length=5)
'parent' => string '0' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=2)
0 =>
array (size=5)
'id' => string '2' (length=1)
'name' => string 'subitem' (length=4)
'parent' => string '1' (length=1)
'sort' => string '2' (length=1)
'child' =>
array (size=1)
0 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subsubitem1' (length=11)
'parent' => string '2' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
1 =>
array (size=5)
'id' => string '3' (length=1)
'name' => string 'subitem2' (length=8)
'parent' => string '1' (length=1)
'sort' => string '1' (length=1)
'child' =>
array (size=0)
empty
now i need to sort every dimension of the array by the sort value, (my "real array" has more subarrays then this one).
i played around with multisort but i can't seem to find the solution
any ideas?
Sort the array when it is 1 dimension before you build the multi-dimensional array. Even better if you are using a query, sort it there. Sort by parent then sort. When you build your multidimensional array, each child will be appended to the parent. If they are already in the correct order, they will end up in the same order.
I have 2 arrays that have the same keys but different values. I am trying to merge just the values into a new array. I have tried array_merge, array_merge_recursive, array1+array2, array_intersect, array_intersect_key(array_merge())
MAIN ARRAY
array (size=2)
'attr' =>
array (size=6)
'attr__6__23__grad_gown_size' =>
array (size=3)
'val' => string '5ft.9in. - 5ft.11in.' (length=20)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=1)
...
'attr__1__23__grad_dvd' =>
array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' =>
array (size=1)
...
'attr__2__23__grad_combo' =>
array (size=3)
'val' => string 'Yes' (length=3)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=1)
...
'attr__3__23__grad_tas_dip_only' =>
array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' =>
array (size=1)
...
'attr__4__23__grad_tassel' =>
array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' =>
array (size=1)
...
'attr__5__23__grad_honor_seal' =>
array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' =>
array (size=1)
...
'attr_add' =>
array (size=6)
'attr__6__23__grad_gown_size' =>
array (size=3)
'val' => string '' (length=0)
'qty' => string '0' (length=1)
'ordqty' =>
array (size=0)
...
'attr__1__23__grad_dvd' =>
array (size=3)
'val' => string 'Yes' (length=3)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=0)
...
'attr__2__23__grad_combo' =>
array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' =>
array (size=0)
...
'attr__3__23__grad_tas_dip_only' =>
array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' =>
array (size=0)
...
'attr__4__23__grad_tassel' =>
array (size=3)
'val' => string 'Yes' (length=3)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=0)
...
'attr__5__23__grad_honor_seal' =>
array (size=3)
'val' => string 'Yes' (length=3)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=0)
...
Array 1 attr
array (size=3)
'val' => string '5ft.9in. - 5ft.11in.' (length=20)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=1)
27 => string '1' (length=1)
array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' =>
array (size=1)
27 => string '0' (length=1)
Array 2 attr_add
array (size=3)
'val' => string '' (length=0)
'qty' => string '0' (length=1)
'ordqty' =>
array (size=1)
30 => string '0' (length=1)
array (size=3)
'val' => string 'Yes' (length=3)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=1)
30 => string '1' (length=1)
Result
array (size=1)
'attr' =>
array (size=6)
'attr__6__23__grad_gown_size' =>
array (size=3)
'val' => string '5ft.9in. - 5ft.11in.' (length=20)
'qty' => string '1' (length=1)
'ordqty' =>
array (size=2)
27 => string '1' (length=1)
30 => string '0' (length=1)
'attr__1__23__grad_dvd' =>
array (size=3)
'val' => Yes
'qty' => string '1' (length=1)
'ordqty' =>
array (size=1)
27 => string '0' (length=1)
30 => string '1' (length=1)
What I'm trying
foreach($extra_fields_array['attr'] as $key=>$value)
{
$ovalue = $extra_fields_array['attr'][$key]; // Array 1
$nvalue = $attr_array['attr_add'][$key]; // Array 2
$new_array['attr'][$key] = array_merge($nvalue,$ovalue); // NOT WORKING
/*
* What do I do here
*/
}
echo '<pre>'; var_dump($new_array);echo '</pre>';
I hope this makes sense. I've been stuck on it all day.
Thanks for any help in advance.
I have a few records from database and I fetched all records. The array return as the code below
array (size=10)
0 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
1 =>
array (size=5)
'id' => string '2' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
2 =>
array (size=5)
'id' => string '3' (length=1)
'type' => string 'team' (length=4)
'members' => string '["3","5","6"]' (length=13)
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
3 =>
array (size=5)
'id' => string '4' (length=1)
'type' => string 'team' (length=4)
'members' => string '["1"]' (length=5)
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
4 =>
array (size=5)
'id' => string '5' (length=1)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
5 =>
array (size=5)
'id' => string '8' (length=1)
'type' => string 'team' (length=4)
'members' => string '["4"]' (length=5)
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
6 =>
array (size=5)
'id' => string '9' (length=1)
'type' => string 'team' (length=4)
'members' => string '["2"]' (length=5)
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
7 =>
array (size=5)
'id' => string '10' (length=2)
'type' => string 'team' (length=4)
'members' => null
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
8 =>
array (size=5)
'id' => string '11' (length=2)
'type' => string 'team' (length=4)
'members' => null
'parents' => string '5' (length=1)
'level' => string '3' (length=1)
9 =>
array (size=5)
'id' => string '12' (length=2)
'type' => string 'group' (length=5)
'members' => null
'parents' => string '1' (length=1)
'level' => string '2' (length=1)
the members field has been encoded as json string. I want to build a array with format as the code below
array('1'=>array(1,2,3,4,5,6),
'2'=>array(),
'3'=>array(3,5,6),
'4'=>array(1),
'5'=>array(2,4),
'8'=>array(4),
'9'=>array(2))
The record's id will be key of array and the members has decoded will become the value. I wrote a function handle the array return from the database but the result not as my intention. This is my code
$results = mysql_query('select id,type,members,parents,level from team');
$array = array();
recursiveDate($results,0,$array);
function recursiveData($sourceArr,$parents = 0, &$newMenu){
if(count($sourceArr)>0){
foreach ($sourceArr as $key => $value){
if($value['parents'] == $parents){
if(isset($newMenu[$value['id']])){
$newMenu[$value['id']] = array();
}
$newMenu[$value['id']] = $value['members']?json_decode($value['members']):array();
if(isset($newMenu[$parents])){
$newMenu[$parents] = array_merge($newMenu[$value['id']],$newMenu[$parents]);
}
$newParents = $value['id'];
unset($sourceArr[$key]);
$this->recursiveData($sourceArr,$newParents, $newMenu);
}
}
}
}
And this is the array after handled
array (size=10)
1 =>
array (size=4)
0 => string '1' (length=1)
1 => string '3' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
3 =>
array (size=3)
0 => string '3' (length=1)
1 => string '5' (length=1)
2 => string '6' (length=1)
4 =>
array (size=1)
0 => string '1' (length=1)
5 =>
array (size=2)
0 => string '2' (length=1)
1 => string '4' (length=1)
8 =>
array (size=1)
0 => string '4' (length=1)
9 =>
array (size=1)
0 => string '2' (length=1)
10 =>
array (size=0)
empty
11 =>
array (size=0)
empty
12 =>
array (size=0)
empty
2 =>
array (size=0)
empty
Please help me build that array
I quickly wrote this so not sure if it's exactly what you're looking for, looks like you over complicated it hugely though. I kept the original result structure incase you wanted that but you can easily overwrite it if you wish.
function modifyData($data = array()) {
foreach($data as &$entry) {
if(!empty($entry['members'])) {
$entry['members'] = json_decode($entry['members'], true);
} else {
$entry['members'] = array();
}
}
return $data;
}
I tested this with
$data = array(
array(
'id' =>'1',
'type' =>'group',
'members' => null,
'parents' =>'0',
'level' =>'1'
),
array(
'id' =>'1',
'type' =>'group',
'members' => '["3","5","6"]',
'parents' =>'0',
'level' =>'1'
),
array(
'id' =>'1',
'type' =>'group',
'members' => '["3"]',
'parents' =>'0',
'level' =>'1'
)
);
And the output is
array (size=3)
0 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=0)
empty
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
1 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=3)
0 => string '3' (length=1)
1 => string '5' (length=1)
2 => string '6' (length=1)
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
2 =>
array (size=5)
'id' => string '1' (length=1)
'type' => string 'group' (length=5)
'members' =>
array (size=1)
0 => string '3' (length=1)
'parents' => string '0' (length=1)
'level' => string '1' (length=1)
I am trying to merge 2 arrays: 1 multidimensional and another one normal:
Multidimensional array - $_SESSION["products"]
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '1' (length=1)
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '1' (length=1)
'price' => string '2300.50' (length=7)
Simple array - $qty
array (size=2)
0 => string '2' (length=1)
1 => string '3' (length=1)
EXPECTED OUTPUT
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '2' (length=1) // notice the qty change
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '3' (length=1) // notice the qty change
'price' => string '2300.50' (length=7)
I tried:
foreach ($_SESSION["products"] as $cart_itm){
foreach($qty as $qt) {
$cart_itm['qty'] = $qt;
}
}
But did not work, cart_itm['qty'] remained the same (1).
Try with this:
foreach ($_SESSION["products"] as $key => &$cart_itm){
$cart_itm['qty'] = $qty[$key];
}
I have this multidimensional array
I am wondered how can i sort this array again so i can use it in for loop.
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
3 =>
array (size=1)
3 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
How can I reindex this array to become
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
1 =>
array (size=1)
1 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
I tried array_shift and array_chunk but nothing works !!!
Please help, thank you all :)
Use array_multisort to sort multi-dimensional arrays or to sort an array using multiple keys.
I think this should do it but it's be a lot cleaner if you didn't have the extra level off the array.
$new_array = array();
$index = 0;
foreach($array as $i1 => $a1){
foreach($a1 as $i2 => $a2){
$new_array[$index][$index] = $a2;
}
$index++;
}
You can use 'array_values' for re-indexing which start index from 0. As per your requirement inner array are not starting from 0 but are same as parent array index. You have to use foreach for that. To index the way you want can be done like this:
$info = array(
0 => array (
0 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
2 => array (
2 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
3 => array (
3 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
)
);
var_dump($info); // original index
$info = array_values($info);
foreach ($info as $key => $value) {
$temp = array();
foreach($value as $k => $v) {
$temp[$key] = $v;
}
$info[$key] = $temp;
}
var_dump($info); // re-index