I'm trying to teach myself how multidimensional arrays work and how I can compare and manipulate them in php I have created two arrays with the same scheme but with one different value in each.
first array
$datastuff1 = array( array( Ttitle => "rose",
Price => 1.25,
Number => 15
),
array( Ttitle => "daisy",
Price => 0.75,
Number => 25
),
array( Ttitle => "lilly",
Price => 1.75,
Number => 3
),
array( Ttitle => "orchid",
Price => 1.15,
Number => 7
)
);
second array
$datastuff2 = array( array( Title => "rose",
Price => 1.25,
Number => 15
),
array( Title => "daisy",
Price => 0.75,
Number => 25
),
array( Title => "nettle",
Price => 2.75,
Number => 33
),
array( Title => "orchid",
Price => 1.15,
Number => 7
)
);
I now want to loop through both arrays and foreach item that matches (using the title as a key) in both arrays add to a new matching array and for each item that doesn't match in both arrays add to my not matching array
heres my code
$matchingarray = array();
$notmatchingarray = array();
foreach($datastuff1 as $data1){
foreach($datastuff2 as $data2){
if($data2['Title']== $data1['Ttitle'])
{
$matchingarray[] = $data1;
}
else {
$notmatchingarray[] = $data1;
}
}
}
but when I output the contents of the arrays using
echo "<pre>";
print_r($notmatchingarray);
echo "</pre>";
I get the output
Array
(
[0] => Array
(
[Ttitle] => rose
[Price] => 1.25
[Number] => 15
)
[1] => Array
(
[Ttitle] => rose
[Price] => 1.25
[Number] => 15
)
[2] => Array
(
[Ttitle] => rose
[Price] => 1.25
[Number] => 15
)
[3] => Array
(
[Ttitle] => daisy
[Price] => 0.75
[Number] => 25
)
[4] => Array
(
[Ttitle] => daisy
[Price] => 0.75
[Number] => 25
)
[5] => Array
(
[Ttitle] => daisy
[Price] => 0.75
[Number] => 25
)
[6] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[7] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[8] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[9] => Array
(
[Ttitle] => lilly
[Price] => 1.75
[Number] => 3
)
[10] => Array
(
[Ttitle] => orchid
[Price] => 1.15
[Number] => 7
)
[11] => Array
(
[Ttitle] => orchid
[Price] => 1.15
[Number] => 7
)
[12] => Array
(
[Ttitle] => orchid
[Price] => 1.15
[Number] => 7
)
)
so to me it seems as though its looping round three times (the amount of items that match) and each time putting the matching items in the array.
What I want is all the items that DON'T match (using the Title as a key) in the non-matching array and the ones that DO in the matching array. I'm missing something painstakingly obvious I suppose.
Any help would be grand
regard mike
I couldn't simply copy/paste your array definitions so I didn't test, but you need to check for equality and if found then break out of the inner loop. Also, after the inner loop, check if it was added to $matchingarray and if not, add to $notmatchingarray:
foreach($datastuff1 as $key => $data1){
foreach($datastuff2 as $data2){
//match add to $matchingarray
if($data2['Title'] == $data1['Ttitle']) {
$matchingarray[$key] = $data1; //use $key so we can check later
break; //we have a match so why keep looping?
}
}
//if no match add to $notmatchingarray
if(!isset($matchingarray[$key])) { //we used $key so we can check for it
$notmatchingarray[$key] = $data1; //don't need $key here but oh well
}
}
Alternate way that may be easier to follow:
foreach($datastuff1 as $key => $data1){
$match = false; //no match
foreach($datastuff2 as $data2) {
//match add to $matchingarray
if($data2['Title'] == $data1['Ttitle']) {
$matchingarray[] = $data1;
$match = true; //match
break; //we have a match so why keep looping?
}
}
//if no match add to $notmatchingarray
if(!$match) {
$notmatchingarray[] = $data1;
}
}
Related
Actual I get
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
[1] => Array
(
[name] => person2
[value] => 5
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person4
[value] => 10
)
)
Actually i need
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
//here i want new row index 1 array
[1] => Array
(
[name] => total
[value] => 11 //this value is the sum of index 1
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person2
[value] => 5
)
// here i want add new line index 4 array
[4] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 2 and 3
)
[5] => Array
(
[name] => person4
[value] => 10
)
// here i want add new line index 6 array
[6] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 5
)
)
may this help you i have tried some step making grouping but sum is not correct but code may help you
$a = Array
(
0 => Array
(
'name' => 'person1',
'value' => 11,
),
1 => Array
(
'name' => 'person2',
'value' => 5,
),
2 => Array
(
'name' => 'person2',
'value' => 5,
),
3 => Array
(
'name' => 'person4',
'value' => 10,
)
);
foreach($a as $c){
$d[]=$c['name'];
$e[]=$c['value'];
}
//print_r($d);
$group = array();
foreach($d as $key=>$val){
$group[$val][] = $e[$key];
}
// this loop for check the max number and count total price
$data = array();
$total = 0;
foreach($group as $key=>$val){
for($i=0;$i<count($val);$i++){
$data[]['name'] = $key;
$data[]['value'] = $val[$i];
$suma[] = $val[$i];
}
$sunmb =array_sum($suma);
$data[]['name'] = 'total';
$data[]['value'] = $sunmb;
}
print_r($data);
You can check code run here https://wtools.io/php-sandbox/b1LD
Working with a single array in PHP, I'd like to merge like with like and am able to do so but I only get 1 return where there should be 2.
Below is the original array, and the new.
id LIKE 'fghij' OR id LIKE 'abcde'
$orig = Array
(
[0] => Array
(
[id] => abcde
[field2] => height
[value2] => 40 ft.
)
[1] => Array
(
[id] => abcde
[field3] => width
[value3] => 10-15 ft.
)
[2] => Array
(
[id] => abcde
[field4] => light
[value4] => Full - Partial
)
[3] => Array
(
[id] => abcde
[field6] => space
[value6] => 5-6 ft.
)
[4] => Array
(
[id] => fghij
[field2] => height
[value2] => 4-8 ft.
)
[5] => Array
(
[id] => fghij
[field3] => width
[value3] => 3-4 ft.
)
[6] => Array
(
[id] => fghij
[field4] => light
[value4] => Full - Partial
)
[7] => Array
(
[id] => fghij
[field5] => season
[value5] => Spring
)
[8] => Array
(
[id] => fghij
[field6] => space
[value6] => 4 ft.
)
[9] => Array
(
[id] => fghij
[field19] => restricted
[value19] => CA, TX, LA, FL, AZ
)
[10] => Array
(
[id] => abcde
[field19] => restricted
[value19] => AZ
)
)
$new = Array
(
[id] => abcde
[field2] => height
[value2] => 4-8 ft.
[field3] => width
[value3] => 3-4 ft.
[field4] => light
[value4] => Full - Partial
[field6] => space
[value6] => 4 ft.
[field5] => season
[value5] => Spring
[field19] => restricted
[value19] => AZ
)
$new is so close to being correct but it ignores everything relating to fghij, returning a single flattened array. I'd like it to return both id's with their merged values.
This is where I'm at code wise:
$new_new = [];
foreach($orig as $key => $value){
if(is_array($value)){
$new_new = array_merge($new_new, $value);
}
}
You need to accumulate the entries for each unique id.
function mergeByKey($array,$key){
$result = [];
foreach($array as $entry){
$is_new = true;
foreach($result as &$result_entry){
if($result_entry[$key]==$entry[$key]){
$is_new = false;
$result_entry = array_merge($result_entry,$entry);
break;
}
}
if($is_new)
$result[] = $entry;
}
return $result;
}
$new_new = mergeByKey($orig,'id');
May be something like this will work for you? You can add additional checks for $value['id'].
$new = [];
foreach($arr as $key => $value){
if(is_array($value)){
if( !array_key_exists($value['id'],$new) ){
$new[$value['id']] = [];
}
$new[$value['id']] = array_merge($new[$value['id']], $value );
}
}
I'v change mine algorithm and now it's work as I expected.
This question already has answers here:
Group rows in an associative array of associative arrays by column value and preserve the original first level keys
(7 answers)
Closed 7 years ago.
How to manipulate php arrays. I have a $data variable below.
$data = Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
[2] => Array
(
[id] => 3
[capacity] => 900
[category] => students
)
[3] => Array
(
[id] => 4
[capacity] => 300
[category] => students
)
)
Now I want to group the data with same category like below. . I am not really familiar in php. Can somebody out there help me how to achieve this. What function should I used. Thanks
Array
(
[users] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
),
[students] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => students
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => students
)
)
)
Just iterate over the array and add it depending on the content of the category field to a new array:
$new = array();
foreach ($data as $val) {
$new[$val['category']][] = $val;
}
This does what you requested
<?php
$data = array(
0 => array (
"id" => 1,
"capacity" => 900,
"category" => "users"
),
1 => array (
"id" => 2,
"capacity" => 300,
"category" => "users"
),
2 => array (
"id" => 3,
"capacity" => 900,
"category" => "students"
),
3 => array (
"id" => 4,
"capacity" => 300,
"category" => "students"
)
);
$groups = array();
foreach ($data as $i) {
if ($i['category'] === "students") {
$groups['students'][] = $i;
}
else if ($i['category'] === "users") {
$groups['users'][] = $i;
}
}
echo "<pre>", print_r($groups), "</pre>";
Here is a working demo - http://codepad.viper-7.com/G4Br28
I have the following array
$a = array(0 => 'Item',
1 => 'Wattles',
2 => 'Types',
3 => 'Compost',
4=> 'Estimated',
5 => '123',
6 => 'Actual',
7 => '12',
);
That is sorted with the following code.
echo "<pre>";
print_r($a);
$a_len = count($a);
$fnl = array();
$i = 0;
while($i<$a_len){
$fnl[$a[$i]] = $a[++$i];
$i++;
}
print_r($fnl);
It prints correctly
Array
(
[Item] => Wattles
[Types] => Compost
[Estimated Qty] => 123
[Actual Qty] => 12
)
until i add multiple entries.
Array
(
[0] => Item
[1] => Wattles
[2] => Types
[3] => Compost
[4] => Estimated Qty
[5] => 123
[6] => Actual Qty
[7] => 12
[8] => Item
[9] => Silt Fence
[10] => Types
[11] => Straw
[12] => Estimated Qty
[13] => 45
[14] => Actual Qty
[15] => 142
)
I need to make this add items in a multidimensional array.
$items = array
(
array("Wattles","Silt Fence), //items
array("Compost","Straw"), //types
array(123,45), //estimated quantity
array(12,142) //actual quantity
);
There are a few given numbers. There are exactly 4 entries (8 items) before the list repeats itself.
I have been stuck on this portion for hours, and don't know how to get my code working as I want it to.
To get the expected result with string keys you can do:
foreach(array_chunk($a, 2) as $pairs) {
$result[$pairs[0]][] = $pairs[1];
}
Yields:
Array
(
[Item] => Array
(
[0] => Wattles
[1] => Silt Fence
)
[Types] => Array
(
[0] => Compost
[1] => Straw
)
[Estimated] => Array
(
[0] => 123
[1] => 45
)
[Actual] => Array
(
[0] => 12
[1] => 142
)
)
Then if you want it numerically indexed:
$result = array_values($result);
You have your structure for a multidimensional array wrong. You should construct your array like this:
$a = array(
0 => array(
'Item' => 'Wattles',
'Types' => 'Compost',
'Estimated' => 123,
'Actual' => 12
)
);
Then to add to it:
$a[] = array(
'Item' => 'Silt Fence',
'Types' => 'Straw',
'Estimated' => 45,
'Actual' => 142
);
Render it out to see the results which are what I think you are looking for.
print_r($a);
I can post a link if you want to learn how to sort multidimensional arrays by sub-array values if you need.
I have an array looks like this:
Array
(
[0] => Array
(
[variant_name] => Green
[product_id] => 2
[variant_id] => 67
[amount] => 1000
)
[1] => Array
(
[variant_name] => Red
[product_id] => 2
[variant_id] => 68
[amount] => 0
)
)
This expected to be like this:
Array (
[2] => array (
[67] => array (
[variant_id] => Green
[amount] => 1000
(
[68] => array (
[variant_id] => Red
[amount] => 0
(
)
Which is group by product_id and then split into arrays group by variant_id.
How can I do that.
Thank you so much.
foreach($values as $product)
{
$newValues[$product["product_id"]][$product["variant_id"]]=
array(
'product_name'=>$product["variant_name"],
'amount'=>$product["amount"]
);
}
Output
Array
(
[2] => Array
(
[67] => Array
(
[product_name] => Green
[amount] => 1000
)
[68] => Array
(
[product_name] => Red
[amount] => 0
)
)
)
Note You have variant_name in your original array and you are using that as variant_id in your output. You can play with indexes in this code. I just used what seemed to match.
A simple foreach loop should suffice. Of course, you need to build them inside a new array. Consider this example: (In your expected example, I think you were referring to variant_name)
$values = array( array( 'variant_name' => 'Green', 'product_id' => 2, 'variant_id' => 67, 'amount' => 1000, ), array( 'variant_name' => 'Red', 'product_id' => 2, 'variant_id' => 68, 'amount' => 0, ), );
$new_values = array();
foreach($values as $key => $value) {
$new_values[$value['product_id']][$value['variant_id']] = array(
'variant_name' => $value['variant_name'],
'amount' => $value['amount'],
);
}
echo '<pre>';
print_r($new_values);
echo '</pre>';
Sample Output