php multidimensional array sort - php

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

Related

Loop through multi-dimensional array and remove duplicate and merge other values

I have array like below. I want to unset all array having same q_id but merge its name key.
array
0 =>
array
'field_name' => string 'Hindu rastrya' (length=13)
'name' => string '283' (length=3)
'q_id' => string '199' (length=3)
1 =>
array
'field_name' => string 'dharma nirpachya' (length=16)
'name' => string '284' (length=3)
'q_id' => string '199' (length=3)
2 =>
array
'field_name' => string 'j vaye pni hunxa' (length=16)
'name' => string '285' (length=3)
'q_id' => string '199' (length=3)
3 =>
array
'field_name' => string 'Nepal' (length=5)
'name' => string '286' (length=3)
'q_id' => string '200' (length=3)
4 =>
array
'field_name' => string 'India' (length=5)
'name' => string '287' (length=3)
'q_id' => string '200' (length=3)
5 =>
array
'field_name' => string 'China' (length=5)
'name' => string '288' (length=3)
'q_id' => string '200' (length=3)
Expected:
array
0 =>
array
'field_name' => string 'Hindu rastrya' (length=13)
'name' =>
array
0 => string '283' (length=3)
1 => string '284' (length=3)
2 => string '285' (length=3)
'q_id' => string '199' (length=3)
1 =>
array
'field_name' => string 'Nepal' (length=16)
'name' =>
array
0 => string '286' (length=3)
1 => string '287' (length=3)
2 => string '288' (length=3)
'q_id' => string '200' (length=3)
I tried following code but It didnot preserve the keys so it fails:
foreach ($form_option_data as $key=>$option)
{
if($form_option_data[$key]['q_id'] == $form_option_data[$key+1]['q_id']){
unset($form_option_data[$key+1]);
}
}
The following should do the job:
$data = ...; //initial array
$result = array();
foreach ($data as $entry) {
if (!array_key_exists($entry['q_id'], $result)) {
$result[$entry['q_id']] = array(
'q_id' => $entry['q_id'],
'name' => array($entry['name'])
);
} else {
$result[$entry['q_id']]['name'][] = $entry['name'];
}
}
$result = array_values($result); // to re-index the table so that keys start from 0

how to decode a json string and add to array in PHP

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)

combine products with same category in array

I have an array that contains products and their categoryID, some of products have the same category. the output of the array is below
array (size=5)
'categoryID' =>
array (size=3)
0 => string '5' (length=1)
1 => string '2' (length=1)
2 => string '2' (length=1)
'name' =>
array (size=3)
0 => string 'HP Player' (length=9)
1 => string 'Android App' (length=11)
2 => string 'TV' (length=2)
'price' =>
array (size=3)
0 => string '600' (length=3)
1 => string '111' (length=3)
2 => string '1' (length=1)
'qty' =>
array (size=3)
0 => string '22' (length=2)
1 => string '22' (length=2)
2 => string '222' (length=3)
'exprice' =>
array (size=3)
0 => string '13200' (length=5)
1 => string '2442' (length=4)
2 => string '222' (length=3)
I want to combine the products that have same categoryID, the output should be like below:
array (size=2)
=> 0 array
'categoryID' =>
array
0 => string '5' (length=1)
'name' =>
array
0 => string 'HP Player' (length=9)
'price' =>
array
0 => string '600' (length=3)
'qty' =>
array
0 => string '22' (length=2)
'exprice' =>
array
0 => string '13200' (length=5)
=> 1 array
'categoryID' =>
array
0 => string '2' (length=1)
'name' =>
array
0 => string '2' (length=1)
1 => string '2' (length=1)
'price' =>
array
0 => string '111' (length=3)
1 => string '1' (length=1)
'qty' =>
array
0 => string '22' (length=2)
1 => string '222' (length=3)
'exprice' =>
array
0 => string '2442' (length=4)
1 => string '222' (length=3)
how could I do that? Thanks
$arr = array (
'categoryID' =>
array (
0 => '5',
1 => '2',
2 => '2'
),
'name' =>
array (
0 => 'HP Player',
1 => 'Android App',
2 => 'TV'
),
'price' =>
array (
0 => '600',
1 => '111',
2 => '1'
),
'qty' =>
array (
0 => '22',
1 => '22',
2 => '222'
),
'exprice' =>
array (
0 => '13200',
1 => '2442',
2 => '222'
)
);
$cat_count = count(array_keys($arr['categoryID']));
$new_arr = array();
for($i=0;$i<$cat_count;$i++){
for($j=0;$j<$i;$j++){
if($arr['categoryID'][$i] == $arr['categoryID'][$j]){
$arr_key = array_search($arr['categoryID'][$i], $arr['categoryID']);
$cat_exists = 1;
}
}
if($cat_exists){
$new_arr[$arr_key]['name'][] = $arr['name'][$i];
$new_arr[$arr_key]['price'][] = $arr['price'][$i];
$new_arr[$arr_key]['qty'][] = $arr['qty'][$i];
$new_arr[$arr_key]['exprice'][] = $arr['exprice'][$i];
}else{
$new_arr[$i]['categoryID'][] = $arr['categoryID'][$i];
$new_arr[$i]['name'][] = $arr['name'][$i];
$new_arr[$i]['price'][] = $arr['price'][$i];
$new_arr[$i]['qty'][] = $arr['qty'][$i];
$new_arr[$i]['exprice'][] = $arr['exprice'][$i];
}
}
P.S :: $arr is input array & $new_arr is output(result) array!

