hello can you help me to find the way to print the index of a multidimensional array
My script
<Php?
$a= array();
$a[0][0][0] = 0;
$a[0][0][1] = 1;
$a[0][1][0] = 2;
$a[0][1][1] = 3;
$a[1][0][0] = 4;
$a[1][0][1] = 5;
$a[1][1][0] = 6;
foreach( $a as $v){
foreach( $v as $v1 ){
foreach ($v1 as $v2=>$value){
echo"[][][$v2] = $value\n";
}
}
}
?>
//Result//
[][][0] = 0
[][][1] = 1
[][][0] = 2
[][][1] = 3
[][][0] = 4
[][][1] = 5
[][][0] = 6
I have a feeling you might want to print like this.
$a = [];
$a[0][0][0] = 0;
$a[0][0][1] = 1;
$a[0][1][0] = 2;
$a[0][1][1] = 3;
$a[1][0][0] = 4;
$a[1][0][1] = 5;
$a[1][1][0] = 6;
foreach( $a as $i => $v){
foreach( $v as $i1 => $v1 ){
foreach ($v1 as $i2=>$value){
echo"[$i][$i1][$i2] = $value\n";
}
}
}
RESULT
[0][0][0] = 0
[0][0][1] = 1
[0][1][0] = 2
[0][1][1] = 3
[1][0][0] = 4
[1][0][1] = 5
[1][1][0] = 6
Related
I have an image gallery and each is saved by index.. gallery1: /image here.. gallery2: / image here.. etc..
I'm using an index with multiple for loops to return the images and by column because it's either Masonry or rectangular. I am getting the return fine except for the very last index.
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 1;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}
I know it can't be that hard but I'm not that good at it right yet.
Any help is so much welcome.
Thank You.
You set the variable $ind to 1, count the length of the array, and then initialize the for loop with the condition $ind < $length.
When $ind reaches the index needed to access the last item, the loop is not run again, since $ind is now equal to $length, not smaller.
You can fix this by changing the condition in your for-loop to "less or equal":
$i < $columns && $ind <= $length
This runs the loop once more when $ind has reached the last index.
Probably the problem is at this line:
$ind = 1;
In PHP non-associative arrays have the indexes starting to 0 instead of 1:
$arr = array('a', 'b', 'c');
print_r($arr);
// output:
Array ( [0] => a [1] => b [2] => c )
So, try to change that line in your code to:
$ind = 0;
Complete code:
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 0;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}
I am trying to sort an array based on a column value:
Example:
$cats[0]['holders'] = 55;
$cats[1]['holders'] = 66;
$cats[2]['holders'] = 77;
$cats[3]['holders'] = 23;
$cats[4]['holders'] = 64;
$cats[5]['holders'] = 82;
$cats[6]['holders'] = -3;
$cats[7]['holders'] = -5;
$cats[8]['holders'] = -17;
$cats[9]['holders'] = -25;
$cats[10]['holders'] = 66;
$cats[11]['holders'] = -10;
$cats[12]['holders'] = 0;
$cats[13]['holders'] = 5;
$cats[14]['holders'] = 4;
$cats[15]['holders'] = -3;
function compareHolders($a, $b) {
$aPoints = $a['holders'];
$bPoints = $b['holders'];
return strcmp($aPoints, $bPoints);
}
$cats = usort($cats, 'compareHolders');
I would like to get the top 5 values and bottom 5 values:
$first_5_tokens = array_slice($tokens, 0, 5, true);
print_r($first_5_tokens);
$last_5_tokens = array_slice($tokens, -5);
print_r($last_5_tokens);
This is not sorting the array for me by "holder" value. How can I do this? Thanks!
Your comparison function sorts array in descending order and also, don't use it like - $cats = usort($cats, 'compareHolders'); Instead just use it like - usort($cats, 'compareHolders');
I changed your function and did it like this.
$cats[0]['holders'] = 55;
$cats[1]['holders'] = 66;
$cats[2]['holders'] = 77;
$cats[3]['holders'] = 23;
$cats[4]['holders'] = 64;
$cats[5]['holders'] = 82;
$cats[6]['holders'] = -3;
$cats[7]['holders'] = -5;
$cats[8]['holders'] = -17;
$cats[9]['holders'] = -25;
$cats[10]['holders'] = 66;
$cats[11]['holders'] = -10;
$cats[12]['holders'] = 0;
$cats[13]['holders'] = 5;
$cats[14]['holders'] = 4;
$cats[15]['holders'] = -3;
foreach($cats as $cat) {
echo $cat['holders'] . "<br>";
}
// function compareHolders($a, $b) {
//
// $aPoints = $a['holders'];
// $bPoints = $b['holders'];
//
// return strcmp($aPoints, $bPoints);
//
// }
function compareHolders($a, $b) {
$a = $a['holders'];
$b = $b['holders'];
if ($a == $b)
return 0;
return ($a > $b) ? -1 : 1;
}
usort($cats, 'compareHolders');
echo "<h4>After</h4>\n";
foreach($cats as $cat) {
echo $cat['holders'] . "<br>";
}
// print_r($cats);
$tokens = $cats;
echo "<h4>First Five</h4>\n";
$first_5_tokens = array_slice($tokens, 0, 5, true);
print_r($first_5_tokens);
echo "<h4>Last Five</h4>\n";
$last_5_tokens = array_slice($tokens, -5);
print_r($last_5_tokens);
I have such associative array. The key prev contains a value to match the id value of the previous item.
When prev is 0 then it's the first item.
Correct order should be by index prev:
$data[3]
$data[1]
$data[0]
$data[4]
$data[2]
but I don't know how to achieve this.
$data[0]['id'] = 10;
$data[0]['name'] = 'Zoe';
$data[0]['prev'] = 20;
$data[1]['id'] = 20;
$data[1]['name'] = 'Tom';
$data[1]['prev'] = 40;
$data[2]['id'] = 30;
$data[2]['name'] = 'Andy';
$data[2]['prev'] = 50;
$data[3]['id'] = 40;
$data[3]['name'] = 'Kathy';
$data[3]['prev'] = 0;
$data[4]['id'] = 50;
$data[4]['name'] = 'Barbara';
$data[4]['prev'] = 10;
I think this is what you want to do:
<?php
$data = array();
$data[0]['id'] = 10;
$data[0]['name'] = 'Zoe';
$data[0]['prev'] = 20;
$data[1]['id'] = 20;
$data[1]['name'] = 'Tom';
$data[1]['prev'] = 40;
$data[2]['id'] = 30;
$data[2]['name'] = 'Andy';
$data[2]['prev'] = 50;
$data[3]['id'] = 40;
$data[3]['name'] = 'Kathy';
$data[3]['prev'] = 0;
$data[4]['id'] = 50;
$data[4]['name'] = 'Barbara';
$data[4]['prev'] = 10;
$nextId = 0;
$results = array();
while (count($data) > 0) {
$matchFound = false;
foreach ($data as $key=>$val) {
if ($val['prev'] === $nextId) {
$results[] = $val;
$nextId = $val['id'];
unset($data[$key]);
$matchFound = true;
break;
}
}
if (!$matchFound) break;
}
The outer while loops checks if the $data array still has elements. The inner for loop searches for the array with element 'prev' equal to the $nextId that we are looking for (initially set to 0). When it finds it, it assigns the id to $next, removes the element from the original array, and adds the element to our sorted array.
Do you mean order by "pre"? You want PHP's usort() function:
<?php
$data = array();
$data[0]['id'] = 10;
$data[0]['name'] = 'Zoe';
$data[0]['prev'] = 20;
$data[1]['id'] = 20;
$data[1]['name'] = 'Tom';
$data[1]['prev'] = 40;
$data[2]['id'] = 30;
$data[2]['name'] = 'Andy';
$data[2]['prev'] = 50;
$data[3]['id'] = 40;
$data[3]['name'] = 'Kathy';
$data[3]['prev'] = 0;
$data[4]['id'] = 50;
$data[4]['name'] = 'Barbara';
$data[4]['prev'] = 10;
function my_order($a,$b) {
return $a['prev'] > $b['prev'];
}
usort($data, "my_order");
print_r($data); //Kathy, Barbara, Zoe, Tom, Andy
Using the function "my_order", which compares $data[$x]['prev'] to $data[$x+1]['prev'], you can yield the results sorted by the "prev" values of the array.
You can use PHP's usort function to do so. It takes an array as the first parameter and then a comparison function as the second. From the documentation:
function cmp($a, $b){
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
The comparison function returns 0 if they're equivalent, -1 if a comes before b, and 1 if b comes before a. You can order it however you like.
For you, it could look like:
function cmp($a, $b){
if ($a['prev'] == $b['prev']) {
return 0;
}
return ($a['prev'] < $b['prev']) ? -1 : 1;
}
usort($data, "cmp");
you should try this hope it helps you
$data = array();
$data[0]['id'] = 10;
$data[0]['name'] = 'Zoe';
$data[0]['prev'] = 20;
$data[1]['id'] = 20;
$data[1]['name'] = 'Tom';
$data[1]['prev'] = 40;
$data[2]['id'] = 30;
$data[2]['name'] = 'Andy';
$data[2]['prev'] = 50;
$data[3]['id'] = 40;
$data[3]['name'] = 'Kathy';
$data[3]['prev'] = 0;
$data[4]['id'] = 50;
$data[4]['name'] = 'Barbara';
$data[4]['prev'] = 10;
function sortIt($data) {
$output = array();
$key = array_search(0, array_column($data, 'prev'));
$output[] = $data[$key];
unset($data[$key]);
$data = array_combine(
array_column($data,'prev'),
$data
);
while($data){
$last = end($output);
$output[] = $data[$last['id']];
unset($data[$last['id']]);
}
return $output;
}
echo '<pre>';
print_r(sortIt($data));
You could get a bit better response time using a hash:
foreach($data as $rec) {
$hash[$rec["prev"]] = $rec;
}
$id = "0";
while (isset($hash[$id])) {
$result[] = $curr = $hash[$id];
$id = $curr["id"];
}
See it run on eval.in
Hi i have following php codes(part of my full code):
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['serisname1'] = $new_instance['serisname1'];
$instance['serisname2'] = $new_instance['serisname2'];
$instance['serisname3'] = $new_instance['serisname3'];
$instance['serisname4'] = $new_instance['serisname4'];
$instance['serisname5'] = $new_instance['serisname5'];
$instance['serisname6'] = $new_instance['serisname6'];
$instance['serisname7'] = $new_instance['serisname7'];
$instance['serisname8'] = $new_instance['serisname8'];
$instance['serisname9'] = $new_instance['serisname9'];
$instance['serisname10'] = $new_instance['serisname10'];
$instance['serisname11'] = $new_instance['serisname11'];
$instance['serisname12'] = $new_instance['serisname12'];
$instance['serisname13'] = $new_instance['serisname13'];
$instance['serisname14'] = $new_instance['serisname14'];
$instance['serisname15'] = $new_instance['serisname15'];
return $instance;
for($x = 1; $x <= 15; $x++)
$serisname = $instance[serisname.$x];
$items[] = $serisname;
print_r($items);
my export :
array ( [0] => The Flash )
i want its be like :
array ( [0] => The Flash [1] => Arrow [2] => Game Of Throne and etc...)
the problem is its only echo last result but i want its echo every 15 results line by line.
for($x = 1; $x <= 15; $x++){
$serisname = $instance['serisname'.$x];
$items[] = $serisname;
}
print_r($items);
If I understead your question correctly, you can try this:
$count = 0;
for($x = 0; $x <= count($instance); $x++) {
$items[$x][] = $instance[serisname.$x];
if($count == 15) {
$count = 0;
}
}
Maybe can include the instance before the loop in your question so we can replicate the array?
I have a query like this:
select truck, oil_type, km_min, km_max from trucks;
I'm using codeigniter and with the $query->result_array() function so its loaded in an associative array with this structure:
/*
array(
truck
oil_type
km_min
km_max
)
*/
$arr[0]["truck"] = 2;
$arr[0]["oil_type"] = 2;
$arr[0]["km_min"] = 345;
$arr[0]["km_max"] = 567;
$arr[1]["truck"] = 2;
$arr[1]["oil_type"] = 4;
$arr[1]["km_min"] = 234;
$arr[1]["km_max"] = 867;
$arr[2]["truck"] = 1;
$arr[2]["oil_type"] = 2;
$arr[2]["km_min"] = 545;
$arr[2]["km_max"] = 867;
$arr[3]["truck"] = 4;
$arr[3]["oil_type"] = 3;
$arr[3]["km_min"] = 45;
$arr[3]["km_max"] = 567;
Then, I'm trying to restructure it grouping the trucks by the id, something like this:
/*
trucks - array(
truck
truck_data - array(
oil_type
km_min
km_max
)
)
*/
$arr["truck"][0]= 2;
$arr["truck"][0]["truck_data"][0]["oil_type"] = 2;
$arr["truck"][0]["truck_data"][0]["km_min"] = 345;
$arr["truck"][0]["truck_data"][0]["km_max"] = 567;
$arr["truck"][0]["truck_data"][1]["oil_type"] = 4;
$arr["truck"][0]["truck_data"][1]["km_min"] = 234;
$arr["truck"][0]["truck_data"][1]["km_max"] = 867;
$arr["truck"][1]= 1;
$arr["truck"][1]["truck_data"][0]["oil_type"] = 2;
$arr["truck"][1]["truck_data"][0]["km_min"] = 545;
$arr["truck"][1]["truck_data"][0]["km_max"] = 867;
$arr["truck"][2]= 4;
$arr["truck"][2]["truck_data"][0]["oil_type"] = 3;
$arr["truck"][2]["truck_data"][0]["km_min"] = 45;
$arr["truck"][2]["truck_data"][0]["km_max"] = 567;
I thought in something like the code below:
$res = $query->result_array();
$cnt_total = count($res);
$y = 0;
for ($x=0; $x < $cnt_total -1 ; $x++) {
$truck = $res[$x]["truck"];
$trucks["truck"][$y] = $truck;
$q = $x + 1;
$i = 0;
do{
$trucks[$y][$i]["oil_type"] = $res[$q]["oil_type"];
$trucks[$y][$i]["km_min"] = $res[$q]["km_max"];
$trucks[$y][$i]["km_max"] = $res[$q]["km_max"];
$q++;
$i++;
if ($q <= $cnt_total) break;
} while ( $truck === $res["truck"][$q] );
$y++;
}
But does not work properly... I'm too far of the solution? The performance It's important for me in this particular case.
There is a phpfiddle where you can try:
http://phpfiddle.org/main/code/67j-ui5
Any idea, tip, or advice will be appreciated, and if you need more info, let me know and I'll edit the post.
Try this:
$res = $query->result_array();
$trucks = array();
foreach ($res as $row) {
$truck["truck"] = $row["truck"];
$truck["truck_data"] = array(
'oil_type' => $row["oil_type"],
'km_min' => $row["km_min"],
'km_max' => $row["km_max"],
);
$trucks[] = $truck;
}
var_dump($trucks);
What you've provided as a desired result is a little ambiguous. This might give what you're asking for.
$trucks=array();
$res = $query->result_array(); // assuming this is actually several trucks
foreach ($res as $r){
// set up the array from the data without writing out every column manually
$truck=array(
'truck'=>$r['truck'],
'truck_data'=>$r,
);
// remove the bit you wanted separately as 'truck' from 'truck_data'
unset($truck['truck_data']['truck']);
// push into $trucks
$trucks[]=$truck;
}
Is this what you need? conversion result is stored in $result
$result = array();
foreach($query->result_array() as $truck)
{
$new_truck = array();
$new_truck['truck'] = $truck['truck'];
$new_truck['truck_data'] = array();
$new_truck['truck_data']['oil_type'] = $truck['oil_type'];
$new_truck['truck_data']['km_min'] = $truck['km_min'];
$new_truck['truck_data']['km_max'] = $truck['km_max'];
array_push($result, $new_truck);
}