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 need a simple way to extract values from an array.
I just can't get it to work.
This is the array from the database with <pre>:
Array
(
[0] => Array
(
[title] => Title1
)
[1] => Array
(
[title] => Title2
)
)
I can't get the titles to echo :-(
M.
-EDIT-
This did the trick for me:
foreach($arrays as $array)
{
echo $array['title'];
}
foreach($arrays as $array)
{
echo $array['title'];
}
Loop through the array like:
foreach($array as $arr)
{
echo $arr['title'];
}
Or to access one item, use:
echo $array[0]['title'];
echo $array[1]['title'];
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
I want to generate a nested array from a string.
The String looks like this
$item = "profile:my_wallet:btn_recharge";
I want to convert it to nested array like this
["profile"]["my_wallet"]["btn_recharge"]
What you could use is a simple recursion to go as deep as you like.
function append(array $items, array $array = []): array
{
$key = array_shift($items);
if (!$key) return $array;
$array[$key] = append($items, $array);
return $array;
}
$array = append(explode(':', "profile:my_wallet:btn_recharge"));
The result of $array looks like below and can be accessed as you asked
$array['profile']['my_wallet']['btn_recharge'];
array (
'profile' =>
array (
'my_wallet' =>
array (
'btn_recharge' =>
array (
),
),
),
)
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 have array like below:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg"]];
i want to add one more element to array $arr by "link"=>"uploads/hello.jpg"
My expected result:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg","link"=>"uploads/hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg","link"=>"uploads/abc.jpg"]];
Any solution for this thank.
You can iterate over the array using foreach, passing a reference into the loop to allow the values to be modified directly:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg"]];
foreach ($arr as &$a) {
$a['link'] = 'uploads/' . $a['pict'];
}
print_r($arr);
Output:
Array
(
[0] => Array
(
[id] => 001
[name] => Hello
[pict] => hello.jpg
[link] => uploads/hello.jpg
)
[1] => Array
(
[id] => 002
[name] => Abc
[pict] => abc.jpg
[link] => uploads/abc.jpg
)
)
Demo on 3v4l.org
You can iterate over each element in the array and set it that way.
for ($i = 0; $i < count($arr); i++) {
$arr[$i]['link'] = 'uploads/'.$arr[$i]['pict'];
}
foreach($arr as $key => $value){
$arr[$key]['link'] = "uploads/".$value['pict'];
}
Use the foreach loop to modify the original array. The $key value is used to refer to each index in the array.
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
I'm new to PHP, please help me to split this array. I never do this before.
I only want to show value http://domainXXX.com without string(18) on browser.
array(3) {
[7] => string(18)
"http://domain7.com"
[4] => string(18)
"http://domain4.com"
[3] => string(18)
"http://domain3.com"
}
Assuming the array looks like this, a simple foreach loop should allow you to output the urls
$links=array(
7 => 'http://domain7.com',
4 => 'http://domain4.com',
3 => 'http://domain3.com'
);
foreach( $links as $key => $url ) {
echo $key,' ',$url,'<br />';
}
don't use direct var_dump() to display output; you can use foreach() or any loop and foreach() has following-
//lets say your array name is $array
// here $key will hold 7,4,3 and $values will hold your domains
foreach($array as $key=>$value)
{
echo $value.'<br>';
}
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 7 years ago.
Improve this question
I have this php:
print_r($lack->call('channels.archive'));
which returns:
Array ( [ok] => 1 [url] => https://thename.slack.com/ [team] => theTeam [user] => jason [team_id] => T1EDES561 [user_id] => U0DD74SB8 )
How can I get that array into a html list or table?
Short, one line code solution:
echo "<ul><li>" . implode('</li><li>', $lack->call('channels.archive')) . "</li></ul>";
An example here.
If you want to print a simple table of all the keys and values, you can do this:
<?php
echo "<table>";
// header row ... not necessary
echo "<tr><th>Key></th><th>Value</th></tr>";
// loop through the array
foreach($lack->call('channels.archive') as $key => $val) {
// prints out header as a `<th>` and value as a `<td>
echo "<tr><th>$key</th><td>$val</td></tr>";
}
echo "</table>";
?>
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
$_REQUEST['assignment_site_id'];
Its output is
Array
(
[47] => 15
)
47 become dynamically. this is changed time to time like
Array
(
[34] => 16
)
I want 15 or 16.
How I can get this value.
Use a loop to print out the value of an associative array entry.
foreach ($Array as $key => &$value){
echo "Element $key is $value";
}
This would output "Element 47 is 15";
If that is what you are trying to find to do, print out the value of an array entry.
I don't know what you are trying to do. But this will output your expected result.
$yourVar = array();
$newValue = '';
foreach($yourVar as $key => $value){
$newValue = $value['your_searching'];
}
print_r($newValue);