What does PHP syntax $var1[] = $var2 mean?
Note the [] after $var1 varable.
It means that $var1 is an array, and this syntax means you are inserting $var2 into a new element at the end of the array.
So if your array had 2 elements before, like this:
$var1=( 1 => 3, 2 => 4)
and the value of $var2 was 5, it would not look like this:
$var1=( 1 => 3, 2 => 4, 3 => 5)
It also means that if $var2 was an array itself, you have just created a two dimensional array. Assuming the following:
$var1=( 1 => 3, 2 => 4)
$var2=( 1 => 10, 2=>20)
doing a $var1[]=$var2; wouls result in an array like this:
$var1=( 1 => 3, 2 => 4, 3 => (1 =>10, 2 => 20))
As an aside, if you haven't used multi-dimensional arrays yet, accessing the data is quite simple. If I wanted to use the value 20 from our new array, I could do it in this manner:
echo $var1[3][2];
Note the second set of square brackets - basically you are saying I want to access element two of the array inside element 3 of the $var1 array.
On that note, there is one thing to be aware of. If you are working with multi-dimensional arrays, this syntax can catch you out inside a loop structure of some sort. Lets say you have a two dimensional array where you store some records and want to get more from the database:
$var1 = (
1 => (id =>1, age => 25, posts => 40),
2 => (id =>2, age => 29, posts => 140),
3 => (id =>3, age => 32, posts => 12)
)
A loop like this following:
while($row=someDatabaseRow)
{
$var1[]=$row['id']; // value 4
$var1[]=$row['age']; // value 21
$var1[]=$row['posts']; // value 34
}
will infact insert a new element for every execution, hence your array would end up looking like this:
$var1 = (
1 => (id =>1, age => 25, posts => 40),
2 => (id =>2, age => 29, posts => 140),
3 => (id =>3, age => 32, posts => 12),
4 => 4,
5 => 21,
6 => 34
)
The correct way would be to assemble the array first, then append it to your current array to maintain the strucutre, like this:
while($row=someDatabaseRow)
{
$tempArr= array();
$tempArr[]=$row['id']; // value 4
$tempArr[]=$row['age']; // value 21
$tempArr[]=$row['posts']; // value 34
$var1[]=$tempArr;
}
Now your array would look like you expected, namely:
$var1 = (
1 => (id =>1, age => 25, posts => 40),
2 => (id =>2, age => 29, posts => 140),
3 => (id =>3, age => 32, posts => 12)
4 => (id =>4, age => 21, posts => 34)
)
It means that you're pushing the value of var2 to array var1.
That's shorthand for array_push()
It will make $var1 and array (if it isn't already) and push $var2 in to it.
It basically makes $var1 an array and adding to it's end a value of $var2.
$var1 = NULL;
$var2 = 'abc';
$var1[] = $var2;
$var1 is now an array containing one value: 'abc'
Related
This might be a little confusing, but I am going to explain it as best as I can. Please bear with me.
I have the following arrays:
Array
(
[question1] => 69
[question2] => 36
[question3] => 57
[question4] => 69
[question5] => 58
[question6] => 40
[question7] => 58
)
Array
(
[question1] => 8
[question2] => 6
[question3] => 5
[question4] => 6
[question5] => 7
[question6] => 8
[question7] => 5
)
As you can see the two arrays have identical keys, but different values for each key.
I need to find the keys in the second array that have the same values, so [question1] and [question6] both have a value of 8. And then in the first array I need to add together the values of [question1] and [question6] because they have a like value in the second array. I need to add the first array values together based on matching values in the second array (if that makes any sense)
Ideally, the output would be another array that would look something like this:
Array
(
[5] => 115
[8] => 109
[6] => 105
[7] => 58
)
Where the value of the second array becomes the key and the sum of the added values from the first array is the value.
Now I won't be picky here, so if we can't get it into that exact format then that is okay. I just need to be able to add together the values in the first array based on the similar values in the second array.
I hope this makes sense. If it doesn't please comment and I will do my best to explain further.
The simplest solution is to iterate over the second array. Lookup the key into the first array and if it exists then add the corresponding value from the first array into the result array, indexed by the value from the second array.
Something like this:
$array1 = array(
'question1' => 69,
'question2' => 36,
'question3' => 57,
'question4' => 69,
'question5' => 58,
'question6' => 40,
'question7' => 58,
);
$array2 = array(
'question1' => 8,
'question2' => 6,
'question3' => 5,
'question4' => 6,
'question5' => 7,
'question6' => 8,
'question7' => 5,
);
// Compose the desired result here
$result = array();
// Iterate over the second array; its values become keys in the result array
foreach ($array2 as $key => $val) {
// If this is the first time when this value is reached then a corresponding
// value does not yet exists in the result array; add it
if (! isset($result[$val])) {
$result[$val] = 0;
}
// Lookup the key into the first array
if (isset($array1[$key])) {
// If it exists then add its value to the results
$result[$val] += $array1[$key];
}
}
// That's all
print_r($result);
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 3 months ago.
I am new to php & I'm not sure that this can be done, but I am hoping that someone knows how to. I've collected all the data that I need to submit but now I need to reformat it before I can json_encode to send to the database.
Basically, I have 1 parent array($data) containing 3 sub-arrays ($hours, $WId, $Pid). I need to create associative arrays for each index position & join them together.
Here is my parent array:
$data = array(
'hours' => array(),
'wId' => array(),
'phaseId' => array(),
);
Here is what currently returns when I print_r each of these arrays:
Array ( [hours] => Array ( [0] => 0.5 [1] => 1 [2] => 2 ) )
Array ( [wId] => Array ( [0] => 10, [1] => 9, [2] => 8, ) )
Array ( [phaseId] => Array ( [0] => 20, [1] => 20, [2] => 19, ) )
I need to take these "vertical" arrays & turn them in to "horizontal" arrays per index, using thearray name as the $key & the value for that index as $value. Here is what I need to return.... (Syntax is probably wrong but you can get the idea.)
Array[1] ("hours" => 0.5, "wId" => 10, "phaseId" => 20)
Array[2] ("hours" => 1, "wId" => 9, "phaseId" => 20)
Array[3] ("hours" => 2, "wId" => 8, "phaseId" => 19)
Is there a function that will allow me to do this easily? I saw how to join & merge them together but not sure how to set the array name (hours, etc) as the $key & the value for each index as $value. I need to loop it too because the length of the arrays will vary. (But they will always the same length as each other, so index should still work as what needs to be collected.)
Any suggestions would be greatly appreciated :)
<?php
// set up your output array
$result = array();
// loop through $data, exposing $name for later use
foreach ($data as $name => $array) {
// loop through each named array and set the desired value
// using the current $key and $name
foreach ($array as $key => $value) {
$result[$key][$name] = $value;
}
}
// tada!
print_r($result);
NOTE: In your desired results in your question, you had the parent Array keys starting at 1. This answer assumes that's a typo and you actually wanted them to match the input. If you indeed wanted it to start at one, just change this line in my answer:
$result[$key+1][$name] = $value;
I need to group data in a multidimensional array data which can be related on a single column entry_id.
In addition to the entry_id there are other columns that will also be identical in other related rows (ic, name, and residency). While grouping, these data points can simply be overwritten -- in other words, I don't need to collect multiple copies of the same value within the respective group.
Finally, there will be data points within respective groups that will differ -- these values need to stored as subarrays within the groups so that no data is lost.
If there is only one row's data in a group, the file_no and detail data does not need to be converted to an array. This means that the result array will have variable depth -- some rows will flat and other may be 2 dimensional.
Sample input:
$array = [
[
'entry_id' => 1,
'ic' => 2147483647,
'name' => 'Kořínková Blanka',
'residency' => 'Štětí, Lukešova 354, 411 08',
'file_no' => 'KSUL 77 INS 18898 / 2013',
'detail' => '749371da-725c-4738-8def-2f7167142a6f'
],
[
'entry_id' => 1,
'ic' => 2147483647,
'name' => 'Kořínková Blanka',
'residency' => 'Štětí, Lukešova 354, 411 08',
'file_no' => 'KSUL 77 INS 21218 / 2013',
'detail' => '43b6a718-4647-451d-9c53-50dfee8403ff'
],
[
'entry_id' => 2,
'ic' => 46900217,
'name' => 'ENTEC a.s. "v likvidaci"',
'residency' => 'Staré Město, Brněnská 1916, 686 03',
'file_no' => 'KSBR 28 INS 1232 / 2013',
'detail' => 'e2155a52-c464-4357-b71b-4f4ff75585eb'
],
];
Desired output (based on same 'entry_id' grouping):
Array
(
[0] => Array
(
[entry_id] => 1
[ic] => 2147483647
[name] => Kořínková Blanka
[residency] => Štětí, Lukešova 354, 411 08
[file_no] => Array
(
[0] => KSUL 77 INS 18898 / 2013
[1] => KSUL 77 INS 21218 / 2013
)
[detail] => Array
(
[0] => A749371da-725c-4738-8def-2f7167142a6f
[1] => 43b6a718-4647-451d-9c53-50dfee8403ff
)
)
[1] => Array
(
[entry_id] => 2
[ic] => 46900217
[name] => ENTEC a.s. "v likvidaci"
[residency] => Staré Město, Brněnská 1916, 686 03
[file_no] => KSBR 28 INS 1232 / 2013
[detail] => e2155a52-c464-4357-b71b-4f4ff75585eb
)
)
Your issue can be resolved with one functional block, using array_reduce() and array_merge() principles:
$mergeId = 'entry_id';
$data = array_reduce($data, function($c, $x) use ($mergeId)
{
$c[$x[$mergeId]] = isset($c[$x[$mergeId]])
?array_combine(
$z=array_keys($c[$x[$mergeId]]),
array_map(function($y) use ($x, $c, $mergeId)
{
return in_array($x[$y], (array)$c[$x[$mergeId]][$y])
?$c[$x[$mergeId]][$y]
:array_merge((array)$c[$x[$mergeId]][$y], [$x[$y]]);
}, $z)
)
:$x;
return $c;
}, []);
you may want to apply array_values() if you need to re-index result set (so keys would be consecutive, starting from 0). Check the fiddle.
Using a nested loop to iterate a single array for potential matches is an indirect "brute force" technique -- there are some fringe cases where this can be tolerated, but in this case the more performant and professional approach will be to assign temporary first level keys as you iterate each row. The new associative arrays permit simple/swift searching because of the way that PHP handles arrays/keys.
When an entry_id value is encountered for the first time, simply save the whole row. When an entry_id value is encountered after the first time, the scalar-typed file_no and detail elements need to be converted to array-type . This can be done manually, but array_merge_recursive() offers this "magic" natively.
Call array_values() after finished iterating if you do not want to keep the first level grouping keys.
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['entry_id']])) {
$result[$row['entry_id']] = $row;
} else {
$result[$row['entry_id']] = array_merge_recursive(
$result[$row['entry_id']],
['file_no' => $row['file_no'], 'detail' => $row['detail']]
);
}
}
var_export(array_values($result));
Manually re-casting elements within groups (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['entry_id']])) {
$result[$row['entry_id']] = $row;
} else {
$result[$row['entry_id']]['file_no'] = (array) $result[$row['entry_id']]['file_no']; // re-cast as single-element array
$result[$row['entry_id']]['detail'] = (array) $result[$row['entry_id']]['detail']; // re-cast as single-element array
$result[$row['entry_id']]['file_no'][] = $row['file_no']; // push new element into subarray
$result[$row['entry_id']]['detail'][] = $row['detail']; // push new element into subarray
}
}
var_export(array_values($result));
When i set a global array as this
$items[$users[$clientID]['room']] = array("seat" => $seat, "item_id" => $q[1], "room" => $users[$clientID]['room']);
it is
$items[4] = array("seat" => 20, "item_id" => 10, "room" => 4);
but when i do a count it telling the length of $items[4] is 3? It should only count 1 because i only have "1" item.
for ( $i=0;$i<count($items[$users[$clientID]['room']]);$i++):
//something
endfor;
print_r($items[$users[$clientID]['room']]);
outputs:
Array
(
[seat] => 43
[item_id] => 46
[room] => 5
)
COUNT 3
What am i doing wrong?
You have defined $items[$users[$clientID]['room']] as one array: but an array that comprises three items - seat, item_id and room - and it is those individual array items that you are counting.
$items[$users[$clientID]['room']] is an array and count() returns no of element in an array, if the value to count is not an array it will return 1. See this link for more information about count function.
Count Function - PHP
I want to store some data in (I guess a semi-2, semi-3d array) in PHP (5.3)
What I need to do is store data about each floor like this:
Floor Num of Spots Handicap Motorcyle Other
1 100 array(15,16,17) array (47,62) array (99,100)
2 100 array(15,16,17) array (47,62) array (99,100)
and on
The problem is, is if the Handicap+Motorcyle+Other were ints, I could just store the data in a 2d array. However, they aren't. So I was thinking I could make something almost like a 3D array, with the first two columns only being in 2D.
The other thought I had was making a 2D array and for columns 3,4, and 5 instead of saving as
array(15,16)
//save like
1516
And then split at two digits (1 digit array numbers would be prefaced with a 0). However, I am wondering about the limit of the length of a string, because if I decide to move to a 3 digit length number in the array, like array(100, 104), and I need to store alot of numbers, I am thinking I am going to quickly exceed the max.
Edit 1
I like Omar's answer alot, but I'm not sure as to how to pull the data out.
While you could store them as ?D array, there is another approach you might want to consider :
$stuff = array (
'floor1' =>
array (
'NumSpots' => 100,
'handicap' => array (15,16,17),
'motorcycle' => array (47, 62),
'other' => array (99, 100),
),
),
'floor2' =>
'NumSpots' => 100,
'handicap' => array (15,16,17),
'motorcycle' => array (47, 62),
'other' => array (99, 100),
),
)
)
That way, you can access things through mroe meaningful names like
$stuff['floor1']['motorcycle'][2]
In php you can have named keys for your arrays. Each element in your array can have different types so you could have
$floors = array(
1 => array(
'num_spots' => 100,
'handicap' => array(15,16,17)
),
2 => array(
'num_spots' => 100,
'handicap' => array(15,16,17),
'motorcycle' => array (47,62)
)
);
etc...
You can do exactly that. Example:
$a = array();
$a[1] = array(
'spots' => 100,
'handicap' => array(5,3,5)
);
$a[2] = array(
'spots' => 50,
'handicap' => array(1,3,20)
);
var_dump($a);
Output:
array
1 =>
array
'spots' => int 100
'handicap' =>
array
0 => int 5
1 => int 3
2 => int 5
2 =>
array
'spots' => int 50
'handicap' =>
array
0 => int 1
1 => int 3
2 => int 20
You can use the array indices for the floor number or have a separate key/value for that.
Something like...
$floors = array();
$floors[1] => array(
Spaces => 100,
Handicapped => array(15, 16, 17),
Motorcycle => array(47, 62),
Other => array (1, 2, 3, ..., n)
)
You can then retrieve the values as ...
$Floor1Spaces = $floors[1]['Spaces']; //An integer
$Floor1HAndicapped = $floors[1]['Handicapped']; //A 1-dimensional array of integer