Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This loop just gives me the output Array instead of the value that shows when I use the print_r function.
print_r gives me this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 1
)
but the echo just array
for($i=0; $i<($n*$n); $i++){
for($j=0; $j<($n*$n); $j++){
$number = "column" . $i . $j;
$plan = $field[$i][$j] = $_POST[$number];
$myvariable[] = $field[$i][$j];
}
echo $myvariable;
}
but if I remove the [] from $myvariable it prints out the values. the problem is that I need to use the array with an unique array
$unique = array_unique($myvariable);
if (count($unique) != count($myvariable)) {
echo ="no uniques";
}
Any tips?
You mean this ?
$unique = array_unique($myvariable);
foreach($unique as $value)
{
echo $value;
}
or make use of a typical for
$unique = array_unique($myvariable);
for($i=0;$i<count($unique);$i++)
{
echo $unique[$i];
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
after sorting the $at1 array , iwant the other arrays keys to be sorted as $at1 array !
<?php
$p1 = array (0=>'p1',1=>'p2',2=>'p3',3=>'p4',4=>'p5');
$at1 = array (0=>0, 1=>4, 2=>7, 3=>6, 4=>2);
$cbt1 = array(0=>5,1=>1,2=>2,3=>2,4=>1);
asort($at1);
?>
Looks like homework :)
$p1 = array (0=>'p1',1=>'p2',2=>'p3',3=>'p4',4=>'p5');
$at1 = array (0=>0, 1=>4, 2=>7, 3=>6, 4=>2);
$cbt1 = array(0=>5,1=>1,2=>2,3=>2,4=>1);
asort($at1);
$a_keys = array_keys($at1);
$p1 = repeat_order($a_keys, $p1);
$cbt1 = repeat_order($a_keys, $cbt1);
function repeat_order( $mask=array(), $haystack)
{
$result = array();
foreach ( $mask as $key ):
if ( isset($haystack[$key]) ):
$result[$key] = $haystack[$key];
unset($haystack[$key]);
endif;
endforeach;
// If the array to be sorted had more elements
foreach ( $haystack as $k => $v ):
$result[$k] = $v;
endforeach;
return $result;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
For example: if strArr is ["B:-1", "A:1", "B:3", "A:5"] then your program should return the string A:6,B:2.
Your final output string should return the keys in alphabetical order. Exclude keys that have a value of 0 after being summed up.
Examples
Input: array("X:-1", "Y:1", "X:-4", "B:3", "X:5")
Output: B:3,Y:1
Simple homework. You have to explode each pair into an array and to sum the values for each letter. Then sort (ksort by key), check and skip if there is a 0 into the produced array:
<?php
$sums = array();
$arr = array("X:-1", "Y:1", "X:-4", "B:3", "X:5");
foreach ($arr as $key => $pair) {
$pairArray = explode(":", $pair);
(!array_key_exists($pairArray[0], $sums))
? $sums[$pairArray[0]] = (int)$pairArray[1]
: $sums[$pairArray[0]] += (int)$pairArray[1];
}
print_r($sums);
ksort($sums);
$result = array();
foreach ($sums as $key => $value) {
if ($value != 0) {
array_push($result, $key . ":" . $value);
}
}
$result = implode(",", $result);
echo $result;
?>
Output:
Array
(
[X] => 0
[Y] => 1
[B] => 3
)
B:3,Y:1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have 3 arrays like this.
Array
(
[id] => 4
)
Array
(
[id] => 6
)
Array
(
[id] => 7
)
What I would like to do is make a variable called $id that prints the id values as a string 4,6,7. How would I accomplish this?
If you always have those three arrays with the element "your_key" each,In first put arrays in some variable then you can simply concatenate the values using '.' in php:
$arr1=Array
(
['id'] => 4
)
$arr2=Array
(
['id'] => 6
)
$arr3=Array
(
['id'] => 7
)
$key = "id";
$id = $arr1[$key] . "," . $arr2[$key] . "," . $arr3[$key];
echo $id;
Edit: You can use the same approach in a loop:
$id = "";
$key = "id";
foreach($results as $result) {
$id .= $result[$key] . ",";
}
$id = rtrim($id, ",");
echo $id;
Based on your comments, this seems like the way you should go
$id = '';
$i=0;
$results = $mysqli->query($query);
$len = count($results);
foreach($results as $result) {
$id .= $result['id'];
$i++;
if($i < $len) {
$id .= ',';
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the below given array
Array
(
[0] => 2,3
[1] => 2,3
)
I want to sum up and get result in a variable. E.g. variable a = 4 (2+2=4) and variable b = 6 (3+3=6). I am coding in php.
Use EXPLODE inside foreach
<?php
$yourArr = array('2,3','2,3');
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$tempnew = explode(",",$temp);
$a += $tempnew[0];
$b += $tempnew[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
?>
Try this
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$a += $temp[0];
$b += $temp[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
Output
a = 4
b = 6
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two arrays in my code that I want to print out on a webpage, the arrays hold search engine results, here are the structures of the foreach loops
This loop returns the url,title,snippet and score
foreach ($js->RESULT as $item)
{
$blekkoArray[str_replace ($find, '', ($item->{'url'}))] = array(
'title'=> $item->{'url_title'},
'snippet' => $item->{'snippet'},
'score' => $score--
);
}
This loop is used for combining two arrays and creating a ranked list
foreach($googleArray as $key=>$value)
{
if(isset($combined[$key]))
$combined[$key]["score"] += $value['score'];
else
$combined[$key] = array("score"=>$value['score'],"title"=>$value["title"], "snippet"=>$value["snippet"]);
}
I know I can use print_r to print arrays but its not very user friendly, I would also like to href the urls as clickable links, any ideas would be gratefully appreciated.
The arrays prints out
Array ( [red.com/] => Array ( [score] => 197 [title] => blah blah....[snippet] more blah blah )
Array ( [green.com/] => Array ( [score] => 196 [title] => blah blah....[snippet] more blah blah )
So I would like it to look like how a conventional search engine displays results, I don't need the score to be displayed
Here is a function I found on a blog a while ago that works for me. I don't remember where I found it, or I would credit them with it. See if this does what you need it to. It may not be perfect, but may help you find the keys you need, then you can modify this to print out what you need. Often with arrays I have trouble determining the structure enough to write my foreach loops, and this helps with that.
function listArrayRecursive($array_name, $ident = 0){
if (is_array($array_name)){
foreach ($array_name as $k => $v){
if (is_array($v)){
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . " | ";
listArrayRecursive($v, $ident + 1);
}else{
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . $v . " | ";
}
}
}else{
echo "Variable = " . $array_name;
}
}