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 want do add values to array in my function to show movies:
$movies = array();
function find_movie($tag) {
// SELECT MOVIE BY %TAG%
$movies[] = $MOVIE;
return 'WATCH MOVIE';
}
Induction:
echo find_movie('cars'); // WATCH MOVIE
My problem is here -> $movies array is empty...
I want to have array with all movies what is showed.
How can I do that ?
You need to pass the $movies array as a reference to make this work out.
Like this..
function find_movie($tag,&$movies){
and call it like..
echo find_movie('cars',$movies);
The code..
<?php
$movies = array();
function find_movie($tag,&$movies) {
$movies[] = "<a href=$tag>WATCH MOVIE</a>";
}
find_movie('cars',$movies);
var_dump($movies);
OUTPUT:
array (size=1)
0 => string '<a href=cars>WATCH MOVIE</a>' (length=28)
Related
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 2 years ago.
Improve this question
how do i loop this kind of array?
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
I want to loop through the column of each item.
e.g.: i want to display ('apple','373','t0','1') at the first loop and ('orange','22','h0','0') at the last loop. thanks
We are assuming that all the arrays inside the main array are the same size in this example.
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
for($i = 0; $i<sizeof($arr["aa"]); $i++)
{
foreach($arr as $key=>$item)
{
echo($item[$i]);
}
echo ' - ';
}
Output: (obviously you can do any necessary formatting you need such as new lines or commas)
apple373t01 - orange22h00 -
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 3 years ago.
Improve this question
I need an array like this in PHP?
array = [mani , mani, nithi, nithi, basto]
return in
array = [basto]
not for other elements
Does anyone know how to solve this problem?
<?php
$items = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$counts = array_count_values($items);
// Remove elements that occur more than once.
$filtered = array_filter($items, function ($item) use ($counts) {
return $counts[$item] === 1;
});
var_export($filtered);
Output:
array (
4 => 'basto',
)
You may want to specify your question so users here can provide better assistance.
To your issue:
array_unique($array);
https://www.php.net/manual/en/function.array-unique.php
EDIT: you want to search all items by name, to do that you need this function: https://www.php.net/manual/en/function.array-search.php
with your example:
$array = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$basto = array_search('basto', $array);
Best,
Sebo
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 2 years ago.
Improve this question
I am using json_encode on foreach its return multiple values i want only one value after foreach completed
foreach($tree as $file) {
$arr = array('success'=>'ok');
echo json_encode($arr);
}
{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}
Expected Output
{"success":"ok"}
Do like following:
foreach($tree as $file) {
$arr = array('success'=>'ok');
}
echo json_encode($arr);
Output will be as expected: {"success":"ok"}
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 5 years ago.
Improve this question
I have two array i want to match a key of first array with another array and if the values of both key matches then add the value of second array to array 1
Array 1
[{"currency":1,"amount":23},{"currency":1,"amount":30},{"currency":2,"amount":40},]
Array 2
[{"currency_id": 1,"currency_symbol":$},{"currency_id":2,"currency_symbol":€}]
Desired output is:
[{"currency":$,"amount":23},{"currency":$,"amount":30},{"currency":€,"amount":40}]
The Code i am using is:
foreach($a1 as $key) {
foreach($a2 as $cKey){
if($a1['currency']==$a2['currency_id']){
$a1['currency_symbol'] = $a2['currency_symbol'];
echo $a1['currency_symbol'];
}
}
}
foreach($arr1 as $k=>$key) {
foreach($arr2 as $cKey){
if($key['currency']==$cKey['currency_id']){
$arr3[$k]['currency'] = $cKey['currency_symbol'];
$arr3[$k]['amount'] = $key['amount'];
}
}
}
var_export($arr3);
You only need to assign variable and print outside loop.
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=""/>';
}
?>