For a wordpress form I look for the entries using:
<?php
print_r($entry); exit;
?>
This gives me:
Array ( [1.1] => 2015 [1.2] => Infiniti [1.3] => Q70 )
How do I write the full php to echo these chosen entries individually that are entered on the form (from a 3 stage chained select field).
I have read this PHP, how to echo specific object data from array? and for his scenario he needs:
echo $arr['name_en'][0];
But I have no apparent 'name_en' equivalents
I am new to php so if I need to define my version of $arr help with that for this scenario would be great $arr = $entry['field'] or something?
If this is your array
Array (
[1.1] => 2015,
[1.2] => Infiniti,
[1.3] => Q70
)
To print single values in PHP you have to use a syntax like this:
echo $array_name[1.1];
echo $array_name[1.2];
echo $array_name[1.3];
And the output will be
2015
Infiniti
Q70
Have a look to the official documentation to understand how an array works PHP Manual: Arrays
I am not sure it response to your question.
Here is how to echo all value inside an array.
$arr = array('1.1' => 2015, '1.2' => 'Infiniti', '1.3' => 'Q70');
foreach ($arr as $key => $value) {
echo $value;
}
Related
I have a problem with the array PHP function, below is my first sample code:
$country = array(
"Holland" => "David",
"England" => "Holly"
);
print_r ($country);
This is the result Array ( [Holland] => David [England] => Holly )
I want to ask, is possible to make the array data become variables? For second example like below the sample code, I want to store the data in the variable $data the put inside the array.:
$data = '"Holland" => "David","England" => "Holly"';
$country = array($data);
print_r ($country);
But this result is shown me like this: Array ( [0] => "Holland" => "David","England" => "Holly" )
May I know these two conditions why the results are not the same? Actually, I want the two conditions can get the same results, which is Array ( [Holland] => David [England] => Holly ).
Hope someone can guide me on how to solve this problem. Thanks.
You can use the following Code.
<?php
$country = array(
"Holland" => "David",
"England" => "Holly"
);
foreach ($country as $index => $value)
{
$$index = $value;
}
?>
Now, Holland & England are two variables. You can use them using $Holland etc.
A syntax such as $$variable is called Variable Variable. Actually The inner $ resolves the a variable to a string, and the outer one resolves a variable by that string.
So there is this thing called
Destructuring
You can do it something like ["Holland" => $eolland, "England" => $england] = $country;
And now you have your array elements inside the variables.
Go read the article above if you want more information about this because it gets really useful (especially in unit tests usind data provders from my experience).
If you want to extract elements from an associative array such that the keys become variable names and values become the value of that variable you can use extract
extract($country);
To check what changed you can do
print_r(get_defined_vars());
Explanation on why the below does not work
$data = '"Holland" => "David","England" => "Holly"';
When you enclose the above in single quotes ' php would recognise it as a string. And php will parse a string as a string and not as an array.
Do note it is not enough to create a string with the same syntax as the code and expect php to parse it as code. The codes will work if you do this
$data = ["Holland" => "David","England" => "Holly"];
However, now $data itself is an array.
A simple copy of an array can be made by using
$data = $country;
I have an array which I create. When reading the array with print_r it is not returning with the correct data inputted! I am missing specific sectionsn such as < & > brackets with its headings.
How can i preserve these?
Code:
$params = array(
"Parm1" => "test",
"Parm2" => "hi",
"Parm3" => GUID(),
"Parm4" => "lol",
"Parm5" => "
<R>
<R1>the</R1>
<R2>dog</R2>
<R3>is</R3>
<R15>happy</R15>
<R20>today</R20>
</R>
");
Basically the only data that is jumbled up is the Parm5 section. I want everything inside to return exactly as it is! EG: Reading as is i only receive Array ( [Parm1] => test [Parm2] => hi [Parm3] => B18BE727-8F79-4D4A-80EA-3974B1429F78 [Parm4] => lol [Parm5] => the dog is happy today ) from print_r
I want to return:
Array ( [Parm1] => test [Parm2] => hi [Parm3] => B18BE727-8F79-4D4A-80EA-3974B1429F78 [Parm4] => lol [Parm5] => <R><R1>the</R1> <R2>dog</R2> <R3>is</R3> <R15>happy</R15> <R20>today</R20></R> )
escape param5 with htmlspecialchars('<R> ..... </R>'). Your browser currently sees it as html tags and parses it.
Use htmlspecialchars on the return value of print_r:
echo "<pre>";
echo htmlspecialchars(print_r($params, true));
echo "</pre>";
I want to write my Array into a readable format.
$result = array_diff($one, $two);
print_r($result);
Array ( [1] => 298GS [2] => 09283 [3] => U4235 )
This is how it should look like:
298GS
09283
U4235
My only idea is to write it like this:
echo $result[1];
echo $result[2];
echo $result[3];
But this is not very useful because I never know how many values my array will have.
There isn't much to say, just loop through the array and show the values. This works for variable number of items
foreach($result as $r){
echo $r."<br>";
}
Since you had difficulties doing such thing, I suggest you to study about the basics of the language (IF, lopps, variables, etc) - maybe that's what you are doing, IDK. Foreach and More.
I use the following function as I can read the keys too, the output showed below is what you'll actually see so it is formatted.
function debugArray($arr)
{
return "<pre>" . var_export($arr, true) . "</pre>";
}
$arr = [
1,
2,
3,
4,
5
];
echo debugArray($arr);
Output
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
)
Actual image of output.
You might want to check json_encode to export your data in json format so you can use it better with UI
I'm about ready to go crazy with this one. I can't seem to figure out how to print out elements of an array and then put all the results on one line (with a specific output).
Here's my array data (after running it through a foreach statement:
Array
(
[hostname] => server01
[server_id] => 4
[disk_mountpoint] => /
[disk_datetime] => 1426395600
[disk_device] => /dev/mapper/VolGroup00-LogVol00
[disk_capacity] => 15G
[disk_used] => 5.3G
)
Array
(
[hostname] => server01
[server_id] => 4
[disk_mountpoint] => /var
[disk_datetime] => 1426395600
[disk_device] => /dev/mapper/VolGroup01-LogVol00
[disk_capacity] => 107G
[disk_used] => 52G
)
Array
(
[hostname] => server01
[server_id] => 4
[disk_mountpoint] => /opt
[disk_datetime] => 1426395600
[disk_device] => /dev/mapper/VolGroup02-LogVol00
[disk_capacity] => 156G
[disk_used] => 127G
)
Here's my foreach statement:
foreach ($storage_report as $item) {
print_r($item);
}
I've spent a couple of hours trying to wrap my mind around a multi-dementional array as well as looking at some other peoples questions on StackOverflow, but nothing seems to fit what I'm trying to do.
By the way, the array above is actually output from a MySQL query and it returns only one unique hostname, so where it has the same hostname as an element, that is correct (same with the server_id).
I even tried a nested foreach loop and that didn't work well and I got confused again, so I didn't want to post it here.
Basically, with the data above, I want the output to look like:
$disk_mountpoint = $disk_used
With that being said, the ultimate output (if you were to parse what I just wrote), the final output would look like:
/ = 5.3G
/var = 52G
/opt = 127G
I feel like I'm over complicating things and wanted to know if someone could just point me in the right direction and help me out.
Thanks!
Drew
um.. just:
foreach ($storage_report as $item) {
echo $item['disk_mountpoint'] .'='.$item['disk_used'];
}
Yeah man over complication. Can't you just do
foreach ($storage_report as $item) {
echo $item['disk_mountpoint']."=".$item['disk_used'];
}
If it is that I undestand, so:
foreach ($storage_report as $item)
{
if (isset($item['disk_mountpoint']) && isset($item['disk_used']))
{
echo $item['disk_mountpoint'] .'='. $item['disk_used'];
}
}
That will iterate the entire collection and will read the content of each cell only if it is set.
I have an associative array which is generated dynamically with the values from database. When I print the whole array, it gives something like this when we put print_r($array).
Array ( [95a5c80811239526fb75cbf31740cc35] => Array ( [product_id] => 2324) )
When I echo like this,
echo $array['95a5c80811239526fb75cbf31740cc35']['product_id'];
it gives me product id.
But the problem is, the code '95a5c80811239526fb75cbf31740cc35' changes dynamically everytime. I want to echo the product id irrespective of this code.
I tried
$array[]['product_id'];
$array['']['product_id'];
But not working. Can anyone help me? Please ask me if you have any doubts.
You can use reset() in this case:
$array = array(
'95a5c80811239526fb75cbf31740cc35' => array( // dynamic
'product_id' => 2324
),
);
$value = reset($array); // set pointer to first element
echo $value['product_id']; // 2324
Assuming that the code is always the first element in the array:
$array[0]['product_id'];
If you collectively want all of the product ID's:
foreach($array as $product){
$productIds[] = $product['product_id'];
}
// $productIds is now what $array was, but without the codes, so the product_id's are the first elements.
You can use for each for this so that you can get the value of product Id
$array = Array ( [95a5c80811239526fb75cbf31740cc35] => Array ( [product_id] => 2324) )
foreach($array as $product){
echo $product['product_id'];
}
This would get your desired o/p
IF you are getting problem using Associative array then you can first convert it into numeric as follows
$arr=array( 'first' => array( 'product_id' => 2324) );
$arrr=array_values($arr);
echo $arrr[0]['product_id'];
Output:
2324
Hope this helps and to know about array_values go here
Depending on your situation, there are few possible solutions:
$array = array_shift(array_values(
Array(
'95a5c80811239526fb75cbf31740cc35' =>
Array(
'product_id' => 2324
)
)));
echo $array['product_id']; // 2324
Another solution, probably more efficient:
echo array_shift(array_slice($array, 0, 1)); // 2324
For PHP 5.4+ you could go with:
echo array_values($array)[0]; // 2324
$array[0]['product_id'];
Should do the trick.