I've got an array like this
Array
(
[0] => Array
(
[0] => Array
(
[szam] => 8
[index] => 0
)
)
[1] => Array
(
[0] => Array
(
[szam] => 1
[index] => 0
)
[1] => Array
(
[szam] => 7
[index] => 1
)
)
I thought that my last cmp will work fine
function maxSzerintCsokkeno($item1,$item2)
{
if ($item1['szam'] == $item2['szam']) return 0;
return ($item1['szam'] < $item2['szam']) ? 1 : -1;
}
with foreach
foreach ($tomb as $kulcs => $adat) usort($adat,"maxSzerintCsokkeno");
but it dosen't do anything, advise?
foreach ($tomb as $kulcs => $adat) usort($adat,"maxSzerintCsokkeno");
This only sorts the subarray array $adat. And this only exists temporarily until foreach loops over the next one. The lazy option here would be to use a reference:
foreach ($tomb as & $adat) usort($adat,"maxSzerintCsokkeno");
Notice the &. This way the modification on $adat will be applied directly in the parent array.
You're sorting a temporary variable, meaning the changes are not applied. The following should work for you:
for($i = 0, $length = count($tomb); $i < $length; $i++)
{
usort($tomb[$i], "maxSzerintCsokkeno");
}
When iterating through the foreach loop, the key and value variables ($kulcs and $adat in your code) are copies of the actual values in the array. Like Tim Cooper said, you are actually sorting a copy of the original value.
You can also pass the value by reference in your foreach loop. This means that you will be modifying the original value:
foreach ($tomb as $kulcs => &$adat) usort($adat,"maxSzerintCsokkeno");
Related
I have an array coming from a mysql database. So it is structured this way (just the first two entry):
Array
(
[0] => Array
(
[id_cre] => CD000000001
[0] => CD000000001
[id_az] => AZ000000001
[1] => AZ000000001
)
[1] => Array
(
[id_cre] => CD000000002
[0] => CD000000002
[id_az] =>
[1] =>
)
)
I would like to count how many entries in the array have [id_az] =>''.
If I do:
count($creds)
I get 2 (the number of items in the array).
I'd prefer to reuse this array (the query runs already for another report), instead of doing a new query with the WHERE clause to subselect WHERE id_az = ''.
Any hint(s)?
This should work for you:
Just get the column id_az with array_column() and count() the array then, e.g.
echo count(array_column($creds, "id_az"));
Why not use a good old foreach loop?
$count = 0;
foreach($data as $row)
{
$count += empty($row['id_az']) ? 0 : 1;
}
or alternativly an array_map with a anonymous function
$count = 0;
array_map(function($row) use (&$count) { $count += empty($row['id_az']) ? 0 : 1; }, $data);
But this is PHP >5.3. Callbacks won't work, since you won't have access to a variable to store your count in.
My Array shown as follows:
Array
(
[0] => Array
(
[amount_id] => 1
[enquiry_id] => 1
[project_id] => 1
)
[1] => Array
(
[amount_id] => 4
[enquiry_id] => 4
[project_id] => 4
)
[2] => Array
(
[amount_id] => 5
[enquiry_id] => 5
[project_id] => 5
)
)
This Array can be increase. How can i get value of each 'amount_id' from this array? What function should i use? Can for each function will work?
Just try with:
$input = array( /* your data */ );
$output = array();
foreach ($input as $data) {
$output[] = $data['amount_id'];
}
You can use a one-liner array_walk() to print those..
array_walk($arr,function($v){ echo $v['amount_id']."<br>";});
Working Demo
Do like this in array_map or Use array_column for The version PHP 5.5 or greater
$outputarr= array_map(function($item){ return $item['amount_id'];},$yourarr);
print_r($outputarr);
Try the following, this is accessing the specific array - easier to debug later on and better speed:
$num=count($your_array);
for($i="0"; $i<$num; $i++)
{
echo $your_array[$i]['amount_id'];
}
you could loop until $i < count($your_array) but it means that the count will run in every loop, for high performance sites I wouldn't do it.
you could access a specific element in a 2D array by $your_array[$the_index]['amount_id'] or other index.
I've got an multi-dimensional array at the moment and want to remove the second-level of arrays and have the value of that second level as the new index value on the parent array. My current array is:
Array ( [0] => Array ( [connectee] => 1 ) [1] => Array ( [connectee] => 6 ) )
And want from that:
Array ( [0] => 1, [1] => 6 )
I was poking around the usort function but couldn't get it to work (where $current_connections is my array as above:
function cmp($a, $b) {
return strcmp($a["connectee"], $b["connectee"]);
}
$current_connections = usort($current_connections, "cmp");
The key doesn't need to be maintained (should be destroyed in the process).
foreach ($array as &$value) {
$value = $value['connectee'];
}
Note: Please note that the question statement is very confusing and contradicting, but this answer is based upon your statement for expected output
Array ( [0] => 1, [1] => 6 )
You could do
<?php
$values=array();
$values[0]=array("connectee"=>1);
$values[1]=array("connectee"=>6);
foreach($values as $index=>$value)
{
$values[$index]=$value["connectee"];
}
print_r($values);
?>
In my application I am using array_diff function as -
$aDeleteCountryCodes = array_diff($aCurrentCountryCodes, $aNewCountryCodes);
Now what happens is, the resultant array, $aDeleteCountryCodes, some times comes as
Array
(
[2] => 213
)
and some times
Array
(
[2] => 213
[3] => 355
)
which messes my for loop that I use to delete records from database. For loop is like this-
for ($i=0; $i <= count($aDeleteCountryCodes); $++)
{
// Delete record $aDeleteCountryCodes[$i]
}
what I want is the array to come as -
Array
(
[0] => 213
)
Array
(
[0] => 213
[1] => 355
)
so that the looping becomes easier. I hope I made it clear. How can I do this ?
Use array_values.
Use foreach instead of "manual for loops."
Rather than reset the keys, it's preferable to just iterate over the existing keys:
foreach ($aDeleteCountryCodes as $key => $value) {
// delete goes here.
}
Use array_values(array_diff($aCurrentCountryCodes, $aNewCountryCodes));
You can just get the values out into a new array:
$aDeleteCountryCodes = array_values($aDeleteCountryCodes) //Keys resetted.
I have an array that looks like the one below. I'm trying to group and count them, but haven't been able to get it to work.
The original $result array looks like this:
Array
(
[sku] => Array
(
[0] => 344
[1] => 344
[2] => 164
)
[cpk] => Array
(
[0] => d456
[1] => d456
)
)
I'm trying to take this and create a new array:
$item[sku][344] = 2;
$item[sku][164] = 1;
$item[cpk][d456] = 1;
I've gone through various iterations of in_array statements inside for loops, but still haven't been able to get it working. Can anyone help?
I wouldn't use in_array() personally here.
This just loops through creating the array as it goes.
It seems to work without needing to first set the index as 0.
$newArray = array();
foreach($result as $key => $group) {
foreach($group as $member) {
$newArray[$key][$member]++;
}
}