I've have a JSON file that I am parsing using json_decode() which outputs the contents of the JSON files as an array, This is a sample of the data output:
array(1) {
["petition"]=>
array(2) {
["postal_districts"]=>
array(2257) {
["DH4"]=>
int(12)
["BT5"]=>
int(14)
["WA9"]=>
int(72)
["EH17"]=>
int(5)
}
}
}
I am wanting to add up all the int() values from under "postal_districts" but at the moment I'm at a loss as to how I can achieve this.
Any help is greatly appreciated.
If they are all ints, you can try:
$sum = array_sum($arr['petition']['postal_districts']);
(see if array_sum helps)
If not, filter them first:
$ints = array_filter($arr['petition']['postal_districts'], 'is_int');
$sum = array_sum($ints);
$sum = 0;
foreach($array['petition']['postal_districts'] as $val)
$sum += $val;
echo $sum;
Do you mean it?
Related
If I have this array in PHP:
array(3) {
[0]=>
string(5) "first"
[1]=>
string(6) "second"
[2]=>
string(5) "third"
}
How can I convert this single array into a multidimensional array? What is the quickest way? So I can access this array like:
$array["first"]["second"]...
I want to be able to then set a value to this index like:
$array["first"]["second"]["third"] = "example";
I thought that I need a for loop or a recursive function but I have no idea how to start.
It wasn't quite as simple as I had imagined to begin with. The key to it was doing the process backwards - starting with the last array and then wrapping it in more arrays until you get back to the top level.
$array = array("first", "second", "third");
$newArr = array();
//loop backwards from the last element
for ($i = count($array)-1; $i >= 0 ; $i--)
{
$arr = array();
if ($i == count($array)-1) {
$val = "example";
$arr[$array[$i]] = $val;
}
else {
$arr[$array[$i]] = $newArr;
}
$newArr = $arr;
}
var_dump($newArr);
echo "-------".PHP_EOL;
echo $newArr["first"]["second"]["third"];
Demo: http://sandbox.onlinephpfunctions.com/code/0d7fa30fde7126160fbcc0e80e5727f17b19e39f
How to group repeated array values in an array using PHP?
I have an array like this
array[0]=203,
array[1]=204,
array[2]=204,
array[3]=203,
array[4]=203,
array[5]=205
I need results like
[203]=1,
[204]=2,
[203]=2,
[205]=1
i want the count of continuously repeating array values
One option to your expected output is to create a indexed array with the associative array below it.
This will create this kind of array:
array(4) {
[0]=>
array(1) {
[203]=>
int(1)
}
[1]=>
array(1) {
[204]=>
int(2)
}
[2]=>
array(1) {
[203]=>
int(2)
}
[3]=>
array(1) {
[205]=>
int(1)
}
}
This is not what you wanted but it is what is possible.
The code loops and keeps track of what the previous value is, if it's the same it will count up the value, else create a new indexed and associative array with value 1.
$array =[203,204,204,203,203,205];
$i=-1;
$prev = null;
$new=[];
foreach($array as $val){
if($val != $prev){
$i++;
}
if(!isset($new[$i][$val])){
$new[$i][$val] =1;
}else{
$new[$i][$val]++;
}
$prev = $val;
}
var_dump($new);
https://3v4l.org/W2adN
array[0]=203;
array[1]=204;
array[2]=204;
array[3]=203;
array[4]=203;
array[5]=205;
$new_array = array();
foreach ($array as $key => $value) {
if(empty($new_array[$value])){
$new_array[$value] = 1;
}else{
$new_array[$value]++;
}
}
/*Now in the array $new_array you have the count of continuously repeating array values*/
This can be done in one line of code using array_count_value function in php.
$arr[0]=203;
$arr[1]=204;
$arr[2]=204;
$arr[3]=203;
$arr[4]=203;
$arr[5]=205;
$result = array_count_values( $arr );
var_dump( $result );
output
array (size=3)
203 => int 3
204 => int 2
205 => int 1
I have faced with the problem, I need to normalize/sort in natural order values in array after some item has been removed.
Consider following example. Initial array
{ [313]=> int(2) [303]=> int(1) [295]=> int(3) [290]=> int(4) }
Sorted array
{ [303]=> int(1) [313]=> int(2) [295]=> int(3) [290]=> int(4) }
Consider case when we are removing first item, array should look like this now
{ [313]=> int(1) [295]=> int(2) [290]=> int(3) }
In case of item inside the array range for example 295 (3) it should be
{ [303]=> int(1) [313]=> int(2) [290]=> int(3) }
I hope you get an idea.
But my function doesn't do this correctly.
I've implemented part of this sorting, here is the code, but maybe there are other ways to do this easier ?
const MIN_VALUE = 1;
public function sort_items(&$items_map)
{
if (!empty($items_map)) {
asort($items_map);
var_dump($items_map);
$first_item = reset($items_map);
if ($first_item > self::MIN_VALUE) {
$normalize_delta = $first_item - self::MIN_VALUE;
$prev_item_id = null;
foreach ($items_map as $id => $part) {
$items_map[$id] = $part - $normalize_delta;
if (!empty($prev_item_id)) {
$difference = $items_map[$id] - $items_map[$prev_item_id];
if ($difference > 1) {
$items_map[$id] = $items_map[$id] - ($difference - 1);
}
}
$prev_item_id = $id;
}
}
}
return $items_map;
}
I would be grateful for any help.
Thanks
UPDATE
To clarify.
I want items not to be just sorted in the correct order, but to be in natural order, for example
Sequence 1,3,5,6,7,9 should be transformed into 1,2,3,4,5,6 but keeping keys the same.
2,3,7,9 => 1,2,3,4
Please see my example above with real word case.
If you need to use a custom sort algorithm, use usort to do so. From PhP the documentation :
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
So you just need to provide those integers if you are in case an item is "greater" or "lower", and usort will do the job for you.
In your case, it could lead to this function :
<?php
function sort_items_map($a, $b)
{
$value = 0;
if( $a < $b )
{
$value = -1;
}
else if( $a > $b )
{
$value = 1;
}
else if( $a == $b )
{
$value = 0;
}
return $value;
}
$items_map = [1, 3, 1, 7]; // or fill it with your own values
usort($items_map, "sort_items_map");
?>
I need to get ID´s from url:
http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID=1&ID=5&ID=24&ID=32
If i use $_GET['ID'] a still get only last ID value. I need to get all of them to array, or select.
Can anybody help me?
Use array syntax:
http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID[]=1&ID[]=5&ID[]=24&ID[]=32
var_dump($_GET['ID']);
array(4) {
[0]=>
int(1)
[1]=>
int(5)
[2]=>
int(24)
[3]=>
int(32)
}
}
echo $_GET['ID'][2]; // 24
The format in the URL is wrong. The second "ID" is overwriting the first "ID".. use an array:
http://www.example.org/?id[]=1&id[]=2&id[]=3
In PHP:
echo $_GET['id'][0]; // 1
echo $_GET['id'][1]; // 2
echo $_GET['id'][2]; // 3
To get this you need to make ID as array and pass it in the URL
http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID[]=1&ID[]=5&ID[]=24&ID[]=32
and this can be manipulated at the backend like this
$urls = $_GET['ID'];
foreach($urls as $url){
echo $url;
}
OR
An alternative would be to pass json encoded arrays
http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID=[1,2,24,32]
which can be used as
$myarr = json_decode($_GET['ID']); // array(1,2,24,32)
I recommend you to also see for this here.
http_build_query()
it's wrong but if you really want to do this
<?php
function getIds($string){
$string = preg_match_all("/[ID]+[=]+[0-9]/i", $string, $matches);
$ids = [];
foreach($matches[0] as $match)
{
$c = explode("=", $match);
$ids [] = $c[1];
}
return $ids;
}
// you can change this with $_SERVER['QUERY_STRING']
$url = "http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID=1&ID=5&ID=24&ID=32";
$ids = getIds($url);
var_dump($ids);
Its my first time working with multidimensional arrays in php. I need to change the second number in each sub array.
What I want is to check if the Id in the array matches the Id from the database. When the two match I want to change the 2nd entry in the sub array by adding a number to it. If the Id from the query does not match anything in the list I want a new sub array to be pushed to the end of the array with the values of Id and points_description.
Also, if its helpful, my program right now does find the matches. The only thing is, it does not update the 2D array.
$array = array(array());
while ($row_description = mysqli_fetch_array($query_description)) {
$check = 1;
$is_match = 0;
foreach ($array as $i) {
foreach ($i as $value) {
if ($check == 1) {
if ($row_description['Id'] == $value) {
//$array[$i] += $points_description;
$is_match = 1;
}
}
$check++;
$check %= 2; //toggle between check and points
}
}
if ($is_match == 0) {
array_push($array, array($row_description['Id'], $points_description));
}
}
I feel like Im doing this so wrong. I just want to go through my 2D array and change every second value. The expected output should be a print out of all the Ids and their corresponding point value
I hope this is helpful enough.
Example: $row_description['Id'] = 2 and $array = array(array(2,1), array(5,1) , array(6,1))
output should be $array = array(array(2,4), array(5,1) , array(6,1))
if $row_description['Id'] = 3 and $array = array(array(2,1), array(5,1) , array(6,1))
output should be $array = array(array(2,4), array(5,1) , array(6,1),array(3,3))
By default PHP will copy an array when you use it in a foreach.
To prevent PHP from creating this copy you need to use to reference the value with &
Simple example :
<?php
$arrFoo = [1, 2, 3, 4, 5,];
$arrBar = [3, 6, 9,];
Default PHP behavior : Make a copy
foreach($arrFoo as $value_foo) {
foreach($arrBar as $value_bar) {
$value_foo *= $value_bar;
}
}
var_dump($arrFoo);
/* Output :
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
}
*/
ByReference : Don't create the copy :
foreach($arrFoo as &$value_foo) {
foreach($arrBar as $value_bar) {
$value_foo *= $value_bar;
}
}
var_dump($arrFoo);
/* Output :
array(5) {
[0]=>
int(162)
[1]=>
int(324)
[2]=>
int(486)
[3]=>
int(648)
[4]=>
&int(810)
}
*/