change referenced array to regular array

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);

Combining an array based off value

Alright, I have been pulling my hair out on this one! I have tried an SQL call to add the features to a product, but since the product is one row and the features are not it creates a new row for each product. I thought I could add both to an array and join them based off the product ID. Here are the arrays
array
0 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Brady ENV100 MAXX Enhanced Sorbents' (length=35)
'number_sold' => string '6' (length=1)
'link_rewrite' => string 'brady-env100-maxx-enhanced-sorbents' (length=35)
'id_image' => string '27' (length=2)
1 =>
array
'id_product' => string '10' (length=2)
'name' => string 'Brady GP100 MAXX Enhanced Heavy Pad' (length=35)
'number_sold' => string '3' (length=1)
'link_rewrite' => string 'brady-gp100-maxx-enhanced-heavy-pad' (length=35)
'id_image' => string '29' (length=2)
array
0 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Height' (length=6)
'value' => string '5' (length=1)
1 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Width' (length=5)
'value' => string '5' (length=1)
2 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Depth' (length=5)
'value' => string '5' (length=1)
3 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Weight' (length=6)
'value' => string '5' (length=1)
4 =>
array
'id_product' => string '10' (length=2)
'name' => string 'Height' (length=6)
'value' => string '10' (length=2)
I have looked at many tutorials, but can't seem to get anything to work. What I would like it to look like is
array
0 =>
array
'id_product' => string '9' (length=1)
'name' => string 'Brady ENV100 MAXX Enhanced Sorbents' (length=35)
'number_sold' => string '6' (length=1)
'link_rewrite' => string 'brady-env100-maxx-enhanced-sorbents' (length=35)
'id_image' => string '27' (length=2)
name' => string 'Height' (length=6)
'value' => string '5' (length=1)
'name' => string 'Width' (length=5)
'value' => string '5' (length=1)
'name' => string 'Depth' (length=5)
'value' => string '5' (length=1)
and so on.
You can try this
For your product array do this:
$result = array();
foreach ($mainItems as $item){
$result[$item["id_product"]] = $item;
}
Then for each of your attribute arrays
foreach($attributes as $val){
if (array_key_exists($val["id_product"], $result)){
$result[$val["id_product"]][$val["name"]] = $val["value"];
}
}
You should end up with an array that looks like
[9] => array(
[id_product] => 9
[name] => "Brady ENV100 MAXX Enhanced Sorbents"
[number_sold] => "6"
[link_rewrite] => "brady-env100-maxx-enhanced-sorbents"
[id_image] => "27"
[Height] => 5
[Width] => 5
[Depth] => 5
),
[10] => array(
[id_product] => '10'
[name] => 'Brady GP100 MAXX Enhanced Heavy Pad'
[number_sold] => '3'
[link_rewrite] => 'brady-gp100-maxx-enhanced-heavy-pad'
[id_image] => '29'

Categories