How equal array with empty? [closed] - php

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
Sir i have some problem about array. this is my code
$isi = array('1','6','7','9','4');
$cekarray = array('1','6','2');
if(!empty($cekarray[$isi]))
{
echo "b = ".1."<br>";
}
else
{
echo "b = ".0."<br>";
}
i hope output it should be look like
b = 1
b = 1
b = 0
thanks before sir

I think you are trying to find out which items in the $cekarray exist in the $isi array.
Here is one simple method of doing that
<?php
$isi = array('1','6','7','9','4');
$cekarray = array('1','6','2');
foreach ( $cekarray as $val) {
if ( in_array($val, $isi) ) {
echo "b = 1<br>";
} else {
echo "b = 0<br>";
}
}

If you want to check whether the values from $cekarray are present in $isi, this should give the desired output:
$isi = array('1','6','7','9','4');
$cekarray = array('1','6','2');
foreach ($cekarray as &$cekarrayValue) {
foreach ($isi as &$isiValue) {
if($cekarrayValue==$isiValue)
echo "b = 1<br />";
else
echo "b = 0<br />";
}
}
Well, only in this case. Depending on what you want, functionality must be added but it's a start. You can also take a look at in_array() function.

Related

How to use foreach array value out side of loop [closed]

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 6 years ago.
Improve this question
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo $value; // Result: 14
the result inside loop is: 10,11,12,13,14
and outside loop is : 14
I want to use all value outside of loop
Use below code to get your array values comma seperated
implode(",", $reciveValue)
Try this :
$value = '';
foreach($reciveValue as $val){
$value .= $val.",";// Result: based on user input like:10,11,12,13,14
}
echo rtrim($value, ',');
#Support Techcherry if you want to use all values of an array outside
the loop then you can use implode() function, below is an example
understand
<?php
$reciveValue = array(10,11,12,13,14); // suppose this is your array
foreach($reciveValue as $value){
echo $value.",";// Result: based on user input like:10,11,12,13,14
}
echo "<br>";
echo implode(',', $reciveValue); // here implode() function convert the array value in the string form
?>
try it, it will help you
thanks to everyone for posting answer
every answer is very helpful for me
but finally i got my solution
$result="";
foreach($reciveValue as $value)
{
$result=$value.",".$result;
}
echo $result;

Create Json from a mysql Result Set Resembling a Tree [closed]

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 6 years ago.
Improve this question
The problem is to get the following result set:
'org1', 'unit1', 'proj1'
'org1', 'unit1', 'proj2'
'org1', 'unit2', 'proj1'
'org1', 'unit2', 'proj2'
'org2', 'unit1', 'proj1'
'org2', 'unit1', 'proj2'
to the following in php:
[{"units": [{"name": "unit1", "projects": ["project1", "project2"]}, {"name": "unit2", "projects": ["project1", "project2"]}], "name": "org1"}, {"units": [{"name": "unit1", "projects": ["project1", "project2"]}], "name": "org2"}]
Any suggestions?
You can use something like
$final_array = array();
foreach($array as $row){
$final_array[$row[0]][$row[1]][] = $row[2];
}
Now this will "convert the SQL result array into a multidimentional array/tree" but not like what you want yet.
So we shall have to process the array again..
$final_array_2 = array(); // Lets go deep
foreach ($final_array as $name => $units) {
$quarterfinal_array = array(); // Not deep enough
$semi_final_array = array();
foreach ($units as $proj_name => $projects) {
$nano_final_array = array(); // Lets dig deeper ;)
$nano_final_array['name'] = $proj_name;
$nano_final_array['projects'] = $projects;
$semi_final_array[] = $nano_final_array;
}
$quarterfinal_array['units'] = $semi_final_array;
$quarterfinal_array['name'] = $name;
$final_array_2[] = $quarterfinal_array;
}
echo json_encode($final_array_2);
PS: Sorry my choice of variable names are not the most ideal, but they get the work done ;) This is a P.O.C, You can always improve on it.
DEMO

How to iterate through all possible combinations in an simple array [closed]

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
Kindly help me with my code:
$a[1]='cloropil';
$a[2]='rodopayta';
$r=2;
$c=1;
$f= while($c<=$r){ echo $a[$c];;
$e = $c++ };
while($c<=$r){
echo $f;
$r++;
}
while($c<=$r){
echo $e;
$r++;
}
I want its output to be like this:
$a[1]='cloropil';
$a[2]='rodopayta';
$r=2;
$c=1;
while($c<=$r){
echo $a[$c];
while($c<=$r){
echo $a[$c];
$c++;
}
$c++;
}
How ever the out put is blank.
it should even display at least "cloropil".
Is this even possible?
I want to display something like this:
cloropilcloropil
cloropilrodopyta
rodopytacloropil
rodopytarodopyta
thank you very much in advance.
I think this may be what you are looking for?:
<?php
$a[]='cloropil';
$a[]='rodopayta';
// You first iterate over each
for($i = 0; $i < count($a); $i++)
{
// As you iterate through the first round, you iterate
// again to match pairs but include the first iteration
// value with this second
foreach($a as $val)
echo $a[$i].'=>'.$val.'<br />';
}
?>
Gives you:
cloropil=>cloropil
cloropil=>rodopayta
rodopayta=>cloropil
rodopayta=>rodopayta

IUsing foreach function i need get the output? [closed]

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
<?php
$val=array(4,10,15);
$val2=array('ravi,suresh,kumar');
foreach ($val2 as $title) {
foreach ($val as $title1) {
echo $title;
echo $title1;
}
}
?>
o/p:
ravi,suresh,kumar4ravi,suresh,kumar10ravi,suresh,kumar15
but i requeied
ravi 4
suresh10
kumar15
Use one loop.
for($i = 0; $i < count($val); $i++) {
print $val[$i] . " " . $val2[$i];
}
The issue with your nested for loops is that it iterates over the first element of val1, then all the elements of val 2. Then the 2nd element of val1, and all the elements of val2 and so on.
since you want to go through both at the same time, use a standard for loop.

Get data from mysql as array [closed]

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
Sorry for my english..
in mysql has row name iurl.. There is data: 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg
i get its as:
<?php $s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
if($s){
$array = array();
while($t=mysql_fetch_array($s)) {
$array[] = $t['iurl'];
}
print_r($array);
?>
it gives me result: Array ( [0] => 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg )
And i`m need get it and print like a link
how can i do it?
Thanks..
You can use explode(); to split the string into an array and then loop over to print each item:
$images = explode(",", $t["iurl"]);
foreach ($images as $image) {
echo "{$image}";
}
I think you are asking to have result or in your case a .jpg file be the href of your link? If so do this:
......
The result you get from your database as you already figured out is an Array
The only thing you need to do is loop trough the array.
ps: consider using mysqli more info at php.net http://www.php.net/manual/en/book.mysqli.php
<?php
$s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
while ($row = mysql_fetch_object($s)) {
echo '<img="http://yourhost.com/images/'.$row->image.' alt=""/>';
}
?>

Categories