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'm trying to do something I've never tried before: reading from APIs, and using the returned values in my website. To do this I need to read from multi-dimensional arrays in PHP.
See below the result I am getting from the API callback, when I run the following lines of code:
$tempContents = json_decode($data, true);
echo '<pre'; print_r($tempContents); echo '</pre';
Results:
Array (
[data] = Array
(
[boards] = Array
(
[0] = Array
(
[columns] = Array
(
[0] = Array
(
[title] = Name
)
[1] = Array
(
[title] = note
)
[2] = Array
(
[title] = notes2
)
[3] = Array
(
[title] = Status
)
)
)
)
)
[account_id] = xxxxxxx
)
So that's great, I can see the information there; however, I'm not sure about how to construct a foreach function to retrieve individual values. For example; the name of the second title value.
You can do this in 2 way
using foreach() directly:-
foreach($tempContents['data']['boards'][0]['columns'] as $title){
echo $title['title'].PHP_EOL;
}
Or using array_column() along with foreach():
$finalArray = array_column($tempContents['data']['boards'][0]['columns'],'title');
foreach($finalArray as $title){
echo $title.PHP_EOL;
}
Sample Output:- https://3v4l.org/4ERqd
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I stored links to $fotolar variable and I converted to array with explode.
My code:
$ex = explode('<br>', $fotolar);
echo "<pre>";
print_r($ex);
echo "</pre>";
Output:
Array
(
[0] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
[1] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
[2] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)
How can I add "url" key to this array?
For example:
Array
(
[0] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
)
[1] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
)
[2] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)
)
After exploded string to array, you can loop through this array to create a new array like this:
<?php
$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;
$arr = explode('<br>', $str);
$result = [];
foreach ($arr as $v) {
$result[]['url'] = $v;
}
var_dump($result);
Here is a one liner for you using array_map
$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;
$array = array_map(function($i){return['url'=>$i];},explode('<br>',$str));
Sandbox
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 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 6 years ago.
Improve this question
I have a database table that would return an array something like this
$roles['admin'] = 'app1,app2,app3';
$roles['moderator'] = 'app2,app3';
Now I want to traverse this array to show in my view, But instead of showing all apps inside each role, i would like to show all roles inside each app.
So ideally i would like the above array to become this
$apps['app1'] = 'admin';
$apps['app2'] = 'admin,moderator';
$apps['app3'] = 'admin,moderator';
I have been trying to solve this for 2 hours now, but for some reason I can't find an efficient way of doing this.
The following will traverse your array and load it into the appropriate array. It works by going through each part of the array and traversing it.
<?php
$apps = array();
foreach($roles as $key1 => $value1){
$parts = explode(',', $value1);
foreach($parts as $key2 => $value2){
$apps[$value2] .= (strlen($apps[$value2])>0)?",":"").$key1;
}
}
?>
You can map one array to the other by using array_reduce, array_keys and explode to turn your CSV values into arrays
$apps = array_reduce(array_keys($roles), function($apps, $key) use ($roles) {
foreach (explode(',', $roles[$key]) as $app) $apps[$app][] = $key;
return $apps;
}, []);
Note that the result is slightly different to what you wanted in that the values are themselves arrays instead of comma separated strings.
Array
(
[app1] => Array
(
[0] => admin
)
[app2] => Array
(
[0] => admin
[1] => moderator
)
[app3] => Array
(
[0] => admin
[1] => moderator
)
)
If you really need CSV values, add this
$apps = array_map(function($list) {
return implode(',', $list);
}, $apps);
which produces
Array
(
[app1] => admin
[app2] => admin,moderator
[app3] => admin,moderator
)
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
For example, here is a QR Image:
Source
This API decodes the QR and outputs it's content which is "HelloWorld" in this case.
How can I use file_get_contents() or a similar function to fetch the required data.
Actually is is possible to use file_get_contents() to access it. (tested AND working)
<?php
$url = 'http://api.qrserver.com/v1/read-qr-code/?fileurl=http%3A%2F%2Fapi.qrserver.com%2Fv1%2Fcreate-qr-code%2F%3Fdata%3DHelloWorld';
$stuff = file_get_contents($url);
$data = json_decode($stuff);
print_r($data);
?>
Which returns this object:
Array
(
[0] => stdClass Object
(
[type] => qrcode
[symbol] => Array
(
[0] => stdClass Object
(
[seq] => 0
[data] => HelloWorld
[error] =>
)
)
)
)
Allowing you to loop through (using foreach()) to echo out the stuff as you require.
foreach($data as $item) {
echo $item->symbol[0]->data;
}
Or simply:
echo $data[0]->sybmol[0]->data;
And that will give you the desired: HelloWorld
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
I have the following code:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = $attributeSelected['id'];
}
?>
This saves the data as:
Array (
[0] => SimpleXMLElement Object ( [0] => 23914175_laptop )
[1] => SimpleXMLElement Object ( [0] => 23914175_laptop )
[2] => SimpleXMLElement Object ( [0] => price_range_10_50004 )
)
However I just want the array data and not to include the "SimpleXMLElement Object". So I added the following code:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = json_decode(json_encode($attributeSelected['id']), true);
}
?>
Which now gives me the data as:
Array (
[0] => Array ( [0] => 23914175_laptop )
[1] => Array ( [0] => 23914175_laptop )
[2] => Array ( [0] => price_range_10_50004 )
)
I'm not sure what I'm doing wrong. I just want to save the data to an array and then use an If statement to check if the array contains specific data.
How about:
$attr_data[] = $attributeSelected['id'][0];
You are trying to store an object, if you need the first value of object you should change the third line to:
$attr_data[] = $attributeSelected['id']->0;
Please add a snippet of your XML, it makes things a lot clearer.
typecast the values to string:
$xml = simplexml_load_string($x); // assume XML in $x
$att = array();
foreach ($xml->search as $a) {
$att[] = (string)$a['id'];
}
see it working: http://codepad.viper-7.com/P0DI0S
The class SimpleXMLElement has a __toString method, so you could call it directly (which is kinda ugly IMO) or just typecast it to a string:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = (string) $attributeSelected['id'];
// Or
$attr_data[] = $attributeSelected['id']->__toString();
}
?>
Read more at http://php.net/manual/en/simplexmlelement.tostring.php