I am trying to create an associate array like this
while($row = $result1->fetch_assoc()) {
$user = $row['first_name'] ."_" . $row['last_name'];
$userholder[$user] = $row['choice'];
$event = $row['event_name'] . "_" . $row['event_location'] . "_" . $row['even_date'];
$consolidateEvents[$event] = $userholder;
}
but my $consolidateEvents array is numeric. I cannot see what I am doing wrong. Why am I not getting $event as the key for my array?
Try this code to correct your output,
function custom_function($input_array){
$output_array = array();
foreach ($input_array as $key => $value) {
foreach ($v as $k => $v) {
$output_array[$key][$k] = $v;
}
}
return $output_array;
}
Give it a try, this will work
Related
I'm using var_export to dump output to logs when errors occur. However since the result is in pure text, I don't get a chance to push it through some sort of library like krumo so I can interactively explores the output.
What methods do people have to deal with making var_export text more readable?
Here is my function, it works well for multidimensional arrays:
function VE($varname, $varval, $short_syntax=true, $tag = ' ', $comma='', $end_line="\r\n") {
$res = '';
if($short_syntax){
$begin_array = '[';
$end_array = ']';
} else {
$begin_array = 'array(';
$end_array = ')';
}
$arr = explode('/',$varname);
$dim =count($arr)-1;
$lastKey = end($arr);
if (! is_array($varval)){
if( is_string($varval)) $varval = "'$varval'";
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $varval . $comma . $end_line;
}else{
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $begin_array . $end_line;
$count_varval = 0;
$dim_varval = count($varval);
foreach ($varval as $key => $val){
$count_varval++;
if($count_varval<$dim_varval) $commma=','; else $commma='';
if( is_string($key)) $key = "'$key'";
$res .= VE ($varname . "/" . $key , $val, $short_syntax, $tag, $commma);
}
$res .= str_repeat($tag,$dim) . $end_array . $comma . $end_line;
}
return $res;
}
$bigarray = array(); // your array
$bb = VE ('$bigarray', $bigarray);
echo "<pre>$bb</pre>";
I hope it helps ;)
I am new at php, so please be kind.
I am building a script that gets the number of facebook likes from facebook pages.
Then it sorts them, I have found a way to add the page's profile picture using css, however the only class I am able to add is a url. how can I give each thumbnail it's own class, which I can then apply the css to?
here is my code:
function array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
function getLikes($arr){
$urls = "";
// Add urls to check for likes
for($i = 0;$i < count($arr);$i++) {
if($urls != "") $urls .= ",https://www.facebook.com/";
$urls .= $arr[$i];
}
// Retreive info from Facebook
$xml = simplexml_load_file("http://api.facebook.com/restserver.php?method=links.getStats&urls=https://www.facebook.com/" . $urls);
$likes = array();
// Loop through the result and populate an array with the likes
for ($i = 0;$i < count($arr);$i++) {
$url = $xml->link_stat[$i]->url;
$counts = (int)$xml->link_stat[$i]->like_count;
$likes[] = array('likes' => $counts,'url' => $url);number_format(1000, 0, '.', ',');
}
return $likes;
}
$array = array("kylieminogue","SiaMusic","iggyazalea");
$likes = getLikes($array);
$likes = array_sort($likes, 'likes', SORT_DESC);
foreach ($likes as $key => $val) {
$final = number_format($val['likes'], 0, '.', ',');
echo "<li class='facebook'><div class='fb-page'><div class='rank'>" . $key . "</div>" . "<div class='thumb " . $val['url'] . "'><div class='link'>" . $val['url'] . "</div></div>" . "<div class='likes'>" . $final . "</div></div></li><br />";
}
If you do this in getLikes(), inside the second loop:
$likes[] = array(
'likes' => $counts,
'url' => $url,
// create a hopefully unique class name
'class' => strtolower($arr[$i]) . '-' . $i
);
// After this you call number_format without receiving its value, why?
Then in the HTML you change
"<div class='thumb " . $val['url'] . "
for
"<div class='thumb " . $val['class'] . "
Is this what you mean?
Say I have this array
$array = array('pen' => 'blue', 'paper' => 'red', 'ink' => 'white');
When I loop through it
$string = '';
foreach ($array AS $key=>$value) {
$string .= $key . ' = ' . $value;
}
I want to get the "line number" of the element the loop is currently on.
If the loop is on "pen" I would get 1.
If the loop is on "paper" I would get 2.
If the loop is on "ink" I would get 3.
Is there an array command for this?
No. You will have to increment an index counter manually:
$string = '';
$index = 0;
foreach ($array as $key=>$value) {
$string .= ++$index . ") ". $key . ' = ' . $value;
}
Use array_values() function to extract values from array. It indexes array numerically and $key will be the index of value in loop.
$array = array('pen' => 'blue', 'paper' => 'red', 'ink' => 'white');
$array = array_values($array);
$string = '';
foreach ($array as $key => $value) {
$string .= $key + 1 . ' = ' . $value;
}
$i = 0;
foreach ($array as $key=>$value) { // For each element of the array
print("Current non-associative index: ".$i."<br />\n"); // Output the current index
$i++; // Increment $i by 1
}
Hope that helps.
I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}
PDO sql codes :
while($r = $q->fetch(PDO::FETCH_ASSOC)){
$gg = join('</td><td>', $r);
echo "<tr><td>" . $no_count . "</td><td>" . $gg . "</td></tr>";
$no_count = $no_count + 1;
}
variable $r is the record, how can I echo the field name of $r?
Let's say $r carry records from 2 different fields "product" and "price". The value of $r is "apple", "130". How can I add "usd" into "130"?
I need something like.... if $field_name == "$r['price']" { $r = join('usd', $r); };
Thanks to Mike B, I almost there :
while($r = $q->fetch(PDO::FETCH_ASSOC)){
foreach ($r as $name => $value) {
if ($name == "price"){
$r = "usd" . $value; // this line got problem, how to change the value in an array variable $r?
}
}
$gg = join('</td><td>', $r);
echo "<tr><td>" . $no_count . "</td><td>" . $gg . "</td></tr>";
$no_count = $no_count + 1;
}
array_keys($r) will get you a list of fields from the table since you're fetching an associative array.
You can also loop through $r:
foreach ($r as $name => $value) {
print "$name: " . $value;
}
Update
// this line got problem, how to change the value in an array variable $r?
$r[$name] = 'usd' . $value;
Make the edit to the original name. Since you have the key in the $name variable from the foreach loop you can set it directly.