I have a multidimensional Array like this:
Array (
[0] => Array (
[id] => 1
[name] => privilages1
)
[1] => Array (
[id] => 2
[name] => privilages2
)
[2] => Array (
[id] => 3
[name] => privilages3
)
[3] => Array (
[id] => 4
[name] => privilage4 )
[4] => Array (
[id] => 5
[name] => privilages5 )
)
and i want to compare it with another array, which looks like this:
Array (
[0] => Array (
[id] => 1 )
[1] => Array (
[id] => 2)
)
if the value of id matches, then i want to all values from the first example.
How can i do this?
You can use array_filter to filter the array elements you want by supplying a user defined callback function.
Here is the code:
$arr = array( array('id' => 1, 'name' => 'foo'),
array('id' => 2, 'name' => 'bar'),
array('id' => 3, 'name' => 'baz'),
array('id' => 4, 'name' => 'wow'));
$ret = array_filter($arr, create_function('$el',
'static $search=array(array("id" => 1), array("id" => 2));
$n=array("id" => $el["id"]);
return (array_search($n, $search) !== false);'));
print_r($ret);
OUTPUT
Array
(
[0] => Array
(
[id] => 1
[name] => foo
)
[1] => Array
(
[id] => 2
[name] => bar
)
)
Related
I tries to array_merge in php but resultant array is not correct
1. Array ( [id] => 12 [name] => Popular )
2. Array ( [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
Resultant Array
Array ( [id] => 12 [name] => Popular [0] => Array ( [id] => 8 [name] => Flowers ) [1] => Array ( [id] => 10 [name] => Chocolates ) [2] => Array ( [id] => 11 [name] => Sweets and Dry Fruits ) )
If you just want to add the new data in the same format as the existing data then use [] rather than array_merge().
$array1 = array( 'id' => 12, 'name' => 'Popular');
$array2 = array(array( 'id' => 8, 'name' => 'Flowers'),
array( 'id' => 10, 'name' => 'Chocolates'),
array( 'id' => 11, 'name' => 'Sweets and Dry Fruits')
);
$array2[] = $array1;
print_r($array2);
outputs...
Array
(
[0] => Array
(
[id] => 8
[name] => Flowers
)
[1] => Array
(
[id] => 10
[name] => Chocolates
)
[2] => Array
(
[id] => 11
[name] => Sweets and Dry Fruits
)
[3] => Array
(
[id] => 12
[name] => Popular
)
)
If you want the data to be at the front, then you need to create an array of that data and then use array_merge()...
$array3 = array_merge(array($array1), $array2);
print_r( $array3);
I have one array in two format. I want to change array from
Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 2
[1] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 2
)
[1] => Array
(
[name] => app_id[]
[value] => 3
)
[2] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[3] => Array
(
[name] => fieldval[3][3][]
[value] => 200
)
[4] => Array
(
[name] => fieldval[3][3][]
[value] => day
)
[5] => Array
(
[name] => title
[value] => new plan
)
[6] => Array
(
[name] => feature_plan
[value] => 3
)
[7] => Array
(
[name] => plan_type
[value] => free
)
[8] => Array
(
[name] => price
[value] =>
)
[9] => Array
(
[name] => sell_type
[value] => us
)
)
)
this format to
Array
(
[app_id] => Array
(
[0] => 2
[1] => 3
)
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
)
)
[3] => Array
(
[3] => Array
(
[0] => 200
[1] => day
)
)
)
[title] => new plan
[feature_plan] => 3
[plan_type] => free
[price] =>
[sell_type] => us
)
these are are one array into two format. i have data in to first array format and i want to change that format to second array type format.
please tell me how i am trying this for 2 days but not succeed.
Here is a function you could use to produce that conversion:
function convert_formdata($input) {
$output = array();
foreach($input['formdata'] as $data) {
$keys = preg_split("#[\[\]]+#", $data['name']);
$value = $data['value'];
$target = &$output;
foreach($keys as $key) {
// Get index for "[]" reference
if ($key == '') $key = count($target);
// Create the key in the parent array if not there yet
if (!isset($target[$key])) $target[$key] = array();
// Move pointer one level down the hierarchy
$target = &$target[$key];
}
// Write the value at the pointer location
$target = $value;
}
return $output;
}
You would call it like this:
$output = convert_formdata($input);
See it run on eval.in for the given input. The output is:
array (
'app_id' =>
array (
0 => 2,
1 => 3,
),
'fieldval' =>
array (
2 =>
array (
2 =>
array (
0 => 1,
),
),
3 =>
array (
3 =>
array (
0 => 200,
1 => 'day',
),
),
),
'title' => 'new plan,',
'feature_plan' => 3,
'plan_type' => 'free',
'price' => NULL,
'sell_type' => 'us',
)
Array
(
[12] => Array
(
[id] => 12
[name] => Car
[children] => Array
(
[0] => Array
(
[id] => 14
[name] => Volvo
)
[1] => Array
(
[id] => 15
[name] => Mercedes-Benz
)
)
)
[13] => Array
(
[id] => 13
[name] => Manga
[children] => Array
(
[0] => Array
(
[id] => 16
[name] => Naruto
)
[1] => Array
(
[id] => 17
[name] => Hunter X Hunter
)
)
)
[18] => Array
(
[id] => 18
[name] => aa
[children] => Array
(
)
)
)
Guys I want to sort the values of this array, i want to sort it by key and the key is 'name'. This should sort the first level and the second level thru key 'name'.
This array i put in print_r() so it looks like this. The array is not fix so i can add in the future.
So after sorting the final value of the array is...
Array
(
[18] => Array
(
[id] => 18
[name] => aa
[children] => Array
(
)
)
[12] => Array
(
[id] => 12
[name] => Car
[children] => Array
(
[0] => Array
(
[id] => 15
[name] => Mercedes-Benz
)
[1] => Array
(
[id] => 14
[name] => Volvo
)
)
)
[13] => Array
(
[id] => 13
[name] => Manga
[children] => Array
(
[0] => Array
(
[id] => 17
[name] => Hunter X Hunter
)
[1] => Array
(
[id] => 16
[name] => Naruto
)
)
)
)
And this is the array in code.
$categories = array(
12 =>
array('id' =>12,
'name' => 'Car',
'children' =>
array(
array('id' => 14,
'name' => 'volvo'
)
),
array(
array('id' => 15,
'name' => 'Mercedez-Benz'
)
)
),
13 =>
array('id' =>13,
'name' => 'Manga',
'children' =>
array(
array('id' => 16,
'name' => 'Naruto'
)
),
array(
array('id' => 17,
'name' => 'Hunter X Hunter'
)
)
),
18=>
array('id' => 18,
'name'=> 'aa',
'children' => array())
);
echo "<pre>";
print_r($categories);
the 'name' is not your real 'Key', just saying, because 'key' in Php normally means the name of your array, like '18' and '12' in your array and so on, like this:
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
in this example, the result will be:
a = orange
b = banana
c = apple
d = lemon
anyway to answer your question on how to sort it on 'name', use 'usort':
function cmp($a, $b) {
return $a['name'] - $b['name'];
}
usort($arr,"cmp");
check out the following link for further information:
How do I sort a PHP array by an element nested inside?
if using php 5.3+ you can use closures in your code and sort like this
usort($categories,function($a,$b){
return $a['name'] - $b['name']
}
So,
I'm going to try and explain it better to you
your array is big, yes, but you want to sort only on the key value '[name]'.
In my test I used the array you provided:
$categories = array(
12 =>
array('id' =>12,
'name' => 'Car',
'children' =>
array(
array('id' => 14,
'name' => 'volvo'
)
),
array(
array('id' => 15,
'name' => 'Mercedez-Benz'
)
)
),
13 =>
array('id' =>13,
'name' => 'Manga',
'children' =>
array(
array('id' => 16,
'name' => 'Naruto'
)
),
array(
array('id' => 17,
'name' => 'Hunter X Hunter'
)
)
),
18=>
array('id' => 18,
'name'=> 'aa',
'children' => array())
);
if the array is declared, you can add the usort like this:
usort($categories,"sortByName");
the first element in the above ($categories), is your array, the second element will provide the name of a function, in this case 'sortByName'.
after that, you just add a function into your code (it doesn't really matter where you put it, even if it's at the bottom of the page):
function sortByName($categories, $b) {
return strcasecmp($categories["name"], $b["name"]);
}
Basically what this function does, is compairing your array with ...your array :) so it can sort it alfabetically by name- like you want it.
sthe 'strcasecmp' is to compare your strings against eachother to sort it. not that this is case non sensitive (strcmp will be case sensitive too). I assume you don't want it to check on uppercase and so on too, otherwise the result won't be what you wanted.
when you added that code, you can just print your array again:
echo "<pre>";
print_r($categories);
your result will be:
Array
(
[0] => Array
(
[id] => 18
[name] => aa
[children] => Array
(
)
)
[1] => Array
(
[id] => 12
[name] => Car
[children] => Array
(
[0] => Array
(
[id] => 14
[name] => volvo
)
)
[0] => Array
(
[0] => Array
(
[id] => 15
[name] => Mercedez-Benz
)
)
)
[2] => Array
(
[id] => 13
[name] => Manga
[children] => Array
(
[0] => Array
(
[id] => 16
[name] => Naruto
)
)
[0] => Array
(
[0] => Array
(
[id] => 17
[name] => Hunter X Hunter
)
)
)
I hope this is what you wanted. :-)
I would like to construct sub n number of arrays from a multi dimensional array depends on the data. For example: I have a main array as
Array
(
[0] => Array
(
[id] => 1
[status] => -1
)
[1] => Array
(
[id] => 2
[status] => 1
)
[2] => Array
(
[id] => 3
[status] => 2
)
[3] => Array
(
[id] => 4
[status] => 2
)
[4] => Array
(
[id] => 5
[status] => 2
)
)
I would like to get 3 arrays from this array depends on the no and type of status value
like
array{
[0]=array(
[status]=-1
[count]=1
)
[1]=array(
[status]=1
[count]=1
)
[2]=array(
[status]=2
[count]=3
)
}
Thanks in advance,
Sunil Kumar P
You mean like this?:
$array = array(
array('id' => 1, 'status' => -1),
array('id' => 2, 'status' => 1),
array('id' => 3, 'status' => 2),
array('id' => 4, 'status' => 2),
array('id' => 5, 'status' => 2)
);
$statuses = array();
foreach($array as $one){
if(!isset($statuses[$one['status']])){ $statuses[$one['status']] = 0; }
$statuses[$one['status']]++;
}
$newArray = array();
foreach($statuses as $key => $val){
$newArray[] = array('status' => $key, 'count' => $val);
}
print_r($newArray);
/*Array
(
[0] => Array
(
[status] => -1
[count] => 1
)
[1] => Array
(
[status] => 1
[count] => 1
)
[2] => Array
(
[status] => 2
[count] => 3
)
)*/
To be more specific I want to turn the following array into an associative one. The original array is indexed like [0],[1],[2],…[n]. The function I used was Set::combine of Cakephp but I couldn't recreate all three levels of the desired associative array.
Array
(
[0] => Array
(
[ACCOUNTS] => Array
(
[description] => A
)
[HEADERS] => Array
(
[description] => B
)
[COLUMNS] => Array
(
[description] => C
[id] => 8
)
)
[1] => Array
(
[ACCOUNTS] => Array
(
[description] => A1
)
[HEADERS] => Array
(
[description] => B1
)
[COLUMNS] => Array
(
[description] => C1
[id] => 9
)
)
)
The array I want to end up is the following associative array:
Array
(
[A] => Array
(
[B] => Array
(
[C] => 8
)
)
[A1] => Array
(
[B1] => Array
(
[C1] => 9
)
)
)
I can't recreate all (3) levels of the array above.
Do you mean like:
$newarray = array($first['ACCOUNTS']['description'] => array($first['HEADERS']['description'] => array($first['COLUMNS']['description'] => $first['COLUMNS']['id'])));
So if you run the following it gives what you want:
$first = array(
'ACCOUNTS' => array('description' => 'A'),
'HEADERS' => array('description' => 'B'),
'COLUMNS' => array('description' => 'C', 'id' => '8'));
echo "<pre>";
print_r($first);
$newarray = array($first['ACCOUNTS']['description'] =>
array($first['HEADERS']['description'] =>
array($first['COLUMNS']['description'] =>
$first['COLUMNS']['id'])));
print_r($newarray);
You then end up with:
Array
(
[ACCOUNTS] => Array
(
[description] => A
)
[HEADERS] => Array
(
[description] => B
)
[COLUMNS] => Array
(
[description] => C
[id] => 8
)
)
Array
(
[A] => Array
(
[B] => Array
(
[C] => 8
)
)
)