array(7) {
[0]=> array(2) { ["id"]=> string(1) "9" ["roi"]=> float(0) }
[1]=> array(2) { ["id"]=> string(1) "1" ["roi"]=> float(0) }
[2]=> array(2) { ["id"]=> string(2) "10" ["roi"]=> float(0) }
[3]=> array(2) { ["id"]=> string(2) "14" ["roi"]=> float(0) }
[4]=> array(2) { ["id"]=> string(1) "4" ["roi"]=> float(0) }
[5]=> array(2) { ["id"]=> string(1) "5" ["roi"]=> float(141) }
[6]=> array(2) { ["id"]=> string(1) "6" ["roi"]=> float(2600) }
}
I would just like to reverse this, so id 6 (with roi of 2600) comes first in the array etc.
How can I do this? array_reverse() and rsort() does not work in this case
http://php.net/manual/en/function.array-reverse.php:
$newArray = array_reverse($theArray, true);
The important part is the true parameter, which preserves the keys.
Not convinced? You can see it in action on this codepad exampole.
foreach($array as $arr){
array_unshift($array, $arr);
array_pop($array);
}
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$res4 = array();
$count = count($res);
for($i=$count-1;$i>=0;$i--){
$res4[$i] =$res[$i];
}
print_r($res4);
You can use an usort() function, like so
$arr = array('......'); // your array
usort($arr, "my_reverse_array");
function my_reverse_array($a, $b) {
if($a['roi'] == $b['roi'])
{
return 0;
}
return ($a['roi'] < $b['roi']) ? -1 : 1;
}
This will make sure the item with the highest roi is first in the array.
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$count = count($res);
for ($i=0, $j=$count-1; $i<=floor($count/2); $i++, $j--) {
$temp = $res[$j];
$res[$j] = $res[$i];
$res[$i] = $temp;
}
echo '<pre>';
print_r($res);
echo '</pre>';
It's easy. May be use usort function of php like:
usort($arr, function($a, $b) {
return $b['roi'] - $a['roi'];
});
Just swap position $a and $b that is correct.
Related
So I have an array stored in a variable called $data that looks like this:
["data"]=>
["rows"]=>
array(30) {
[0]=>
array(3) {
[0]=>
string(10) "2016-08-15"
[1]=>
int(0)
[2]=>
int(0)
}
[1]=>
array(3) {
[0]=>
string(10) "2016-08-16"
[1]=>
int(0)
[2]=>
int(0)
}
[2]=>
array(3) {
[0]=>
string(10) "2016-08-17"
[1]=>
int(0)
[2]=>
int(0)
}
[3]=>
array(3) {
[0]=>
string(10) "2016-08-18"
[1]=>
int(0)
[2]=>
int(0)
}
By using the following function I take the values from the array:
$subscribersGained = [];
foreach ($data->data->rows as $obj) {
if (isset($obj[1])) {
// add the element to the beginning of the array
array_unshift($subscribersGained, $obj[1]);
}
if(count($subscribersGained) >= 30) {
break;
}
}
$gained = array_map( create_function('$value', 'return (int)$value;'),
$subscribersGained);
echo json_encode($gained);
And store them into a json_string that looks like this:
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
What I need to do is that I have to make the non 0 number be negative. So in this case I want to have -1 not 1. Any ideas how to make that happen? Thank you for your time!
Well, the most primitive way to do it that I figure out it is:
foreach($array as $key => $number) {
$array[$key] = 0 - $number;
}
$gained = array(0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$gained = array_map(function($el) { return 0-$el; }, $gained);
print_r($gained);
//from #Dmytrechko`s code
I am trying to do a custom WP_query loop and put the results into an array of associate arrays.. It is not working too well.. I realize the array_push overwrites any arrays that have the same indexes so I have the index increment +1 in the loop so they are not identical..however it still is not working.. My results show correct only on the first index (zero).. Here is my code:
<?php
$permlink='permalink';
$excerpt='exerpt';
$title='title';
$id='id';
$finalarray=array();
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
$count=0;
foreach($finalarray as $item){
echo $count.':'.'<br>';
echo $item[$count]['title'.$count];
echo $item[$count]['id'.$count];
echo $item[$count]['permalink'.$count];
$count++;
}
var_dump($finalarray);
?>
Any my results show :
0:
title0id0permalink01:
2:
3:
4:
5:
6:
7:
8:
9:
10:
array(11) { [0]=> array(1) { [0]=> array(4) { ["id0"]=> string(3) "id0" ["title0"]=> string(6) "title0" ["excerpt0"]=> string(7) "exerpt0" ["permalink0"]=> string(10) "permalink0" } } [1]=> array(1) { [0]=> array(4) { ["id1"]=> string(3) "id1" ["title1"]=> string(6) "title1" ["excerpt1"]=> string(7) "exerpt1" ["permalink1"]=> string(10) "permalink1" } } [2]=> array(1) { [0]=> array(4) { ["id2"]=> string(3) "id2" ["title2"]=> string(6) "title2" ["excerpt2"]=> string(7) "exerpt2" ["permalink2"]=> string(10) "permalink2" } } [3]=> array(1) { [0]=> array(4) { ["id3"]=> string(3) "id3" ["title3"]=> string(6) "title3" ["excerpt3"]=> string(7) "exerpt3" ["permalink3"]=> string(10) "permalink3" } } [4]=> array(1) { [0]=> array(4) { ["id4"]=> string(3) "id4" ["title4"]=> string(6) "title4" ["excerpt4"]=> string(7) "exerpt4" ["permalink4"]=> string(10) "permalink4" } } [5]=> array(1) { [0]=> array(4) { ["id5"]=> string(3) "id5" ["title5"]=> string(6) "title5" ["excerpt5"]=> string(7) "exerpt5" ["permalink5"]=> string(10) "permalink5" } } [6]=> array(1) { [0]=> array(4) { ["id6"]=> string(3) "id6" ["title6"]=> string(6) "title6" ["excerpt6"]=> string(7) "exerpt6" ["permalink6"]=> string(10) "permalink6" } } [7]=> array(1) { [0]=> array(4) { ["id7"]=> string(3) "id7" ["title7"]=> string(6) "title7" ["excerpt7"]=> string(7) "exerpt7" ["permalink7"]=> string(10) "permalink7" } } [8]=> array(1) { [0]=> array(4) { ["id8"]=> string(3) "id8" ["title8"]=> string(6) "title8" ["excerpt8"]=> string(7) "exerpt8" ["permalink8"]=> string(10) "permalink8" } } [9]=> array(1) { [0]=> array(4) { ["id9"]=> string(3) "id9" ["title9"]=> string(6) "title9" ["excerpt9"]=> string(7) "exerpt9" ["permalink9"]=> string(10) "permalink9" } } [10]=> array(1) { [0]=> array(4) { ["id10"]=> string(4) "id10" ["title10"]=> string(7) "title10" ["excerpt10"]=> string(8) "exerpt10" ["permalink10"]=> string(11) "permalink10" } } }
So it looks like the values are in the array, however, only the first index ( zero ) prints correctly.. any suggestions? Also, is there any way i can push an associate array and it not be over written so I dont have to increment the index?
There are a few problems here.
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
You are pushing an array, containing an array, containing 4 elements into $finalarray. There is no need for the outer array(). You can just do $newitem = array(...). array_push appends to end of the array, incrementing the index for you.
Second, in your foreach, your $count variable is not needed at all. When using foreach, $item is the element of the array. No need to look it up by the index.
If you want the index, however, foreach can give it to you.
foreach($finalarray as $count=>$item){
echo $count.':'.'<br>';
echo $item['title'.$count];
echo $item['id'.$count];
echo $item['permalink'.$count];
}
Your original code wasn't working because in each $item there was one array. That array had one item. In each element, the inner aray was always at index 0. Incrementing $count made it so only the 1st element worked, the others didn't because $item[1] didn't exist, it was always $item[0].
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
$finalarray[] = $newitem;
unset($newitem);
}
Since you don't care about the key of the final array just add it on using $finalarray[]
array(7) {
[0]=> array(2) { ["id"]=> string(1) "9" ["roi"]=> float(0) }
[1]=> array(2) { ["id"]=> string(1) "1" ["roi"]=> float(0) }
[2]=> array(2) { ["id"]=> string(2) "10" ["roi"]=> float(0) }
[3]=> array(2) { ["id"]=> string(2) "14" ["roi"]=> float(0) }
[4]=> array(2) { ["id"]=> string(1) "4" ["roi"]=> float(0) }
[5]=> array(2) { ["id"]=> string(1) "5" ["roi"]=> float(141) }
[6]=> array(2) { ["id"]=> string(1) "6" ["roi"]=> float(2600) }
}
I would just like to reverse this, so id 6 (with roi of 2600) comes first in the array etc.
How can I do this? array_reverse() and rsort() does not work in this case
http://php.net/manual/en/function.array-reverse.php:
$newArray = array_reverse($theArray, true);
The important part is the true parameter, which preserves the keys.
Not convinced? You can see it in action on this codepad exampole.
foreach($array as $arr){
array_unshift($array, $arr);
array_pop($array);
}
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$res4 = array();
$count = count($res);
for($i=$count-1;$i>=0;$i--){
$res4[$i] =$res[$i];
}
print_r($res4);
You can use an usort() function, like so
$arr = array('......'); // your array
usort($arr, "my_reverse_array");
function my_reverse_array($a, $b) {
if($a['roi'] == $b['roi'])
{
return 0;
}
return ($a['roi'] < $b['roi']) ? -1 : 1;
}
This will make sure the item with the highest roi is first in the array.
$res = array(
0=>array("id"=>9, "roi"=>0),
1=>array("id"=>1,"roi"=>0),
2=>array("id"=>10,"roi"=>0),
3=>array("id"=>14,"roi"=>0),
4=>array("id"=>4,"roi"=>0),
5=>array("id"=>5,"roi"=>141),
6=>array("id"=>6,"roi"=>2600));
$count = count($res);
for ($i=0, $j=$count-1; $i<=floor($count/2); $i++, $j--) {
$temp = $res[$j];
$res[$j] = $res[$i];
$res[$i] = $temp;
}
echo '<pre>';
print_r($res);
echo '</pre>';
It's easy. May be use usort function of php like:
usort($arr, function($a, $b) {
return $b['roi'] - $a['roi'];
});
Just swap position $a and $b that is correct.
From this array how could I get that array which first element is 128 and the second is 64.
$positions = array(
array('64','64','home.png','www.sdsd.vf'),
array('128','64','icon-building64.png','www.sdsd.vf')
);
Thanks for your help
Ungi.
foreach($positions as $position) {
if ($position[0] == '128' AND $position[1] == '64') {
// This is it!
}
}
Or you could drop the other members with array_filter().
$positions = array_filter($positions, function($position) {
return ($position[0] == '128' AND $position[1] == '64');
});
var_dump($positions);
Output
array(1) { [1]=> array(4) { [0]=> string(3) "128" [1]=> string(2) "64" [2]=> string(19) "icon-building64.png" [3]=> string(11) "www.sdsd.vf" } }
See it.
I have this $record array
array(4) {
[0]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "904" }
[1]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "903" }
[2]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "902" }
[3]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "901" }
}
How can i manipulate it so it will become like this?
array("901","902","903","904");
Thanks in advance
$subfunctionIds = array();
foreach($record as $values) {
$subFunctionIds[] = $values['SUBFUNCTION_ID'];
}
// If you want them reversed like in your example output...
$subFunctionIds = array_reverse($subFunctionIds);
var_dump($subFunctionIds);
function fetch($row) {
return $row["SUBFUNCTION_ID"];
}
$result = array_map("fetch", $record);
sort($result);
var_dump($result);
in 5.3+ you could do better:
$result = array_map(function ($row) { return $row["SUBFUNCTION_ID"]; }, $record);
sort($result);
var_dump($result);
Try doing this:
foreach ($array as $row ) {
$response[] = $row["SUBFUNCTION_ID"];
}
print_r($response);